TExcelTAD
Home › Fortran › Fortran Arrays

Fortran Arrays

Arrays are the heart of Fortran. An array is a collection of values of the same type, stored next to each other in memory and accessed by an index. Fortran arrays are a first-class part of the language: you can declare them in many shapes, assign whole arrays at once and pass them to built-in functions.

Declaring arrays

Give the size in parentheses after the name, or use the dimension attribute:

fortran · try it yourself
program declare_arrays
  implicit none
  integer :: counts(5)                    ! 5 integers
  real, dimension(3) :: position          ! 3 reals
  character(len=6) :: names(2)            ! 2 strings of length 6

  counts = 0                              ! set every element
  position = [1.0, -2.5, 4.0]             ! array constructor
  names(1) = 'alpha'
  names(2) = 'beta'

  print '(5i3)', counts
  print '(3f6.2)', position
  print '(a, "|", a)', names(1), names(2)
  print '(a, i0)', 'size(counts) = ', size(counts)
end program declare_arrays
  0  0  0  0  0
  1.00 -2.50  4.00
alpha |beta
size(counts) = 5

Indexing starts at 1

By default the first element has index 1, not 0, and the last has index n. Elements are accessed with parentheses: a(1), a(i), a(n).

fortran · try it yourself
program indexing
  implicit none
  integer :: primes(6) = [2, 3, 5, 7, 11, 13]
  integer :: i

  print '(a, i0)', 'first: ', primes(1)
  print '(a, i0)', 'last:  ', primes(6)
  primes(2) = 99
  do i = 1, size(primes)
    write(*, '(i4)', advance='no') primes(i)
  end do
  print *
end program indexing
first: 2
last:  13
   2  99   5   7  11  13

Custom bounds

You can choose any lower and upper bound with lower:upper. This is handy when the index has a meaning: years, temperatures, or offsets around zero.

fortran · try it yourself
program custom_bounds
  implicit none
  real :: rainfall(2020:2024) = [612.0, 580.5, 701.2, 655.0, 598.3]
  integer :: offsets(-2:2)
  integer :: year, k

  do year = lbound(rainfall, 1), ubound(rainfall, 1)
    print '(i4, a, f6.1, a)', year, ': ', rainfall(year), ' mm'
  end do

  do k = -2, 2
    offsets(k) = k * k
  end do
  print '(a, 5i3)', 'offsets: ', offsets
  print '(a, i0, a, i0)', 'bounds: ', lbound(offsets, 1), ' to ', ubound(offsets, 1)
end program custom_bounds
2020:  612.0 mm
2021:  580.5 mm
2022:  701.2 mm
2023:  655.0 mm
2024:  598.3 mm
offsets:   4  1  0  1  4
bounds: -2 to 2

lbound(a, dim) and ubound(a, dim) return the lower and upper bound of dimension dim. Loop with them instead of hard-coding the numbers, so the loop stays correct if the bounds change. When an array is printed as a whole, its elements appear in index order, from offsets(-2) to offsets(2).

Array constructors

Square brackets build an array from a list of values: [1, 2, 3]. (Older code uses (/ 1, 2, 3 /), which means the same.) Inside a constructor you can use an implied do loop (expression, i = start, end) to generate values:

fortran · try it yourself
program constructors
  implicit none
  integer :: i
  integer :: evens(5), squares(6)
  real :: grid(5)

  evens = [(2 * i, i = 1, 5)]
  squares = [(i**2, i = 0, 5)]
  grid = [(0.25 * i, i = 0, 4)]

  print '(5i4)', evens
  print '(6i4)', squares
  print '(5f6.2)', grid
  print '(8i3)', [1, 2, [3, 4, 5], evens(1:3)]   ! constructors flatten nested arrays
end program constructors
   2   4   6   8  10
   0   1   4   9  16  25
  0.00  0.25  0.50  0.75  1.00
  1  2  3  4  5  2  4  6

For strings, all items must have the same length, or you give the type explicitly: [character(len=8) :: 'Mercury', 'Venus'].

Out-of-bounds access

Fortran does not check indices by default, for speed. Reading a(7) from a 6-element array silently reads whatever memory follows it, and writing there can corrupt other variables. While developing, compile with -fcheck=bounds (or -fcheck=all) and the program stops with a clear message:

text
At line 5 of file prog.f90
Fortran runtime error: Index '7' of dimension 1 of array 'a' above upper bound of 6

Test yourself

What is the index of the first element of real :: a(10)?

  1. 0
  2. 1
  3. -1
  4. 10

Which declaration gives an array with indices -3 through 3?

  1. integer :: a(7)
  2. integer :: a(-3:3)
  3. integer :: a(-3, 3)
  4. integer :: a[-3..3]

What does [(i * 10, i = 1, 3)] produce?

  1. [1, 2, 3]
  2. [10, 20, 30]
  3. [0, 10, 20]
  4. [30]