0
C++_ Need help accessing a 2D list with a for loop.
Using C++ I need help understanding how to access a 2D list with a for loop. I found an example from Geeks for Geeks, but they use variables like "i" and "j". Link from GeeksforGeeks https://www.google.com/amp/s/www.geeksforgeeks.org/multidimensional-arrays-c-cpp/amp/ Can someone explain what is going on? What is the first loop and second loop accessing? My Code Example: https://code.sololearn.com/cxM9Yb8GB7O1/?ref=app
5 Antworten
+ 1
You can use any variable in place of i and j ,the outer for loop runs once ,then inner for loop runs 3 times accessing the 3 values in the first row ,then outer loop runs again ,and inner loop again accessing 3 values in the second row ,so yea outer loop represents loop for rows and inner loop represents loop for columns
0
Thanks Abhay for the explanation. I need to visually see this.
c1, c2, c3
// row 1 {1, 2, 3}
// row 2 {4, 5, 6}
I think of it like this:
For (rows){
For (columns){
.........
}
}
So when the outer loop runs, it gets the 1st array and the inner loop accesses each index within.
Row 1 {1, 2, 3}
Index 0, 1, 2
After this step, the outer loop accesses the 2nd array.
Row 2 {4, 5, 6}
Index 0, 1, 2
Would this be correct?
0
Thank you so much AteFish🇧🇩 for the explanation.