FORTRAN 90+: ARRAYS
An extremely important topic in engineering and science, the use of arrays in modern science is widespread. An array is a set of data that ranges from one dimensional to three dimensional and is used to represent either vectors or matrixes (2D or 3D).
1D Arrays
The format for declairing one dimensional arrays is...
type, DIMENSION(extent) :: name1, name2, name3
The extent of an array is how you define the indexes of the array. The type is the same as for other variables [CHARACTER REAL INTEGER LOGICAL COMPLEX]. Instead of defining all arrays as having indexes of one to the size of the array, the index is set by the user to be any smaller number to a bigger number. This means that if you so desire your extent can be -3 to 7. [Note: the size of the this array is 11 not ten. Count the positions to check for yourself]. If you want to set the size of an array to be a parameter is can be done by...
INTEGER, PARAMETER :: lower=-3, upper=7 type, DIMENSION(lower:upper) :: name1, name2
The contents of an array can be initialized in the same way as any variable; by entering the data in at the begining.
Indexing
To pull the value out a specific position in an array you will use the name of the specific array followed by parathesis and an integer that falls within the extent of the array.
a= x(i)
To set the value at a point simply reverse the above to look like
x(i)= a
Implied Do
Implied DO is a method of setting the values of arrays. The basic syntax of an implied DO is...
(item, item2, item3, var= lower_bound, Upper_bound, step_size) !as many items as want
The following are some examples of how implied DO loops may be implimented.
(i, i=-3,3,1)
The result of the above statement would be an array (-3,-2,-1,0,1,2,3). The items can also be equations. For example...
(i, i*2, i=-1,1,1)
This would result in (-1,-2,0,0,1,2).[Note: the value of ’i’ is implimented in each item before moving to the next value of ’i’]
The item can also be another implied DO statement. For example...
((i*j, i=-1,1,1), j=-2,1)
Which results in (2,0,-2,1,0,-1,0,0,0,-1,0,1). This result is achieved by working from right to left inputing the first value of ’j’ then going through the second Implied DO as if it were (i*-2, i=-1,1,1). Also, although represented here as horizontal figures they are stored and used as vertical columns. Another important use of the implied DO is to set values reading in one at a time using the READ function. This may look like
INTEGER :: I INTEGER, PARAMETER :: upper=5, lower=1 INTEGER, DIMENSION(lower:upper) :: A READ(*,*) (A(I), I=lower,upper)
The above script would require the user to enter all the numbers in the correct order at once or one or multiple at a time till the program could see that the positions lower through upper were filled.
Next Page →
Next Page →