FOR loop to populate a 2D array

This is a discussion on FOR loop to populate a 2D array within the C++ Programming forums, part of the General Programming Boards category; Hi,
I am trying to populate a two dimensional array using a FOR loop.
This is what I have made ...

Note that you don't necessarily need to do like this. It's better to define the loop variables inside the loops, as they will go out of scope when the loop ends. If you need the loop numbers outside the loops, then it might be better to define them outside:

I agree that declaring the variables in the for control is a better idea. However, note that even if you don't you still need to initialize j to 0 inside the for because after the first time through the outer loop it will be 2, not 0.