Two Dimensional Array |
|
This array represents a matrix since it is two-dimensional. |
Let us consider a 4 x 3 array A which will be stored in the computer as: |
A(1,1), A(2, 1), A(3,1), A(4,1), A(1,2), A(2,2), A(3,2) |
A(4,2), A(1,3), A(2,3), A(3,3), A(4,3) |
Here the second subscript is set to 1 when the first subscript changes from 1 to 4; the second subscript is then set to 2, first subscript changes from 1 to 4. The first subscript changes rapidly and the second less rapidly. Arrays are stored column wise in Fortran language. |
Matrix: An array with a rank of 2 or greater is called a matrix |
Matrices are usually represented by two-dimensional arrays. |
Example: |
The declaration is: |
real A(3,5) |
It defines a two-dimensional array of 3*5=15 real numbers. Its first index as the row index, and the second as the column index. |
Hence we have: |
(1,1) (1,2) (1,3) (1,4) (1,5) |
(2,1) (2,2) (2,3) (2,4) (2,5) |
(3,1) (3,2) (3,3) (3,4) (3,5) |
The general syntax for declarations is: |
name (low_index1 : hi_index1, low_index2 : hi_index2) |
The total size of the array is then |
size = (hi_index1-low_index1+1)*(hi_index2-low_index2+1) |
It is common in Fortran to declare arrays that are larger than the matrix we want to store because Fortran does not have dynamic storage allocation. |
Example: |
real A(3,5) |
integer i,j |
c |
c |
do 20 j = 1, 3 |
do 10 i = 1, 3 |
A (i, j) = real (i)/real(j) |
10 continue |
20 continue |
Here we have used only 3 by 3 numbers. |
Example: |
Let us consider three two dimensional arrays with this example |
Program: |
This program is of two dimension which display the output in the matrix form. |
 |
Output: |
 |
|
Three dimensional arrays |
Let us consider A(2, 3, 2) is a three dimensional array which have 2 x 3 x 2 elements. These are stored in the computer and their values will be as: |
A(1,1,1), A(2,1,1), A(1,2,1), A(2,2,1), A(1,3,1), A(2,3,1) |
A(1,1,2), A(2,1,2), A(1,2,2), A(2,2,2), A(1,3,2), A(2,3,2) |
Computer memory always stores the dimensions of the array in linear form. Fortran 77 allows arrays of up to seven dimensions. |
The dimension statement |
There is an alternate way to declare arrays in Fortran 77. |
The statements: |
real A, x |
dimension x(50) |
dimension A(10,20) |
These are equivalent to real A(10,20), x(50) |