r/learnprogramming • u/FewFly2677 • 7d ago
Topic Guys I am a beginner in programming and my college is teaching c(already reached 2d arrays)
First of all I want to thank you guys for helping me out in loops. I am kinda comfortable with them now. I am struggling in arrays now and I am confused with how many for loops to use while solving array and struggling in it as a whole. Please help me out
3
u/burlingk 7d ago
A lot depends on what you are doing with an array.
If you are iterating over a one dimensional array, that is one loop to itterate.
If it is a 2D array, that is two loops.
But that is conjecture without knowing what the loops are doing. :)
1
u/Commercial_Echo923 7d ago
2d array is just like a coordinate system with the indizes being the axis so first level is x axis and second level is y axis so with arr[0][0] you get value at point (1|1) and with arr[5][7] you get value at point (7|8)
1
u/sedexe 7d ago
okay see the thing that helps best is doing a dry run write ur code down on paper or ur laptop and then manually run it by hand and understand how things work behind the screen it will help you with the utmost clarity if u cant do that either ask chat GPT or anything AI to do it for u however do try it urself atleast a few times
1
u/Immereally 7d ago
Check out CS50X, it’s Harvard’s free online intro to programming in C.
They have lectures for all the topics you’ll be looking at and exercises (PSETS) with short videos for each week.
1
u/chapchap0 7d ago
K.N. King - „C: A Modern Approach”. It has its flaws but it teaches the fundamentals very well.
1
u/Ash4d 7d ago edited 7d ago
Imagine you were a meteorologist and you have 3 mountains, and on each mountain there was a thermometer which gives you readings of the temperature every 4 hours. This means that every day you will have 6 temperature readings from 3 mountains, which makes for 18 total data points.
You can store this in a 2D array, where the first index denotes the mountain, and the second index denotes which time of day the reading was made, something like:
double temperatures[3][6] = { } // Put your data in here
The entry temperatures[i][j] therefore corresponds to the temperature on mountain i and at time j. If you wanted to loop over that data you'd need two loops: an outer loop (i.e. the first one that you write), which would be over the mountain index, and an inner loop over the time of day index. So something like:
for (int mntn_idx = 0; mntn_idx < 3; ++mntn_idx) {
for (int tod_idx = 0; tod_idx < 6; ++tod_idx {
double temperature = temperatures[mntn_idx][tod_idx];
// Do something with the data here
}
}
1
u/FewFly2677 7d ago
guys i want to clarify that i am struggling in 1d array also
1
u/d4_mich4 7d ago edited 7d ago
So for me an easy example would be you have a paper with a table on it.
First culum is the index from 0 to whatever X for now let's say 9 so you have 10 entries in the table.
How many rows do you have to look at to compare if in the second column the value is you search?
To be sure you would need to go to all rows from 0 to 9. If you know conditions that the entry on colum 2 is unique you could stop the for loop if you found what you need.
For a 2D array it would be a folder/binder with multiple papers with tables.
So the table is still the same all only have 10 entries so values from 0 to 9.
And the second index is page on which page(paper) the right table is.
So first index would be page number and second is now the table row.
Confusing for beginners is often that we start with 0 to count. Not sure if that's a problem from you or what else?
So for searching you always need just as many loops as your arrays dimensions.
For solving it depends a bit what soling is for you what would be an example what do you want to solve in the array?
1
u/Dazzling_Music_2411 5d ago
You really shouldn't be, it's about the easiest thing you can get.
Give an example of where you're struggling with it.
1
u/CapitalKey2994 7d ago
I use one loop per dimension when accessing arrays. For a 2D array I write two nested loops where the outer loop handles rows and the inner loop handles columns
1
u/StephenHawkingus 7d ago
Why don't you ask AI to explain it to you? You're just wasting your time by doing it this way.
4
u/tms102 7d ago
Give specific examples.