Welcome to AE Resources
Converted document

2D Arrays

2 Dimensional arrays are quite similar to 1D arrays. You still must declair the type, the size and the names. The difference is, of course, in the size. 2D arrays have a second part of the DIMENSION statement which gives them a size in the vertical direction. The result looks like...
type, DIMENSION(number_rows, number_columns) :: names

Indexing

Indexing a 2D array is again very similar to 2D indexing. The format for indexing a 2D array is array_name(row_pos, column_pos). Indexes can be used to get the number at a point in the array out as well as to assign values to a position. To reference a series of positions use a colon : between the sets of numbers you are referencing
PROGRAM array_ind
   INTEGER, PARAMETER :: upper=5, lower=1
   INTEGER, DIMENSION(lower:upper,lower:upper) :: A
   INTEGER :: ind
   A(1:5,1)=(/1,2,3,4,5/)
   A(1:5,2)=(/6,7,8,9,10/)
   A(1,3:5)=22
   A(2:5,3:5)=44
   ind= A(1,5)
END PROGRAM array_ind
This would produce an array that looked like
1 6 22 22 22
2 7 44 44 44
3 8 44 44 44
4 9 44 44 44
5 10 44 44 44
and return the value ind=22 by looking in row 1 column 5.

Reading into 2D Arrays/ Implied Do

Reading into a 2D array is very similar to reading into a 1D array. The first way is to of course assign each position individually through the use of the READ. This method can be done several ways using a DO or if you already have the numbers to inputed into each column as vectors already you can index entire columns. The following is a program example of indexing entire columns, followed by an example of using a DO statement.
PROGRAM array_test
   INTEGER :: I
   INTEGER, PARAMETER :: upper=5, lower=1
   INTEGER, DIMENSION(lower:upper) :: A
   INTEGER, DIMENSION(lower:upper, 1:2) :: B
   READ(*,*) (A(I), I=lower,upper)
   B(1:5,1)=A !This is were an index is used to read in an entire column
   B(1:5,2)=(/6,7,8,9,10/)
END PROGRAM array_test
The above example shows using one 1D array to help construct a 2D array. Note: 1D arrays are represented as columns of numbers. It is also important to note how the positions are counted and walked through. In a 3x3 matrix called ’a’ the second position is at the second row first column. Although you can not actually index using one number in a 3x3 matrix, this is important when formating as the WRITE function will walk through in a vertical manner.
PROGRAM array_ex
   INTEGER :: I, J
   INTEGER, PARAMETER :: upper=5, lower=1
   INTEGER, DIMENSION(lower:upper, lower:upper) :: A
   DO I= lower,upper
      DO J= lower,upper
         READ(*,*) A(I,J) !A(row, column)
      END DO
   END DO
Finally the last way of creating arrays this page will discuss is using an Implied DO statement to read in the values of an array
INTEGER :: I,J
INTEGER, PARAMETER :: upper=5, lower=1
INTEGER, DIMENSION(lower:upper, lower:upper) :: A
READ(*,*) ((A(I,J), J=lower,upper), I=lower,upper) 
!Order read into below
The above code reads the data in all in one statement and orders it filling the 1st row first then going on to the next row and so on.
Next Page →
Next Page →
← Previous Page
← Previous Page