Your report now has a collection rather than one variable per session, and a collection raises a question a single value never did: what is the answer when there is nothing in it? The total of no sessions is clearly 0. The longest of no sessions is less obvious, and the shortest is a trap. In this checkpoint you write summary functions over an array and its count, and you decide the empty result before you write the loop, because the starting value you choose is the empty answer.
Predict and run
#include <stdio.h>
int main(void) {
int values[4] = {20, 30, 10, 0};
int count = 3;
int total = 0;
for (int i = 0; i < count; i++) {
total += values[i];
}
printf("%d\n", total);
return 0;
}Check your prediction
60The array has capacity 4 but the count is 3, so the loop stops after index 2 and the unused fourth slot never affects the total. Change count to 0 without changing the array and the program prints 0. The data in unused slots is irrelevant, whatever it holds, because the count says it is not part of the collection.
What you will use
- An array and a separate count.
const int values[]for read-only access,int countfor how many elements are real. - A loop bounded by the count. Indexes 0 up to, but not including,
count. - A "best so far" variable. It holds the largest (or smallest) value seen, and is replaced whenever a better one appears.
- A deliberate empty result. This course reports 0 for the longest and shortest of an empty collection of positive durations.
- Assertions at the edges. Count 0, count 1, and a full count where the answer is not the first element.
Plan before you type
- Write the expected results for
{20, 45, 10}at counts 3, 1 and 0, for both longest and shortest. - Write
longestwithbeststarting at 0. Since every duration is positive, the first element always replaces it. - Assert the three longest cases and run.
- Write
shortestand think before choosing its starting value. Which values couldbestsafely start from, and what does each choice need when the collection is empty? - Assert the shortest cases and print the summary line.
Prove it can fail
When everything passes, copy the 0 start from longest into shortest on purpose. The shortest of {20, 45, 10} should now come out as 0, which is not even in the data, and the assertion for 10 fails. Then remove the empty check from shortest and leave best = values[0]: with count 0 that reads an element that is not part of the collection. It may well pass by luck, which is exactly why you reason about it instead of trusting the run. Restore both.
Mistakes worth checking for
- Shortest is always 0. The best-so-far value started at 0, below every real duration. Start from the first real element.
- Longest of count 1 is 45. The loop is bounded by the array size, 3, instead of
count. - The count-0 case returns some leftover number. The function reads
values[0]before checking that the collection is not empty. - A function that should be read-only changes the data. The parameter is missing
const, so an accidental assignment compiled. - The loop skips the last element. The condition stops one early, for example at
count - 1. The case where the answer is the last element, 10 for shortest, catches this.
Practice
Your turn
Write longest(values, count) and shortest(values, count) for positive durations. Both return 0 for an empty collection. For {20, 45, 10}, assert the results at counts 3, 1 and 0 for both functions, then print:
Longest 45, shortest 10
Summary checks passed#include <stdio.h>
#include <assert.h>
int main(void) {
int values[3] = {20, 45, 10};
// Write your solution here.
return 0;
}Hint 1: the idea
Keep the best value seen so far. For the longest, 0 is a safe start because every real duration beats it. For the shortest, no constant is safe, so start from the first element once you know there is one.
Hint 2: a first step
Write longest and its three assertions first, and run. Then write shortest as a copy of longest with < in the comparison, and run again: one assertion fails. Work out from the failing line what the copied starting value does wrong before you change anything.
Show one solution
#include <stdio.h>
#include <assert.h>
int longest(const int values[], int count) {
int best = 0;
for (int i = 0; i < count; i++) {
if (values[i] > best) {
best = values[i];
}
}
return best;
}
int shortest(const int values[], int count) {
if (count == 0) {
return 0;
}
int best = values[0];
for (int i = 1; i < count; i++) {
if (values[i] < best) {
best = values[i];
}
}
return best;
}
int main(void) {
int values[3] = {20, 45, 10};
assert(longest(values, 3) == 45);
assert(longest(values, 1) == 20);
assert(longest(values, 0) == 0);
assert(shortest(values, 3) == 10);
assert(shortest(values, 1) == 20);
assert(shortest(values, 0) == 0);
printf("Longest %d, shortest %d\n", longest(values, 3), shortest(values, 3));
printf("Summary checks passed\n");
return 0;
}Longest 45, shortest 10
Summary checks passedThe two functions look alike but need different starting points. longest can start from a value below every possible duration; shortest cannot, so it guards the empty case and then starts from real data. The count-1 cases prove each function respects count rather than the array size. Tomorrow you will meet pointers, the mechanism behind array parameters, and use them to let a function update its caller.
Completion is saved in this browser only. It does not measure mastery.