The idea of an array is that we have a numbered collection of variables all of the same type and with similar names. The variables are called elements of the array.
How do we declare an array
The syntax for this has several options.
typename :: arrayname(M:N)
or
typename :: arrayname(N)
or
typename, Dimension(N) :: arrayname
where M and N are integers, M <= N, indicating the minimum and maximum subscript values for the array. The first value M is optional; if it is not present, it defaults to the value 1. The second value N is mandatory. If M is not coded, N must therefore be >= 1.
Examples
Integer :: NUM(1:100)
Integer :: NUMB(100)
Real :: XVALUE(50), YVALUE(50)
Character*20 :: NAME(100)
Real :: TEMP(32:212)
Logical :: CSTYLE(0:10)
Here notice:
How do we use an array?
We can refer to an element of an array by using the array name followed by an Integer expression in parentheses. For example:
The subscript must be an Integer-valued expression. Its value must be in the possible range for the array we are using.
Arrays and loops
We often use loops when we work with arrays. For instance, if B is an array of 10 Real elements, we could load it with data with a loop:
DO I=1, 10
READ *, B(I)
END DO
Likewise, we could use a loop to find the sum or product of the values in the array.
How do we initialize an array?
As with other variables, we can assign values to the elements of an array when we declare it. An example of this is:
Integer :: A(8) = (/ 13, 23, 33, 43, 53, 63, 73, 83 /)
where the expression on the right-hand side is an example of an array constant.
There are a couple of variations on this. If we want the same value repeatedly, we can use a multiplier:
Integer :: B(8) = (/ 4*13, 3*23, 33 /)
and now the elements of B are four 13s, three 23s and one 33, in that order.
If we want the values to be calculated from the subscript, we can use an implied Do loop:
Integer :: C(8) = (/ (4*I+3, I = 1, 8) /)
and now the elements of C are 7, 11, 15, etc.
Whole array operations
If we have two or more arrays of the same type and size, we can often do useful operations just by using the array names:
A = B ! Set A(I) = B(I) for each I.
A = B + C ! Set A(I) = B(I) + C(I) for each I.
A = C + 3 ! Set A(I) = B(I) + 3 for each I.
Passing arrays as parameters
If an array is passed as an argument to a function, its dimension can (and often should be) also be passed in as an argument.
Example
Function MAXVAL(N, P)
Implicit None
Integer :: N, MAXVAL
Integer :: P(N)
(rest of the function)
For this to make sense, the actual arguments passed to MAXVAL will have be an INTEGER and an array of INTEGER, in that order.
Because we passed in the size N of the array P, we have a more flexible and useful function than if we had stuck to a specific size such as 8 or 10.
Multi-dimensional arrays
Sometimes we have data that makes sense not as an ordinary array but organized rather in rows and columns. We can have a two-dimensional array. This can be declared like this:
REAL :: T(3, 4)
which gives us 3 rows and 4 columns.
We can refer to an element of the array T by using 2 subscripts, as in:
A = T(2, 3)
Print *, T(I, 3)
T(3, J) = 2.9
where I and J are Integer variables.
If we want to print the values in the array T, we can use loops and implied loops:
Do I = 1, 3
Print *, (T(I, J), J = 1, 4))
End Do
which will print out the values in the array like this:
3.1 4.2 5.6 7.9
-1.4 6.8 1.1 0.7
0.0 3.5 9.1 5.6
We could print the array in various other ways by writing the loops differently.