Arrays
One Dimensional Arrays
Each of the following reserves memory for storing values in a one dimensional array of the specified data type.
integer a(20) real b(0:19) logical c(-162:237)
Each element of each array can be thought of and treated as a separate variable.
integer i, sq(10) do 100 i=1,10 sq(i) = i**2 100 continue
Two Dimensional Arrays
Create a two-dimensional array by providing the number of rows, number of columns in the declaration. FORTRAN 77 does not allow dynamically created (at runtime) arrays, you must pre-allocate enough memory for any data that you will need to store.
integer a(4,3)
The above declaration reserves space for 12 integers and the value for each integer can be accessed by name(row,col) as shown in this table.
a(1,1) | a(1,2) | a(1,3) |
a(2,1) | a(2,2) | a(2,3) |
a(3,1) | a(3,2) | a(3,3) |
a(4,1) | a(4,2) | a(4,3) |
Unlike MATLAB, FORTRAN stores the values in consecutive memory locations in column major order. This means that the data is actually stored in memory this order:
a(1,1) |
a(2,1) |
a(3,1) |
a(4,1) |
a(1,2) |
a(2,2) |
a(3,2) |
a(4,2) |
a(1,3) |
a(2,3) |
a(3,3) |
a(4,3) |
a(1,4) |
a(2,4) |
a(3,4) |
a(4,4) |
For efficient processing of 2D arrays in FORTRAN, the outer loop should process the column and the inner loop should process each value in the current column.