Return to this page about a week after you finish, and again about a month later. Forgetting is not a sign that you did not learn; it is what memory does without practice. Pulling an idea back out of memory, with effort, is what makes it last. Rereading a solution feels productive because everything looks familiar, but recognizing code is much easier than writing it. So begin each visit with a blank editor, try every task before looking at anything, and use what you could not do to decide which lessons to revisit. These reviews are extra practice, not part of Day 7's time estimate.
Retrieve
Without opening an earlier lesson, write int total(const int values[], int count) that returns the sum of the first count elements and returns 0 for count 0. Then answer in a sentence or two each:
- Why does an array parameter need a separate count?
- Why is
conston the parameter useful to the caller? - What is the largest valid index of an array declared with 8 elements?
#include <stdio.h>
int main(void) {
int values[3] = {25, 10, 15};
// Write total above main, then print total(values, 3) and total(values, 0).
printf("%d\n", values[0]);
return 0;
}Check your answer
total(values, 3) should print 50 and total(values, 0) should print 0. An array parameter is really a pointer to the first element, so the function cannot find out how many elements there are; the caller has to say. const is a promise the compiler enforces: the caller can pass its data knowing the function cannot change it. The largest valid index of an 8-element array is 7.
If you hesitated on any of these, revisit Day 4.
Debug
Each fragment below contains at least one fault that the course covered. Explain it without running the invalid code, write down how to repair it, and only then open the check.
int values[3] = {25, 10, 15};
int total = 0;
for (int i = 0; i <= 3; i++) {
total += values[i];
}Check your answer
The loop condition allows i to reach 3, and values[3] is past the end of the array. That read is undefined behavior: it may produce a plausible number, which is the dangerous part. The repair is to continue while i is less than 3, the element count.
int *latest_minutes(void) {
int minutes = 25;
return &minutes;
}Check your answer
minutes is a local variable, so its lifetime ends when the function returns, and the caller receives a dangling pointer. The repair is to return the int value itself, or to have the caller pass the address of storage it owns.
int set_minutes(int value, int *out) {
*out = value;
if (out == NULL || value < 1 || value > 180) {
return 0;
}
return 1;
}Check your answer
Two faults: the store happens before validation, so a rejected value still changes the caller's variable, and it dereferences out before checking it for NULL. Move the store after the complete check.
Predict and run
#include <stdio.h>
int main(void) {
int values[4] = {0, 1, 10, 11};
int count = 0;
for (int i = 0; i < 4; i++) {
if (values[i] >= 1 && values[i] <= 10) {
count++;
}
}
printf("%d\n", count);
return 0;
}Check your prediction
2Without opening an earlier lesson, explain why replacing && with || would count all four values. Then name a single input value that would reveal that bug in a test.
Check your answer
With ||, a value passes if it is at least 1 or at most 10, and every integer is one or the other, so all four are counted. Any value far outside the range, such as 500, is counted by the broken version and not by the correct one.
Transfer
Your turn
Implement count_in_range(values, count, low, high), counting elements from low through high inclusive. Return 0 for an empty collection or for a reversed range where low is greater than high. Assert both boundaries, the empty case and the reversed range, and add one case that would catch || used in place of &&. Print Review checks passed at the end.
#include <stdio.h>
#include <assert.h>
int main(void) {
int values[4] = {0, 1, 10, 11};
// Write your solution here.
return 0;
}Hint 1: the idea
An accepted element must satisfy both bounds at the same time. A reversed range accepts nothing, because no number is at least 10 and at most 1 together.
Hint 2: a first step
Start with a zero counter and visit only indexes strictly below count. Write the boundary assertions first, using the array {0, 1, 10, 11} with the range 1 through 10.
Show one solution
#include <stdio.h>
#include <assert.h>
int count_in_range(const int values[], int count, int low, int high) {
int found = 0;
for (int i = 0; i < count; i++) {
if (values[i] >= low && values[i] <= high) {
found++;
}
}
return found;
}
int main(void) {
int values[4] = {0, 1, 10, 11};
assert(count_in_range(values, 4, 1, 10) == 2);
assert(count_in_range(values, 0, 1, 10) == 0);
assert(count_in_range(values, 4, 10, 1) == 0);
assert(count_in_range(values, 4, 2, 9) == 0);
printf("Review checks passed\n");
return 0;
}Review checks passedThe reversed range needs no special code: with &&, no value can pass both comparisons. The range 2 through 9 contains none of the four values, so the || bug would count all four and fail that assertion. Boundaries 1 and 10 are inside the range and 0 and 11 just outside, so moving either bound by one changes the first result.
Choose what to practice next
If bounds or lifetime were unclear, return to Days 4 and 5 before studying dynamic allocation, where the same questions about who owns memory and how long it lives become harder. This page did not review parsing. If Day 6's parser felt shaky, reopen Validate Before Converting and explain its contract aloud, stage by stage, before attempting a more general parser. If testing felt mechanical, take any function from this week and invent a mutation your tests would miss, then add the case that catches it. C++ is a separate learning path; learning it later will introduce its own strings, containers and ownership tools. There is no C++ prerequisite for completing this course.
Completion is saved in this browser only. It does not measure mastery.