Arrays
Array Reduce
Count With a Mask
Whole-array comparisons return a logical mask. count returns how many positions are .true..
Program
Play the program to count scores at or above 80.
array_reduce.f90
Replay: real traced execution (multi-file project)
program array_reduce
implicit none
integer :: scores(5)
integer :: above
scores = [65, 82, 91, 74, 88]
above = count(scores >= 80)
print '(I0)', above
end program array_reduce
scores ← [65, 82, 91, 74, 88]
4integer :: above5scores = [65, 82, 91, 74, 88]6above = count(scores >= 80)values this step[65, 82, 91, 74, 88]scoresabove ← 3
5scores = [65, 82, 91, 74, 88]6above = count(scores >= 80)7print '(I0)', abovevalues this step3above[F, T, T, F, T]scores >= 80print '(I0)', above
6 above = count(scores >= 80)7 print '(I0)', above8end program array_reduceoutput3values this step3above
Count Passing Scores
scoresstarts as[65, 82, 91, 74, 88].scores >= 80checks every score.- Scores at least
80become.true.in the mask. count(...)returns the number of.true.entries:3. | Score |>= 80? | | --- | --- | |65|.false.| |82|.true.| |91|.true.| |74|.false.| |88|.true.|
logical mask
`scores >= 80` produces a logical array.
count
`count(mask)` returns the number of `.true.` positions.
whole array op
Comparisons apply to every element at once.
Exercise: array_reduce.f90
Compare an array of scores to a threshold, count the true mask entries, and print the count