To traverse a two-dimensional array, we use a loop within a loop. The outer loop controls the row offset and the inner loop controls the column offset. Our nested loop structure is as follows:
for( j = 0; j < size2D ; j++ ) { // j : 0..(size2D-1)
for( i = 0; i < size1D ; i++ ) { // i : 0..(size1D-1)
array2D[j][i] = (10*j) + i ;
}
}
Each element of array2D is assigned a value computed from i and j.
Note that for each loop counter variable, we showthe range of valid values as a comment. This is only a reminder for us; the goal is to help us keep our offsets within the proper range.