There are several ways to define a Fortran array but the most important thing to remember is that by default index starts from 1.
Fortran arrays are very powerful and allows to define:
matrices
vectors
other arrays with up to 7 dimensions.
Arrays Syntax & arrays sections
Arrays syntax
In older Fortran codes, arrays are usually accessed element by element while in modern Fortran, what is called the Fortran 90 array syntax is used.
However, you have to be very careful when copying array sections. Both left and right hand sides of the assignment statement has to have conforming dimensions:
LMS(1:3,0:9) = RHS(-2:0,20:29) ! This is OK
LMS(1:2,0:9) = RHS(-2:0,20:29) ! This is wrong!!!
Dynamic memory allocation
So far in our examples the array dimensions have been defined at compile time:
memory allocation is static
If an array size depends on the input of your program, its memory should be allocated at runtime:
memory allocation becomes dynamic
Fortran provides two ways to allocate memory dynamically for arrays:
Array variable has an ALLOCATABLE (or POINTER) attribute, and memory is allocated through the ALLOCATE statement, and freed through DEALLOCATE
a variable, which is declared in the procedure with size information coming from the argument list or form a module, is an
automatic array - no ALLOCATE is needed, neither DEALLOCATE
If you allocate a variable, you must not forget to deallocate it.
Array intrinsic functions
Built-in functions can apply various operations on the whole array, not just array elements.
As a result either another array or just a scalar value is returned .
A subset selection through masking is also possible:
Masking and use of array (intrinsic) functions is often accompanied with the use of FORALL and WHERE array statements.
SIZE(array [,dim]) returns the number of elements in the arrays, optionally along the specified dimension
SHAPE(array) returns an INTEGER vector containing SIZE of array with respect to each of its dimension
COUNT(L_array [,dim]) returns the count of elements which are .TRUE. in the LOGICAL L_array
SUM(array [,dim][,mask]) sum of elements, optionally along a dimension, and optionally under mask
ANY(L_array [,dim]) returns a scala value of .TRUE. if any value in LOGICAL L_array is found to be .TRUE.
MINVAL/MAXVAL(array[,dim][,mask]) return the minimum/maximum value in a given array [along specified dimension]
[,under mask]
MINLOC/MAXLOC(array [,mask]) returns a vector of location(s) [, under mask],
where the minimum/maximum value(s) is/are found
Summary
Arrays make Fortran a very powerful language, especially for computationally intensive program development.
Using its array syntax, vectors and matrices can be initialized and used in a very intuitive way.
Dynamic memory allocation enables sizing your arrays according to particular needs.
Array intrinsic functions further simplify coding effort and improve code readability.