10.2 How to define and use a matrix in FORTRAN
MATRICES
Two dimensional arrays or matrices
of data occur naturally in many areas of engineering, science
and business. Any kind of tabular data, such as results of an
experiment or the performance of a company or stock can be considered
a matrix.
In FORTRAN, we have to define
the number of ROWS and the number of COLUMNS of
a matrix in that order after the variable name in the declaration
statements.
General form :
NAME_OF_MATRIX(ROWS,COLUMNS)
Examples :
READING AND PRINTING
MATRICES
* Arrays are stored by columns.
SEE EXAMPLES :
P71.F90 MATRIX
INPUT/OUTPUT
P72.F90 MATRIX
SUMMARY
P73.F90 MATRIX
MULTIPLICATION PROGRAM
On to the next lecture
INTEGER :: DAYS(12,31)
REAL :: YSALES(12,2)
CHARACTER (LEN=3) :: SEASON(4,3)
PROGRAM MATRICES
IMPLICIT NONE
CHARACTER(LEN=3) :: MONTHS(3,4)
READ 33,MONTHS
33 FORMAT (12(' ',A3))
PRINT 33,MONTHS
L1: DO I=1,3
L2: DO J=1,4
PRINT 40,I,J,MONTHS(I,J)
40 FORMAT (' ROW ',I1,' COLUMN ',I1,' IS ',A3)
END DO L2
END DO L1
STOP
END PROGRAM MATRICES
/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
ROW 1 COLUMN 1 IS JAN
ROW 1 COLUMN 2 IS APR
ROW 1 COLUMN 3 IS JUL
ROW 1 COLUMN 4 IS OCT
ROW 2 COLUMN 1 IS FEB
:
Go back to lecture menu