Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two Dimensional Arrays. One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3.

Similar presentations


Presentation on theme: "Two Dimensional Arrays. One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3."— Presentation transcript:

1 Two Dimensional Arrays

2 One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3

3 Two dimension Rank 2 Array INTEGER, DIMENSION (3,5) :: a Row 2 Row 3 Row 1 Col 1Col 2Col 3Col 4Col 5 a(2,4)

4 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.

5 Initialization a) 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

6 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/) ) √

7 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

8 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

9 Initialization Implied Nested Do READ (*,*) ((a(i,j), j=1,5), i=1,3)

10 Rank 2 operations Rank 2 operations occur column major order i.e. column wise During initialization (data storage in the array elements)

11 Array Subsets Let  A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 10 20

12 Array Subsets A (:, 1) = A(2, : ) = 1 6 11 16 6 7 8 9 10

13 Examples 1) Multiply two matrices

14 Multiply two Matrices a 11 a 12 a 13.. a 1n a 21 a 22 a 23.. A 2n a 31 a 32 a 33.. a 3n b 11 b 12 b 21 b 22 b 31 b 32. b n1 b n2

15 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

16 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

17 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)


Download ppt "Two Dimensional Arrays. One dimension Rank 1 Array INTEGER, DIMENSION (3) :: a Row 1 Row 2 Row 3."

Similar presentations


Ads by Google