next up previous contents index
Next: Arithmetic Up: Arrays Previous: Using Arrays

Storage Sequence

  Arrays are always stored in a contiguous set of memory locations. In the case of multi-dimensional arrays, the order of the elements is that the first subscript varies most rapidly, then the second subscript, and so on. For example in the following 2-dimensional array (for simplicity one of only six elements):
X(2,3) = \left[ \begin{array}
{lll} x_{1,1} & x_{1,2} & x_{1,3} \\  
x_{2,1} & x_{2,2} & x_{2,3} \end{array} \right] \end{displaymath} (1)

The elements are stored in the following sequence:
X(1,1), X(2,1), X(1,2), X(2,2), X(1,3), X(2,3)
i.e. the sequence moves down each column first, then across to the next row. This column order is different from that used in some other programming languages.

The storage order may be important if you use large multi-dimensional arrays and wish to carry out some operation on all the elements of the array. It is then likely to be faster to access the array in storage order, i.e. by columns rather than rows. This means arranging loop indices with the last subscript indexed by the outer loop, and so on inwards. For example:

      SUM = 0.0D0 
      DO 250,L = 1,100 
          DO 150,K = 1,100 
               SUM = SUM + ARRAY(K,L) 
150       CONTINUE 
With the loops arranged this way around the memory locations are overhead in subscript calculations.

Helen Rowlands