This document provides an overview of arrays in Fortran, including:
1) How to declare arrays with dimensions, including compile-time and run-time arrays. Allocatable arrays allow memory to be allocated dynamically.
2) How to initialize arrays with constants or input values. Array expressions and sections allow accessing parts of arrays.
3) The WHERE construct and intrinsic functions for processing arrays element-wise. Arrays can be passed to and returned from subprograms.
4) Two-dimensional arrays represent matrices with row and column indices. Basic operations on 2D arrays are similar to 1D arrays.
3. Ex:
To read 6 number from the keyboard.
Real, dimension(6):: example
integer:: j
do j =1,6
print*, ‘Enter a number’
read*, example(j)
end do
Or print*, ‘please enter 6 numbers’
read*, example
4. Compile Time & Run Time Arrays
The memory for grades is allocated at
compile time.
This means that the size of such compile-time
arrays is fixed before execution begins.
If the data to be stored in the array is too
large, it cannot be stored in the array and
processed correctly.
To deal with this problem, we use run-time
arrays for which memory is allocated during
execution.
6. Declaration of Allocatable Arrays
Type, dimension, Allocatable:: Array_name
the actual bounds of an allocatable array can
be specified in an Allocate statement.
Form
Allocate(array_name)
Allocate(array_name,Stat= StatusVariable)
7. Ex:
Print*, ‘Enter size of arrays A and B:’
read*, N
Allocate(A(N),B(0:N+1), stat=allostatus)
if(allostatus /= 0) stop “not enough memory”
If memory is available, A can store N values
and be can store N+2 values
Once memory has been allocated, these
arrays may be used in the same ways as
compile-time arrays.
8. Deallocate
Memory that is not needed anymore
should be cleared so that we can use it
for allocating other arrays.
Form deallocate(grades)
or
deallocate(grades,stat=StatusVar)
Please read the example in page 530.
9. Array Constants
An array constant may be constructed
as a list of values enclosed between (/
and /)
(/ Value1, Value2, … , Value n /)
Ex: integer:: A(5)
A= (/ 2,4,6,8,10/)
A= ( /(2*I, I = 1,5)/ )
10. Array Expressions
Operators and functions normally
applied to simple expressions may also
be applied to arrays having the same
number of elements.
Operations applied to an array are
carried out element wise.
12.
A=2*C
or Do I = 1,4
A(I)= 2 * C(I)
End do
C = 0 assigns zeros to all elements of
C
13. Array Sections and Subarrays
In some cases, it is desired to have access to
only a part of the array. We can either use Do
loops or subarrays.
Form array_name(lower:upper:stride)
integer:: A(10)
A=(/11,22,33,44,55,66,77,88,99,110/)
print*, A(2:10:2)
prints A(2), A(4), A(6), A(8), A(10)
14. The WHERE Construct
May be used to assign values to arrays
depending on the value of a logical array
expression.
Form where (logical_array_expr)
array_var1= array_expr1
Elsewhere
array_var2= array_expr2
End where
16.
Same as
Do I=1,5
if (A(I) > 0) then
B(I) = 1.0/ Real(A(I))
else
B(I) = -1.0
end if
end do
17. Intrinsic Array-Processing Subprograms
Fortran 90 provides several intrinsic functions
whose arguments are arrays, some of the
useful ones are:
Dot_product(A,B)
Maxval(A)
Maxloc(A)
Minval(A)
Minloc(A)
refer to page 548 and Appendix D.
20. 2-Dimensional Arrays
2-D arrays have two indices to indicate rows
and columns.(matrix)
The first index corresponds to rows, the
second to columns.
Real:: Matrix(5,3)
declares a matrix with 5 rows and 3
columns.
Integer, dimension(22:28,1:24):: hours
declares a matrix of 7 rows and 24 columns