Académique Documents
Professionnel Documents
Culture Documents
Array
An array is a collection of data of the same type.
Array elements are indexed or subscripted, just like
are INTEGERs.
Each dimension has its own extent.
An extent of a dimension is the range of its index. If
m: is omitted, the default is 1.
-3:2 means possible indices are -3, -2 , -1, 0, 1, 2
5:8 means possible indices are 5,6,7,8
7 means possible indices are 1,2,3,4,5,6,7
type declaration.
Values in the DIMENSION attribute are usually PARAMETERs
to make program modifications easier.
INTEGER, PARAMETER :: SIZE=5, LOWER=3, UPPER = 5
INTEGER, PARAMETER :: SMALL = 10, LARGE = 15
REAL, DIMENSION(1:SIZE) :: x
INTEGER, DIMENSION(LOWER:UPPER,SMALL:LARGE) :: a,b
LOGICAL, DIMENSION(2,2) :: Truth_Table
Use of Arrays
Fortran has, in general, three different ways to use
Use of Arrays
Suppose we have the following declarations
INTEGER, PARAMETER :: L_BOUND = 3, U_BOUND = 10
INTEGER, DIMENSION(L_BOUND:U_BOUND) :: x
Use of Arrays
Suppose we have the following declarations:
INTEGER, PARAMETER :: L_BOUND = 3, U_BOUND = 10
INTEGER, DIMENSION(L_BOUND:U_BOUND, &
L_BOUND:U_BOUND) :: a
Initialization of Arrays
Array can be initialized by defining every element :
Initialization of Arrays
Initialization can be done through RESHAPE and
TRANSPOSE functions:
RESHAPE:
Integer, DIMENSION(3,3):: A
A= RESHAPE((/1,2,3,4,5,6,7,8,9/), shape(A))
Initialization of Arrays
TRANSPOSE:
Integer, DIMENSION(3,3):: A
A= TRANSPOSE (RESHAPE((/1,2,3,4,5,6,7,8,9/),
shape(A)))
The above statement will generate the following 3X3
matrix:
1
2
3
4
5
6
7
8
9
Initialization of Arrays
Initialize through an implicit do
The Implied DO
Fortran has the implied DO that can generate
The Implied DO
The execution of an implied DO below lets variable v to start with
REAL, DIMENTION(10) :: A
The above statement causes the compiler to allocate a block
of memory large enough to hold 10 real values. The size of
this block cannot be altered except by recompiling the array
with a different size.
A compile time / fixed size array suffer from two problems:
If the size of the array exceeds the number of values to be stored
Example
REAL, DIMENSION(:), ALLOCATABLE :: A, B
PRINT*, Enter the size of the array A and B
READ*, N
ALLOCATE (A(N), B(0:N+1), STAT = AllocateStatus)
IF (AllocateStatus | = 0) then
PRINT*, Not enough memory
Else
other statements
-