A weekly summary reports the longest and the shortest session. Both sound like the same loop with the comparison flipped, and that is exactly where the bug hides. The longest search can start from 0, because every real duration is positive and beats it. The shortest search cannot: if it starts from 0, nothing is ever smaller, and every week reports a shortest session of 0 minutes. In this checkpoint you handle that difference and the empty collection deliberately, with tests that prove it.
For this course, durations are positive, and an empty collection has no sessions to compare. We define both results as 0 for an empty vector. That is a choice, stated in the contract, so callers and tests know what to expect.
Predict and run
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{20, 45, 10};
int best = 0;
for (int value : values) {
if (value > best) {
best = value;
}
}
std::cout << best << "\n";
return 0;
}Check your prediction
45Replace the vector's contents with an empty initializer, std::vector<int> values{};. The loop runs zero times and best stays 0, a defined result. Nothing ever read values[0], which is what keeps the empty case safe. Now change > to < to find the shortest and run again with the original three values: it prints 0, not 10.
What you will use
- A
constreference parameter. The summary reads the durations and must not copy or change them. - An accumulator that tracks "best so far." Updated only when an element beats it.
empty()before indexing. The shortest search starts from the first element, which exists only when the vector is not empty.assertfrom<cassert>. A typical case, a single-element case and the empty case for each function.
Plan before you type
- Write the contract for each function in one sentence, including the empty case.
- Write all six assertions first, with answers worked out by hand.
- Implement
longestwith a starting value of 0 and run. - Implement
shortest: return 0 for an empty vector, otherwise start fromvalues[0]and compare every element. - Run until all assertions pass, then try the failures in the callout.
Prove it can fail
With all checks passing, change shortest to start from 0 instead of values[0]. The assertion for 20, 45 and 10 should fail because the function now returns 0. Restore it, then delete the empty() guard and run the empty case. Reading values[0] of an empty vector is undefined behavior, so the result is not reliable: it may crash, return a strange number, or even seem to pass. That last possibility is why you prevent undefined behavior with a guard rather than trusting a test to reveal it.
Mistakes worth checking for
shortestreturns 0 for every non-empty vector: the starting value is 0, and no positive duration is smaller.- The empty case returns a random number or crashes: the code reads
values[0]without checkingempty(). That is undefined behavior, not a stable symptom. longest({20})returns 0: the comparison is inside the loop but the update assigns the wrong variable, orbestis declared inside the loop and reset every iteration.- A compile error when calling
longest({}): the parameter is a non-const reference, which cannot bind to a temporary vector. Useconst std::vector<int>&. - A warning about comparing signed and unsigned values (it stops a local
-Werrorbuild; the browser editor doesn't show it): an index loop usesint iwithvalues.size(). Use a range-based loop, orstd::size_tfor the index.
Practice
Your turn
Write longest and shortest, each taking const std::vector<int>& and returning an int. Assert:
longest: 20, 45 and 10 give 45; a single 20 gives 20; an empty vector gives 0shortest: 20, 45 and 10 give 10; a single 20 gives 20; an empty vector gives 0
When all six pass, print Collection checks passed.
#include <iostream>
#include <vector>
#include <cassert>
int main() {
// Write both functions above main and your assertions here.
return 0;
}Hint 1: the idea
Keep the best value encountered so far. For the longest, 0 is a safe start. For the shortest, the only safe start is a real element, so handle the empty vector before reading one.
Hint 2: a first step
Write all six assertions first, with both functions returning 0. Run: the first failure names longest with 20, 45 and 10. Finish longest, then start shortest from its empty-vector case.
Show one solution
#include <iostream>
#include <vector>
#include <cassert>
int longest(const std::vector<int>& values) {
int best = 0;
for (int value : values) {
if (value > best) {
best = value;
}
}
return best;
}
int shortest(const std::vector<int>& values) {
if (values.empty()) {
return 0;
}
int best = values[0];
for (int value : values) {
if (value < best) {
best = value;
}
}
return best;
}
int main() {
assert(longest({20, 45, 10}) == 45);
assert(longest({20}) == 20);
assert(longest({}) == 0);
assert(shortest({20, 45, 10}) == 10);
assert(shortest({20}) == 20);
assert(shortest({}) == 0);
std::cout << "Collection checks passed\n";
return 0;
}Collection checks passedlongest can start from 0 because the contract promises positive durations. shortest starts from a real element, and the early return makes sure that element exists. Comparing values[0] with itself in the first iteration is harmless. The single-element cases catch a start value that is too small or too large, and the empty cases pin down the contract's choice. On Day 5 you will store each duration together with its title and completion state in one record.
Completion is saved in this browser only. It does not measure mastery.