200 likes | 364 Views
Two Dimensional Arrays. One dimension . Rank 1 Array INTEGER, DIMENSION (3) :: a. Row 1. Row 2. Row 3. Two dimension . Rank 2 Array INTEGER, DIMENSION (3,5) :: a. Row 1. a(2,4). Row 2. Row 3. Col 1. Col 2. Col 3. Col 4. Col 5. Rank 2 Array Declaration .
E N D
One dimension • Rank 1 Array • INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3
Two dimension • Rank 2 Array • INTEGER, DIMENSION (3,5) :: a Row 1 a(2,4) Row 2 Row 3 Col 1 Col 2 Col 3 Col 4 Col 5
Rank 2 Array Declaration • INTEGER , DIMENSION (3,5) :: a • INTEGER , DIMENSION (0:2,-2:2) :: a • The first index gives the ROW number while the second provides the COLUMN number.
Initialization • Element by element INTEGER :: num=0 ,i,j INTEGER, DIMENSION (3,5) :: a DO i = 1,3 DO j = 1,5 num=num+1 a (i,j)=num END DO END DO
Initialization b) Using Array Constructor INTEGER, DIMENSION (3,5) :: a a=(/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15/) ERROR! a=reshape((/1,2,3,4,5,6,7,8,9,10,11,12,& 13,14,15/),(/3,5/) ) √
Initialization c) With READ statements Row by row INTEGER, DIMENSION (3,5):: a INTEGER :: i, j DO i=1,3 READ (*,*) (a(i,j),j=1,5) END DO
Initialization c) With READ statements Column by Column INTEGER, DIMENSION (3,5) :: a INTEGER :: i, j DO j=1,5 READ (*,*) (a(i,j),i=1,3) END DO
Initialization • Implied Nested Do READ (*,*) ((a(i,j), j=1,5), i=1,3)
Rank 2 operations • Rank 2 operations occur column major order i.e. column wise • During initialization (data storage in the array elements)
Array Subsets • Let • A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 10 20
Array Subsets • A (:, 1) = A(2, : ) = 1 6 11 16 6 7 8 9 10
Examples 1) Multiply two matrices
Multiply two Matrices a11 a12 a13 .. a1n a21 a22 a23 .. A2n a31 a32 a33 .. a3n b11 b12 b21 b22 b31 b32 . . . . bn1 bn2
Write (*,*) 'Enter the elements of the matrix1 row by row' do i=1,row1 read (*,*) mat1(i,1:col1) end do Write (*,*) 'Enter the elements of the matrix2 row by row' do i=1,row2 read (*,*) mat2(i,1:col2) end do do i=1,row1 do j=1,col2 prod(i,j)=0 do k=1,row2 prod(i,j)= prod(i,j)+ mat1(i,k)* mat2(k,j) end do end do end do
Rank-n Fortran Supports n-Ranked matrices upto 7 subscripts Rank – 1 : Row/Column Rank - 2 : Table Rank – 3 : Many tables Eg. A(6,4,2) No. of elements = 6*4*2 = 48
Rank 3 Memory storage : A(1,1,1) ; A(2,1,1)….A(6,1,1) A (1,2,1); A(2,2,1)….A(6,2,1) ..A(1,4,1); A(2,4,1)….A(6,4,1) A(1,1,2); A(2,1,2)…A(6,1,2) A(1,2,2);……………..A (6,2,2) A(1,4,2)………………A(6,4,2)