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
sum(hauls)andmaxval(hauls)reduce the whole array in one call.- The average is
sumoversize— no accumulator variable anywhere. maxlocanswers where the best haul sits, not just what it weighed.
Keywords and builtins used here
endimplicitmaxlocmaxvalminvalnoneprintprogramrealsum
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.
Step 3 of 3 in Arrays, step 6 of 27 in Language basics.