Manipulating multi-dimensional arrays using loops within loops
There are many ways in which to use loops to access elements within multi-dimensional arrays. The best way, however, is to use nested for()…
loops. In a nested loop, the outermost loop contains one or more loops within it, nested such that the outer loop completely contains the inner loop. When using nested loops, the outermost loop manipulates the index of the highest-order dimension, while the innermost loop manipulates the index of the lowest-order dimension. We will explore 2D and 3D looping. It is then simple to extend the nested loops to as many as are needed for an array with more than three dimensions.
When nesting loops, it is a common convention to use variables named i
, j
, k
, and so on to hold the values of the array offsets, with i
being the first-order dimensional offset, j
being the second-order dimensional offset, k
being the third-order dimensional offset, and so on. This convention is optional...