Capstone 3: a sorted, grouped report

A grouped report needs its input sorted and nothing else. Once the records are in date order, the months are already together, and a single walk over the array can start a new group when the month changes, add up as it goes, and print a subtotal at the boundary. This lesson writes that walk, aligns the columns with one format string, and closes the third capstone with the totals and the count that prove the file was read completely.

Grouping a sorted array

The report keeps one variable, the month of the previous record, and compares it with the month of the current one. When they differ, the previous group is finished: the subtotal is printed, the accumulator is set back to zero and the new month starts a row with its label. That is the whole algorithm, and it needs the sort of the previous lesson to be correct: an unsorted array would start a new group at every change of month and print the same month three times.

The program checks that condition instead of hoping for it: if (.not. is_sorted(recs, n)) error stop before the report runs. A report on unsorted data is wrong in a way that looks plausible, which is the worst kind of wrong, so the guard is part of the report and not a comment.

The report

WRITE_REPORT prints a title, a header, one row per record, a subtotal at every month boundary, a grand total and a count. Three details make it readable. The month is printed only on the first row of its group and the column is blank afterwards, so the eye finds the group rather than reading the same string four times. A blank line separates the groups. And the last line repeats the count and the total in one sentence, so a reader who photographs the end of the output has both.

The subtotal line is built in a character variable first, write (label, '(a,a)') ' subtotal for ', previous, and printed with an A31 descriptor in front of the amount. Building the label with an internal write is what lets the text and the number share one format, which is the same trick as lesson 048 for a string that changes length.

The columns

Every row uses (a7,2x,a10,2x,a8,2x,f10.2): seven for the month, ten for the date, eight for the region and ten for the amount, with two spaces between the fields. The header uses the same widths in a single character constant, which is why amount sits over the last column and not over the gap before it. The subtotal and grand total lines use (a31,f10.2), which puts the number in the same ten columns because thirty-one characters precede it in both formats.

The amount is F10.2 rather than a scientific descriptor, because a report of sales is read in units and cents and not in mantissas. Lesson 128 made the opposite choice for a residual, which can be a millionth, and the two pages together are the rule: pick the descriptor from the range of the column, not from habit.

The run

The file report.f90 is the module of lessons 132 and 133 with the report added, and a program that loads the sample, sorts it, checks the order, prints the report and then lists the lines it skipped:

module records use, intrinsic :: iso_fortran_env, only: dp => real64 implicit none private public :: record, load, write_sample, before, sort_records, is_sorted, write_report integer, parameter :: date_len = 10, region_len = 8 type :: record character(len=date_len) :: date = ' ' character(len=region_len) :: region = ' ' real(dp) :: amount = 0.0_dp end type record contains subroutine write_sample(path) character(len=*), intent(in) :: path integer :: u open (newunit=u, file=path, status='replace', action='write') write (u, '(a)') '2026-03-02,east,3050.00' write (u, '(a)') '2026-01-11,south,980.00' write (u, '(a)') '2026-02-16,north,1890.00' write (u, '(a)') '2026-01-04,north,1200.50' write (u, '(a)') '2026-03-16,south,1425.60' write (u, '(a)') '2026-02-09,east,540.75' write (u, '(a)') '2026-01-25,north,760.00' write (u, '(a)') '2026-02-16,east,410.00' write (u, '(a)') '2026-01-18,east,1430.25' write (u, '(a)') '2026-03-09,north,675.25' write (u, '(a)') '2026-02-23,south,1120.40' write (u, '(a)') '2026-03-23,east,880.00' write (u, '(a)') '# a comment line the batch skips' write (u, '(a)') '2026-03-30,west,oops' close (u) end subroutine write_sample subroutine load(path, recs, n, skipped, n_skipped) character(len=*), intent(in) :: path type(record), allocatable, intent(out) :: recs(:) integer, intent(out) :: n character(len=80), allocatable, intent(out) :: skipped(:) integer, intent(out) :: n_skipped integer :: u, ios, line_no character(len=120) :: line type(record) :: r allocate (recs(8), skipped(8)) n = 0 n_skipped = 0 open (newunit=u, file=path, status='old', action='read', iostat=ios) if (ios /= 0) error stop 'load: cannot open the file' line_no = 0 do read (u, '(a)', iostat=ios) line if (ios /= 0) exit line_no = line_no + 1 read (line, *, iostat=ios) r%date, r%region, r%amount if (ios == 0 .and. len_trim(r%date) == date_len .and. r%amount >= 0.0_dp) then call append_record(recs, n, r) else call append_line(skipped, n_skipped, line) end if end do close (u) print '(a,a,a,i0,a,i0,a,i0,a)', 'read ', path, ': ', n, ' records, ', n_skipped, ' lines skipped from ', line_no, ' lines' end subroutine load pure function before(a, b) result(earlier) type(record), intent(in) :: a, b logical :: earlier if (a%date /= b%date) then earlier = a%date < b%date else earlier = a%region < b%region end if end function before subroutine sort_records(list, n) type(record), intent(inout) :: list(:) integer, intent(in) :: n integer :: i, j type(record) :: key do i = 2, n key = list(i) j = i - 1 do while (j >= 1) if (.not. before(key, list(j))) exit list(j + 1) = list(j) j = j - 1 end do list(j + 1) = key end do end subroutine sort_records function is_sorted(list, n) result(ok) type(record), intent(in) :: list(:) integer, intent(in) :: n logical :: ok integer :: i ok = .true. do i = 2, n if (before(list(i), list(i - 1))) ok = .false. end do end function is_sorted pure function month_of(r) result(m) type(record), intent(in) :: r character(len=7) :: m m = r%date(1:7) end function month_of subroutine write_report(list, n) type(record), intent(in) :: list(:) integer, intent(in) :: n integer :: i real(dp) :: subtotal, grand, amount character(len=7) :: month, previous, month_col character(len=31) :: label print '(a,i0,a)', 'Sales report, ', n, ' records' print * print '(a7,2x,a10,2x,a8,2x,a10)', 'month', 'date', 'region', 'amount' grand = 0.0_dp subtotal = 0.0_dp previous = ' ' do i = 1, n month = month_of(list(i)) amount = list(i)%amount if (i > 1 .and. month /= previous) then write (label, '(a,a)') ' subtotal for ', previous print '(a31,f10.2)', label, subtotal print * subtotal = 0.0_dp month_col = month else if (month /= previous) then month_col = month else month_col = ' ' end if print '(a7,2x,a10,2x,a8,2x,f10.2)', month_col, list(i)%date, list(i)%region, amount subtotal = subtotal + amount grand = grand + amount previous = month end do write (label, '(a,a)') ' subtotal for ', previous print '(a31,f10.2)', label, subtotal print * print '(a31,f10.2)', 'grand total', grand print '(a,i0,a,f10.2)', '# ', n, ' records, total ', grand end subroutine write_report subroutine append_record(list, n, r) type(record), allocatable, intent(inout) :: list(:) integer, intent(inout) :: n type(record), intent(in) :: r type(record), allocatable :: bigger(:) if (n == size(list)) then allocate (bigger(max(8, 2 * n))) bigger(1:n) = list(1:n) call move_alloc(bigger, list) end if n = n + 1 list(n) = r end subroutine append_record subroutine append_line(list, n, line) character(len=*), allocatable, intent(inout) :: list(:) integer, intent(inout) :: n character(len=*), intent(in) :: line character(len=80), allocatable :: bigger(:) if (n == size(list)) then allocate (bigger(max(8, 2 * n))) bigger(1:n) = list(1:n) call move_alloc(bigger, list) end if n = n + 1 list(n) = line end subroutine append_line end module records program report_demo use records implicit none type(record), allocatable :: recs(:) character(len=80), allocatable :: skipped(:) integer :: n, n_skipped, i call write_sample('sales.csv') call load('sales.csv', recs, n, skipped, n_skipped) call sort_records(recs, n) if (.not. is_sorted(recs, n)) error stop 'report: the records are not sorted' print * call write_report(recs, n) print * print '(a)', 'skipped lines:' do i = 1, n_skipped print '(a,a)', ' ', trim(skipped(i)) end do end program report_demo

gfortran -Wall -Wextra -std=f2018 report.f90 -o report
./report

read sales.csv: 12 records, 2 lines skipped from 14 lines

Sales report, 12 records

  month        date    region      amount
2026-01  2026-01-04  north        1200.50
         2026-01-11  south         980.00
         2026-01-18  east         1430.25
         2026-01-25  north         760.00
   subtotal for 2026-01           4370.75

2026-02  2026-02-09  east          540.75
         2026-02-16  east          410.00
         2026-02-16  north        1890.00
         2026-02-23  south        1120.40
   subtotal for 2026-02           3961.15

2026-03  2026-03-02  east         3050.00
         2026-03-09  north         675.25
         2026-03-16  south        1425.60
         2026-03-23  east          880.00
   subtotal for 2026-03           6030.85

                    grand total  14362.75
# 12 records, total   14362.75

skipped lines:
  # a comment line the batch skips
  2026-03-30,west,oops

The totals

Three subtotals and one grand total, and the arithmetic is checkable by hand: 4370.75 + 3961.15 + 6030.85 = 14362.75, which is the line the program prints twice, once as the grand total and once in the last sentence. The count is twelve, which is the number of records the load reported, and the two skipped lines are still listed at the end so that the batch does not hide what it could not read.

The second capstone would have checked these numbers with a test. A batch report can be checked the same way: record the output of a known file, compare it in the build, and treat a difference as a failure. That is the golden-file idea, and for a report whose format is part of its contract it is the only test that covers the formatting as well as the sums.

The end of the course

Three capstones, each a small program of the kind a numerical team writes on its first day: a root finder with a model, two methods and a convergence report; a matrix library with a type, a factorisation, a solver and a suite with tolerances; and a record batch that reads a file it did not write, sorts it, and prints a grouped report with the lines it skipped. Between them they use the modules, the derived types, the operators, the arrays, the I/O and the floating point of the whole course, and none of them is longer than four hundred lines.

What comes after the course is the same three shapes with real data and a real build around them: a test target in the Makefile, the checked build of lesson 114 in front of the release build, and a repository whose history says why each number changed.

Exercises

  1. Add a total per region inside each month. Which variable does the report need, and where is it reset?
  2. Print the report to a file instead of the standard output, with the same format. Which line of the program changes?
  3. Add a percentage column: each row as a share of its month. Why does the report have to compute the subtotal before it can print the rows?
  4. Make the report print the skipped lines first and a warning when there are any. Which part of the output should a batch put on the error stream instead?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does grouping a sorted array need?

    Answer

    One variable with the previous key and a comparison at every record

    A change of month ends a group.

  2. What happens if the report runs on an unsorted array?

    Answer

    The same month starts a new group several times and the subtotals are wrong

    Which is why the program checks is_sorted first.

  3. Why is the month printed only on the first row of a group?

    Answer

    The eye finds the group instead of reading the same string four times

    The column is blank afterwards.

  4. How is the subtotal label built?

    Answer

    With an internal write into a character variable

    It shares one format with the number.

  5. Which format do the rows use?

    Answer

    (a7,2x,a10,2x,a8,2x,f10.2)

    The header uses the same widths.

  6. Why F10.2 for the amount?

    Answer

    A sales report is read in units and cents

    A residual would use ES instead.

  7. What is the grand total?

    Answer

    14362.75

    The sum of the three subtotals.

  8. How many records were reported?

    Answer

    12, the number the load found

    Two lines were skipped and listed.

  9. Why check is_sorted before the report?

    Answer

    A wrong report looks plausible, so the guard is part of the report

    ERROR STOP beats a wrong total.

  10. What covers the formatting as well as the sums of a report?

    Answer

    A recorded output compared in the build

    The golden-file idea.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev