Array Fundamentals

What is an Array?

An array is a collection of data of the same type. Array elements are indexed or subscripted, just like x1, x2, ..., xn in mathematics.

Array is an example in Fortran 90 of a compound object (i.e., an object which can have more than one value).

Important Components of an Array

A one-dimensional array has the following important components:

Declaring an Array

The syntax for declaring arrays is the following:
type, DIMENSION( extent ) :: name-1, name-2, ..., name-n
where type is the type of the arrays name-1, name-2, ..., name-n, DIMENSION is a required keyword, and extent gives the range of the array indices.

For example, suppose we have the following array declarations:

REAL, DIMENSION(-1:1)     :: a, Sum
INTEGER, DIMENSION(0:100) :: InputData
The integers in an extent can be PARAMETERs:
INTEGER, PARAMETER :: MaximumSize = 100
LOGICAL, DIMENSION(1:MaximumSize) :: AnswerSheet

INTEGER, PARAMETER :: LowerBound = -10
INTEGER, PARAMETER :: UpperBound =  10
REAL, DIMENSION(LowerBound:UpperBound) :: Score, Mark
In the above, the range of array AnswerSheet is 1 and 100, while the range of arrays Score and Mark is -10 and 10.

Array Elements

An element of an array has a form of the following:
array-name ( integer-expression )
where array-name is the name of the array, and integer-expression is an expression whose final result is an integer. The result of integer-expression, which must be an integer in the range of the extent, gives the index or the subscript of the desired array element.
REAL, DIMENSION(-1:1)     :: a, Sum
INTEGER, DIMENSION(0:100) :: InputData
Consider array a declared above. Its elements are a(-1), a(0) and a(1). The elements of array InputData are InputData(0), InputData(1), ..., InputData(100). Moreover, if integer variables i and j have values 3 and 8, respectively, InputData(j-i) is equivalent to InputData(5), InputData(i*j) is equivalent to InputData(24), and InputData(j/i) is equivalent to InputData(2).

However, InputData(3.0) is incorrect, since array index or subscript must be integers.

Since indices of an array must be within the lower bound and upper bound of the extent used to declare the array, InputData(20*i+j*j) is equivalent to InputData(124), which is wrong since 124 is larger than the upper bound. Similarly, Sum(i-j) is equivalent to Sum(-5), which is also wrong since -5 is less than the lower bound -1.

The value of the integer-expression used as an index or subscript for an array element must be in the range of the extent used to declare the array.