Today you assemble everything into one program: a reusable record collection and a small text analyzer that reads study sessions from stdin and prints a grouped report. Before reading any of its code, read its brief. A brief is the contract for a whole program: what input is valid, what is stored, what is printed, and what happens on each kind of failure. Every test you will run later is one sentence of this brief turned into a check, and every design choice in the code has a reason here.
The input format
Each line holds one record:
- Optional leading whitespace (space, tab,
'\r','\n'). - A topic of 1 to 23 lowercase ASCII letters.
Readingandread-ingare invalid. - At least one whitespace character.
- A duration in minutes, 1 to 180, written as decimal digits. Leading zeros are allowed, so
001is 1. Signs are not. - Optional trailing whitespace, and nothing else.
Why so strict? Each rule removes an ambiguity. Case-sensitive lowercase means Coding and coding can never be two spellings of one topic. A 23-letter limit fits char topic[24] with its terminator, and rejecting longer topics instead of truncating keeps distinct topics distinct. The 1 to 180 range catches typos such as reading 2500 and keeps totals small.
Storage and limits
Accepted records are stored in insertion order in a growing array, at most 1024 of them. Further valid records are rejected as FULL. The limit bounds memory use and the totals: 1024 times 180 is 184320, which fits in long on every platform but not in a minimal int. Storage keeps insertion order so that a report never changes what later operations, such as removing the first record, refer to.
The report
Topics appear in alphabetical order, each with its session count and total, then three summary lines. The sample input reading 25, coding 40, bad, reading 10, coding 0 produces:
coding: 1 sessions, 40 minutes
reading: 2 sessions, 35 minutes
Accepted: 3
Total: 75 minutes
Rejected: 2Empty input prints No records, then Accepted: 0, Total: 0 minutes, Rejected: 0. The wording 1 sessions is fixed, so tests can compare exact text.
Failure behavior
- A malformed line, a blank line, an overlong line (more than 94 characters) or a valid record beyond 1024 is rejected: counted once, then reading continues with the next line.
- An allocation failure while storing is fatal: print
Storage unavailableto stderr, release everything and exit with status 1. Continuing would produce a report that silently omits records. - If the report's temporary copy cannot be allocated, print nothing on stdout, report the error on stderr and exit with 1. Half a report is worse than none.
- A read error on stdin or a write error on stdout also ends with exit status 1.
No network, no files and no prompts are needed: input arrives on stdin and the report goes to stdout.
Boundary cases the tests must cover
A brief is only useful if it is checked. These cases come straight from the rules above, and the project's checks run every one: empty input; input with no final newline; \r\n line endings; 001 and 180 accepted, 0, 181, +25 and -1 rejected; 25x and 25 extra rejected; a 23-letter topic accepted and a 24-letter one rejected; a 120-character line rejected once with the next line still read; exactly 1024 records accepted and the 1025th rejected.
Practice
Your turn
Write void print_summary(const int minutes[], size_t accepted, size_t rejected) that prints the three summary lines in the brief's format, preceded by No records when nothing was accepted. Add the minutes in a long. The starter must print Accepted: 3, Total: 75 minutes, Rejected: 2, then No records, Accepted: 0, Total: 0 minutes, Rejected: 0.
#include <stdio.h>
#include <stddef.h>
/* Write print_summary here. */
int main(void) {
int minutes[3] = {25, 40, 10};
print_summary(minutes, 3, 2);
print_summary(minutes, 0, 0);
return 0;
}Hint 1: the idea
The total depends on the stored minutes, not on a number typed into the code. A hard-coded 75 would pass the first call and fail the moment the input changed.
Hint 2: a first step
Call your function from main with the accepted count only, printing just the Accepted: line, and run both calls. Then add the total, computed from the array rather than typed in, and decide where the No records line goes.
Show one solution
#include <stdio.h>
#include <stddef.h>
void print_summary(const int minutes[], size_t accepted, size_t rejected) {
if (accepted == 0) { printf("No records\n"); }
long total = 0;
for (size_t i = 0; i < accepted; ++i) { total += minutes[i]; }
printf("Accepted: %zu\nTotal: %ld minutes\nRejected: %zu\n", accepted, total, rejected);
}
int main(void) {
int minutes[3] = {25, 40, 10};
print_summary(minutes, 3, 2);
print_summary(minutes, 0, 0);
return 0;
}Accepted: 3
Total: 75 minutes
Rejected: 2
No records
Accepted: 0
Total: 0 minutes
Rejected: 0The empty case needs no special arithmetic: the loop runs zero times and the total is 0. The format specifiers match the types exactly, %zu for size_t and %ld for long; a mismatch such as %d for a size_t is undefined behavior that -Wall warns about. In the project, the grouped lines come from analyzer_report and these three from main. Next, you will read how those parts connect.
Completion is saved in this browser only. It does not measure mastery.