temp_stats.f90 in Fortran
A week of daily highs reduced to low, high and mean, with a scaled bar per day.
! One week of daily highs: spread, mean, and a scaled bar chart.
program temp_stats
implicit none
character(len=3) :: days(7) = ["Mon", "Tue", "Wed", "Thu", &
"Fri", "Sat", "Sun"]
integer :: temps(7) = [61, 68, 74, 79, 72, 65, 58]
integer :: lo, hi, span, width, i
real :: mean
lo = minval(temps)
hi = maxval(temps)
span = max(hi - lo, 1)
mean = sum(temps) / 7.0
print '(a)', "day temp range diff"
print '(a)', "------------------------------------"
! Bars scale to the week's spread; the +1 keeps Sunday visible.
do i = 1, 7
width = 20 * (temps(i) - lo) / span + 1
print '(a, 1x, i3, 1x, a, t27, f6.1)', days(i), temps(i), &
repeat("#", width), temps(i) - mean
end do
print '(a)', "------------------------------------"
print '(a, i0, a, i0, a, f5.1)', "low ", lo, ", high ", hi, &
", mean ", mean
print '(a, i0)', "days above the mean: ", count(temps > mean)
end program temp_stats
How it works
minval,maxval, andsumset the spread and the mean without a loop.- Each bar scales to the week's spread;
repeatdraws it,t27aligns the diff column. count(temps > mean)closes the report with the warm-day tally.
Keywords and builtins used here
charactercountdoendimplicitintegerlenmaxmaxvalminvalnoneprintprogramrealrepeatsum
The run, in numbers
- Lines
- 27
- Characters to type
- 901
- Tokens
- 192
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 177 seconds.
Step 3 of 3 in Encore, step 27 of 27 in Language basics.