Following a walkthrough and building something from a blank editor are different skills, and only the second one shows what you have really learned. This brief is new, but every tool it needs is from this week, and the design is yours.
The brief
Write a function that summarizes an array of reading sessions, given as page counts, and a main that proves it works.
- A session of 1 through 100 pages is accepted; anything else is rejected.
- The summary has four numbers: how many sessions were accepted, how many were rejected, the total of the accepted pages, and the largest accepted session (0 if none).
- The function takes the array as read-only, with its count, and must not change it.
- How the function hands back four numbers is your decision. Anything from this week works: a
structfilled through a pointer, four output parameters, or something else you can justify. - Keep the work independent of the study-report helpers.
Acceptance cases
{12, 0, 20, 101, 8}gives 3 accepted, 2 rejected, 40 pages and a largest session of 20.{0, 101}gives 0 accepted, 2 rejected, 0 pages and a largest of 0.- A count of zero gives four zeros.
{100, 1}gives 2 accepted, 0 rejected, 101 pages and a largest of 100. Both boundaries are accepted.- The input array is unchanged after the call.
Spend at least one full attempt with every hint below closed; getting stuck and then unstuck is where the learning happens.
Predict and run
#include <stdio.h>
int main(void) {
int pages[3] = {12, 20, 8};
int total = 0;
for (int i = 0; i < 3; i++) {
total += pages[i];
}
printf("%d\n", total);
return 0;
}Check your prediction
40This program adds three values, but it is far from the brief. It has no validation, no rejection count, no largest value, and its result cannot be reused, because it lives inside main and is only printed. List those gaps before you start; each one becomes part of your design.
Practice
Your turn
Implement the brief. Assert every acceptance case, then print the sample's summary as four numbers:
3 2 40 20#include <stdio.h>
#include <assert.h>
int main(void) {
int pages[5] = {12, 0, 20, 101, 8};
// Write your solution here.
printf("%d pages in the first session\n", pages[0]);
return 0;
}Hint 1: the idea
Every element takes exactly one of two paths: rejected, or accepted. Only the accepted path touches the total and the largest. All four numbers must start at a known value before the loop.
Hint 2: a first step
Before the accepted numbers, get just the rejected count right: write the loop and the validity check, and assert that {0, 101} gives 2 rejected and 0 accepted. Run it before you add anything else.
Hint 3: a plan, if you are still stuck
- Decide how the four numbers leave the function, and write the function's first line.
- Make it report four zeros, and assert the empty case against it.
- Add the loop and the rejection path; assert the all-invalid case.
- Add the accepted numbers; assert the sample and the boundary case.
- Print one summary so you can see the result, not just the success of the checks.
Show one solution
This solution fills a struct through a pointer, the pattern from Day 5. Four int * output parameters would be just as correct.
#include <stdio.h>
#include <assert.h>
struct Summary {
int accepted;
int rejected;
int total;
int largest;
};
void summarize(const int pages[], int count, struct Summary *out) {
out->accepted = 0;
out->rejected = 0;
out->total = 0;
out->largest = 0;
for (int i = 0; i < count; i++) {
if (pages[i] < 1 || pages[i] > 100) {
out->rejected++;
continue;
}
out->accepted++;
out->total += pages[i];
if (pages[i] > out->largest) {
out->largest = pages[i];
}
}
}
int main(void) {
int pages[5] = {12, 0, 20, 101, 8};
struct Summary result;
summarize(pages, 5, &result);
assert(result.accepted == 3 && result.rejected == 2);
assert(result.total == 40 && result.largest == 20);
int bad[2] = {0, 101};
struct Summary invalid;
summarize(bad, 2, &invalid);
assert(invalid.accepted == 0 && invalid.rejected == 2);
assert(invalid.total == 0 && invalid.largest == 0);
struct Summary empty;
summarize(pages, 0, &empty);
assert(empty.accepted == 0 && empty.rejected == 0);
assert(empty.total == 0 && empty.largest == 0);
int edges[2] = {100, 1};
struct Summary edge;
summarize(edges, 2, &edge);
assert(edge.accepted == 2 && edge.rejected == 0);
assert(edge.total == 101 && edge.largest == 100);
assert(pages[1] == 0 && pages[3] == 101);
printf("%d %d %d %d\n", result.accepted, result.rejected, result.total, result.largest);
return 0;
}3 2 40 20The function sets all four fields before the loop, so the caller's uninitialized struct Summary never leaks garbage into a result, and the empty case comes out as four zeros. continue after a rejection keeps the two paths apart, so accepted plus rejected always equals the count. The const parameter and the final assertion on pages both protect the promise that the input is unchanged, and the {100, 1} case pins both boundaries. If this went smoothly, the last lesson's review will check that it stays with you.
After it passes
Prove it can fail
Break your solution three ways, one at a time, and check that an assertion catches each. Make the upper check reject exactly 100 (in the solution above, pages[i] >= 100): the {100, 1} case should fail. Count a session as accepted before the validity check: the sample should report 5 accepted. Remove the lines that set the four numbers to 0 before the loop: the results are no longer predictable, because local variables start indeterminate. Even if a run happens to pass, the code is wrong. Restore all three.
Mistakes worth checking for
- Accepted plus rejected is not equal to the count. Some element was counted twice, or not at all. Each one must take exactly one path.
- The largest is 101. It is updated before the validity check, so a rejected value was considered.
- The empty case prints strange numbers. The summary was not set to zero before the loop.
- 100 pages is rejected. The upper check uses
>=where the brief says "through 100". - The compiler rejects an assignment to
pages[i]. Good: theconstis doing its job. Compute in the summary, not in the input array.
Completion is saved in this browser only. It does not measure mastery.