typestar

Array constructors in Fortran

Arrays built in brackets, sized by their type, walked by an implied-do.

program array_constructors
    implicit none
    ! An array constructor in brackets; size is part of the type.
    real :: tides(5) = [2.1, 3.4, 4.8, 3.9, 2.6]
    integer :: berths(4) = [(7 * 2), 3, 5, 8]
    integer :: i

    print *, "readings held: ", size(tides)
    print *, "first and last: ", tides(1), tides(5)
    ! An implied-do builds a sequence inline.
    print *, [(i * 10, i = 1, 4)]
    print *, "berths: ", berths
end program array_constructors

How it works

  1. real :: tides(5) = [...] declares and fills in one line.
  2. size(tides) asks the array itself; expressions like (7 * 2) are fine inside a constructor.
  3. [(i * 10, i = 1, 4)] is an implied-do: a sequence built inline.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
422
Tokens
95
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 88 seconds.

Type this snippet

Step 1 of 3 in Arrays, step 4 of 27 in Language basics.

← Previous Next →