Arrays in Fortran

There are several ways to define a Fortran array but the most important thing to remember is that by default index starts from 1.
INTEGER, PARAMETER :: M = 50, N = 2000
INTEGER            :: idx(M)
REAL               :: vector(0:N-1)
REAL               :: matrix(M,N)
CHARACTER(LEN=40)  :: screen(200)
or
INTEGER, PARAMETER                :: M = 50, N = 2000
INTEGER, DIMENSION(M)             :: idx
REAL, DIMENSION(0:N-1)            :: vector
REAL, DIMENSION(M,N)              :: matrix
CHARACTER(LEN=40), DIMENSION(200) :: screen
Arrays in Fortran
Fortran arrays are very powerful and allows to define:

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.
 vector(:) = vector(:) + 1.0
 
 do j=1,n
    y(:) = y(:) + m(:,J) * x(j)
 enddo
The array syntax allows for less explicit do loops. With the "old Fortran" syntax:
integer, parameter :: N=4, M=3
real               :: A(M,N), x(N),y(M)
integer            :: i,j 

y(:) = 0.0

OUTER_LOOP: do j=1,N
  INNER_LOOP: do i=1,M
	y(i) = y(i) + A(i,j) * x(j)
   enddo INNER_LOOP
enddo OUTER_LOOP
while with the array syntax:
integer, parameter :: N=4, M=3
real               :: A(M,N), x(N),y(M)
integer            :: j 

y(:) = 0.0

OUTER_LOOP: do j=1,N
	y(:) = y(:) + A(:,j) * x(j)
enddo OUTER_LOOP
Do not forget to initialize your arrays!
do j=1,10
  vector(j) = 0.0
  idx(j) = j
enddo
or using an array syntax initialization:
vector = 0
vector(:) = 0
idx(1:10)= 0
idx(0:) = (/ (j, j=0,10)/)

Array sections

With Fortran array syntax you can access a portion of an array in a very intuitive way: array sections.
vector(3:8) = 0
everyThird(1:N:3)= 10
diagBlock(i-1:i+1,j-2:j+2) = k
Sections enable you to refer to a sub-block of a matrix or a sub-cube of a 3D array, etc:
REAL(kind=8) :: A( 1000,1000)
INTEGER      :: p3D(256,256,256)

A(2:500,3:300:3) = 4.0
p3D(128:150,56:80,1:256:8) = 4837
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: If an array size depends on the input of your program, its memory should be allocated at runtime: Fortran provides two ways to allocate memory dynamically for arrays:
integer, allocatable :: M(:,:)
integer              :: err

allocate(M(4,5), STAT=err)
if (err /= 0) STOP
...
deallocate(M)
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:
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.