8.1 What are arrays and why we use them
8.2 How to define and use FORTRAN ARRAYS
ONE DIMENSIONAL ARRAYS
To store data in the computer
we must define an array and specify the type of data and how many
elements will be in the array. By type we mean, REAL, INTEGER,
CHARACTER, etc, and the number of elements is called the dimension
of the array.
1 | JAN | 672 | 3.4 |
2 | FEB | 609 | 3.2 |
3 | MAR | 715 | 3.7 |
4 | APR | 803 | 4.2 |
5 | MAY | 810 | 4.8 |
6 | JUN | 831 | 5.1 |
7 | JUL | 829 | 5.1 |
8 | AUG | 827 | 5.1 |
9 | SEP | 780 | 4.3 |
10 | OCT | 703 | 3.9 |
11 | NOV | 791 | 4.2 |
12 | DEC | 783 | 3.6 |
To store the above data we could
use :
CHARACTER (LEN=3) :: MONTH(12) INTEGER :: UNITS(12) REAL :: SALES(12)
INPUT/OUTPUT OF DATA
ARRAYS
PROGRAM RPARRAYS IMPLICIT NONE CHARACTER(LEN=3) :: MONTHS(12) READ 33,MONTHS 33 FORMAT(12('',A3)) PRINT 33,MONTHS DO I=1,12 PRINT 70,I,MONTHS(I) 70 FORMAT (' MONTH ',I2,' IS ',A3) END DO STOP END PROGRAM RPARRAYS
/DATA
JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
/ENDRUN
OUTPUT :
JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC MONTH 1 IS JAN MONTH 2 IS FEB MONTH 3 IS MAR MONTH 4 IS APR . . .
INDEXING OF DATA ARRAYS
Before we proceed to do some
calculations with the above data, there is an important concept
we need, that of an Index. An index is a pointer, an INTEGER number
or variable ( say K ) which we can use to point at a single element
of an array or at all the elements in turn. In the diagram we
have 12 rows corresponding to the 12 months of the year. For this
case we can use an Index which takes on values from 1 to 12. That
is, the index must lie within the range of number of elements
declared. Also in the diagram we have written the name of the
array and a subscript in brackets beside the individual elements.
This is one of the ways we can refer to a single value in the
array.
1 | JAN | 672 | 3.4 |
2 | FEB | 609 | 3.2 |
3 | MAR | 715 | 3.7 |
4 | APR | 803 | 4.2 |
5 | MAY | 810 | 4.8 |
6 | JUN | 831 | 5.1 |
7 | JUL | 829 | 5.1 |
8 | AUG | 827 | 5.1 |
9 | SEP | 780 | 4.3 |
10 | OCT | 703 | 3.9 |
11 | NOV | 791 | 4.2 |
12 | DEC | 783 | 3.6 |
SUBSCRIPTED VARIABLE
We use the term a subscripted
variable to describe an array to distinguish it from simple variables
which can only store a single value.
In the diagram we should be clear
in our use of the terms :
Array | MONTH |
Subscripted Variable | SALES(K) |
Subscript | (2) (K) |
Index | K |
Array element | UNITS(2) |
Value of an array element | 609 |
TRY TO USE ARRAY INSTEAD
OF LARGE BLOCKS OF IF-THEN STATEMENTS
Bad :
... IF ( MSG = = 128 ) THEN PRINT *,'FILE TRANSFER FINISHED' ELSE IF ( MSG = = 129 ) THEN PRINT *,'FILE TRANSFER ABORTED' ... ELSE IF ( MSG = = 157 ) THEN PRINT *,'OUT OF MEMORY' END IF ...
Better :
... CHARACTER (LEN=30) :: MSGTAB(30) ... PRINT *,MSGTAB(MSG-127) ...
APPLICATION OF ARRAYS :
85-100 | A |
80-84 | A- |
75-79 | B+ |
70-74 | B |
65-69 | B- |
60-64 | C+ |
55-59 | C |
50-54 | C- |
45-49 | D |
0-44 | F |
85-100 | A |
80-84 | A- |
75-79 | B+ |
70-74 | B |
65-69 | B- |
60-64 | C+ |
55-59 | C |
50-54 | C- |
45-49 | D |
0-44 | F |
85 | A |
80 | A- |
75 | B+ |
70 | B |
65 | B- |
60 | C+ |
55 | C |
50 | D |
0 | F |
PROGRAM EXAMPLEIMPLICIT NONE
INTEGER :: RANGE
CHARACTER (LEN=2) :: GRADE(9)
GRADE(1) = 'F'
...
GRADE(9) = 'A'
...
RANGE = ( INT(FINAL) - 40 ) / 5
IF ( RANGE < 1 ) THEN
RANGE = 1
ELSE IF ( RANGE > 9 ) THEN
RANGE = 9
END IF
200 PRINT *, NAME, FINAL, GRADE(RANGE)
STOP
END PROGRAM EXAMPLE
PROGRAM EXAMPLE
IMPLICIT NONE
INTEGER :: RANGE
CHARACTER (LEN=2) :: GRADE(9)
GRADE(1) = 'F'
...
GRADE(9) = 'A'
...
RANGE = ( INT(FINAL) - 40 ) / 5
200 PRINT *, NAME, FINAL, GRADE(RANGE)
STOP
END PROGRAM EXAMPLE
SEE OTHER PROGRAM EXAMPLES :
On to the next lecture
Go back to lecture menu