DIMENSION A(27),KKK(7) REAL A(27) INTEGER KKK(7)all declare arrays, the last two also declare its type. An array name follows exactly the same rules as a variable name (including the implicit integer rule for I,J,K,L,M,N). The difference is that an array can contain one value for each of its elements (in the above example array A has 27 and array KKK has 7). You cannot have an array and a variable with the same name in the program unit. Individual elements of an array are accessed using:-
array name (expression)If expression is real it is converted to an integer: (but its not a good practise to use real expressions). The expression is called the array index and MUST ALWAYS lie in the range 1 to the size of the array. For example for array A the index must lie in the range 1 to 27. An array element can be treated exactly like a variable - it can be used in expressions and can have a value assigned to it. Shown below are some valid array elements:-
A(1) A(3*K) A(5+ 7*KKK(L))The last example shows KKK(L) being used in an expression for the element of A.
INTEGER NUM(10) INDEX=1 10 IF (INDEX.LE.10) THEN NUM(INDEX)=10*INDEX INDEX=INDEX+1 GOTO 10 ENDIF 20 IF (INDEX.GE.1) THEN PRINT *,NUM(INDEX) INDEX=INDEX-1 GOTO 20 ENDIF STOP END
You will notice something rather strange about the results. Not only does it type the answers 100 down to 10, it also first types the number 11. In fact this simple program rather elegantly demonstrates several points:-
As we have just seen FORTRAN does not normally check that the index refers to a legal element in the array and this is a source of some of the most spectacular bugs, for trying to store data using an illegal index will overwrite other data or even the program. To see what happens try this:-
DIMENSION A(2) N=1 10 A(N)=0 N=N+10 PRINT *,A(N) GOTO 10 END
FORTRAN is a static language, that is to say all the storage required for variables and arrays must be reserved at compilation time. This means that (unfortunately) you cannot do this:-
N=27 DIMENSION A(N)FORTRAN allows multi-dimensional arrays, so all the following statements are valid :-
REAL XYZ(3,5) INTEGER IDAY(31,12) DIMENSION POLY(5,7,3,4)
The number of elements of storage used can be found by multiplying all the individual index limits together so, for example, in the above XYZ has 15 elements. The way that FORTRAN stores a multi-dimensional array in memory is by starting with all indeces at 1 and then varying the LEFTMOST one through its range, then increasing the next index by 1 and repeating. So, for example, the array XYZ above would be stored:-
XYZ(1,1) XYZ(2,1) XYZ(3,1) XYZ(1,2) ... XYZ(2,5) XYZ(3,5)
INTEGER NUM(3,3) I=1 10 IF (I.LE.3) THEN J=1 20 IF (J.LE.3) THEN NUM(I,J)=I+3*J-3 J=J+1 GOTO 20 ENDIF I=I+1 GOTO 10 ENDIF I=1 30 IF (I.LE.9) THEN PRINT *,NUM(I,1) I=I+1 GOTO 30 ENDIF STOP END
Try, where possible to avoid multi-dimensional arrays with many dimensions because the calculation needed to find the position of the element relative to the start of the array can be very time consuming. For example, if array MULTI is declared:-
DIMENSION MULTI(5,7,3,4)then to find the element:-
MULTI(I1,I2,I3,I4)requires the following calculation:-
(((I4-1)*3+I3-1)*7+I2-1)*5+I1-1Statements that declare arrays should precede executable statements. See section 18 for more details about the placing of statements.