Hello, Modou J.,
In FORTRAN, and unlike most other programming languages (at least of those I'm familiar with), it is the left-most subscript of a multi-dimensioned array which varies fastest, and the right-most which varies the slowest. The algorithm for locating the (i, j)'th element of the array. however is similar for either row- or column-major arrays. The difference is whether you start from the left-most, or the right-most, index (respectively), and we have to remember that FORTRAN arrays are 1-based, not 0 based.
First, let's make clear that the idea, here, is to take the 2 indicies, and calculate a *single* index, which the the offset to the desired element. Let's take the following as an example:
INTEGER*1 A(2,3)
In memory, A is a block of 2x3, or 6, INTEGER*1 (byte-size) elements; so A looks like this:
A(1,1): INTEGER*1 0: <- the address of this element is the start of A, and the 0th element
A(2,1): INTEGER*1 1:
A(1,2): INTEGER*1 2:
A(2,2): INTEGER*1 3:
A(1,3): INTEGER*1 4:
A(2,3): INTEGER*1 5
If we want to access element A(2,2), we need to calculate an offset of 3 (0-based) from the start of A, to reach the 4th element of A.
For each array, we need to know the dimensions and their sizes. So, let's assume that we have the following variables for array A:
(Note, I'm going to write the following code in C/C++, but it should be easy to translate to whatever language you choose.)
int const NumberOfDimensionsOfA = 2;
int DimensionSizesOfA[NumberOfDimensionsOfA] = { 2, 3 };
If we now want to reference A(i, j), we need to use the following expression:
... A + ((i - 1) + ((j - 1) * DimensionSizesOfA[0])) ...
or
... A + ((i - 1) + ((j - 1) * 2)
We have to subtract 1 from i and j, because of FORTRAN's 1-based indexing. We have to multiply (j - 1) by the size of the left-most dimension, because each increase in j means that many elements have to be skipped.
If A were, instead, a 3 dimensional array, the expression A(i,j,k) might result in something like this:
... A + ((i - 1) + ((j - 1) * DimensionSizesOfA[0])) + ((k - 1) * DimensionSizesOfA[0] * DimensionSizesOfA[1])) ...
-+- Sid M