There are several ways of using DATA statements to initialise arrays, all of them simpler and more efficient than the equivalent set of DO-loops. Perhaps the most common requirement is to initialise all the elements of an array: in this case the array name can appear without subscripts. If several of the elements are to have the same initial value a repeat count can be precede any constant:
REAL FLUX(1000) DATA FLUX / 512*0.0, 488*-1.0 /
The total number of constants must equal the number of array elements. The constants correspond to the elements in the array in the normal storage sequence, that is with the first subscript varying most rapidly.
Named constants are permitted, but not constant expressions. The repeat count may be a literal or named integer constant. To initialise a multi-dimensional array with parameterised array bounds it is necessary to define another integer constant to hold the total number of elements:
PARAMETER (NX = 800, NY = 360, NTOTAL = NX * NY) DOUBLE PRECISION SCREEN(NX,NY), ZERO PARAMETER (ZERO = 0.0D0) DATA SCREEN / NTOTAL * ZERO /
If only a few array elements are to be initialised they can be listed individually:
REAL SPARSE(50,50) DATA SPARSE(1,1), SPARSE(50,50) / 1.0, 99.99999 /
The third, and most complicated, option is to use an implied-DO loop. This operates in much the same way as an implied-DO in an I/O statement:
INTEGER ODD(10) DATA (ODD(I),I=1,10,2)/ 5 * 43/ DATA (ODD(I),I=2,10,2)/ 5 * 0 /
This example has initialised all the odd numbered elements to one value and all the even numbered elements to another. Note that the loop control variable (I in this example) has a scope which does not extend outside the section of the DATA statement in which it is used. Any integer variable may be used as a loop control index in a DATA statement without effects elsewhere; the value of I itself is not defined by these statements.
When initialising part of a multi-dimensional array it may occasionally be useful to nest DO-loops like this:
DOUBLE PRECISION FIELD(5,5) DATA ((FIELD(I,J),I=1,J), J=1,5) / 15 * -1.0D0 /
This specifies initial values only for the upper triangle of the square array FIELD.