1 / 17

Understanding Two-Dimensional Arrays in Programming: Definition, Initialization, and Operations

This guide explores two-dimensional arrays (rank-2 arrays) in programming, focusing on integer dimensions such as (3,5). It outlines the declaration process, initialization methods (element-wise, array constructor, read statements), and operations, specifically emphasizing column-major order for data storage. Additionally, it includes examples of matrix multiplication and n-ranked matrices up to rank 7, providing a comprehensive overview of how to work with these structures in various programming contexts.

huslu
Télécharger la présentation

Understanding Two-Dimensional Arrays in Programming: Definition, Initialization, and Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 1 a(2,4) Row 2 Row 3 Col 1 Col 2 Col 3 Col 4 Col 5

  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 • 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

  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 a11 a12 a13 .. a1n a21 a22 a23 .. A2n a31 a32 a33 .. a3n b11 b12 b21 b22 b31 b32 . . . . bn1 bn2

  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 end do 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)

More Related