# FORTRAN Arrays

Arrays
Many scientific computations use vectors and matrices (subscripted variables). The data type Fortran uses for representing such objects is the array. A one-dimensional array corresponds to a vector, while a two-dimensional array corresponds to a matrix. To fully understand how this works in Fortran, you will have to know not only the syntax for usage, but also how these objects are stored in memory in Fortran.

The DIMENSION Statement
When subscripted variables are used in a program, certain information about them must be supplied to the FORTRAN compiler:

Which variables are subscripted?
How many subscripted are there for each subscripted variable?
What is the maximum size of each subscript?

The DIMENSION statement answers these questions. Every subscripted variable in a program must be mentioned in a DIMENSION statement, and this statement must appear before the first occurrence of the variables in the program. A common practice is to give the dimension information for all subscripted variables in DIMENSION statement at the beginning of program.

Example:

DIMENSION X(10), A(2,5), M(3,4,5)

This will assign 10 storage locations to the one-dimensional array named X; 10 (2 X 5) to the two-dimensional array A; and 60 (3 X 4 X 5) to the three-dimensional array M.

Example - Handling matrix elements using arrays
C Handling 3x3 matrix using 2D arrays
DIMENSION A(3,3)
C Entering matrix elements
DO 20 IROW=1,3
DO 20 ICOL=1,3
WRITE(6,30) IROW, ICOL
READ(5,*) A(IROW,ICOL)
20    CONTINUE
30    FORMAT('/Enter element (',I1,',',I1,') = ')
C Display the matrix
WRITE(6,*) 'Here is the matrix '
DO 40 IROW=1,3
WRITE(6,50) (A(IROW,ICOL),ICOL=1,3)
40    CONTINUE
50    FORMAT(3F7.2)
STOP
END