typestar

Array intrinsics in Fortran

sum, maxval, and friends replace the counting loop entirely.

program array_intrinsics
    implicit none
    real :: hauls(6) = [12.5, 8.0, 15.25, 6.75, 11.0, 9.5]

    ! Whole-array intrinsics replace the counting loop.
    print *, "total catch: ", sum(hauls)
    print *, "best haul:   ", maxval(hauls)
    print *, "worst haul:  ", minval(hauls)
    print *, "average:     ", sum(hauls) / size(hauls)
    print *, "which best:  ", maxloc(hauls)
end program array_intrinsics

How it works

  1. sum(hauls) and maxval(hauls) reduce the whole array in one call.
  2. The average is sum over size — no accumulator variable anywhere.
  3. maxloc answers where the best haul sits, not just what it weighed.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
383
Tokens
78
Three-star pace
70 tpm

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

Type this snippet

Step 3 of 3 in Arrays, step 6 of 27 in Language basics.

← Previous Next →