I was reading a lecture on arrays for Fortran 90
and I came across this sentence :
‘Fortran always stores by columns – the first subscript
varies more rapidly than the second, and so on.’
What does the author mean to say ?
1
These are really two separate points, so let’s address them separately:
“Fortran always stores by column.”: This means that a(i,j)
denotes the element of the 2D array a
corresponding to column i
, row j
.
“The first index varies more rapidly than the second, and so on.”: For this to make sense, it helps to think about arrays being stored on a one-dimensional medium, regardless of their dimension. For a 2D array with m
rows and n
columns, we would store the first element of the second column immediately next to the last element of the first column, etc. This is equivalent to ‘stacking’ the columns. The distance in this 1D structure between a(i,j)
and a(i,j+1)
is one, but the distance between a(i,j)
and a(i+1,j)
is m
, the length of one column. The amount of distance being traversed when adding one to the column index is higher, so this index ‘varies more rapidly’.
As an example, if you have an array A such that A(1, 1)=100, A(2, 1)=200, A(1, 2)=300, A(2, 2)=400, and if you have the following line in your Fortran 90 code
print*, A
you will get “100 200 300 400” in your output instead of “100 300 200 400”.