Skip to content

Checkpoint: Planned and Completed

The Course
•
35 min study

The planner's report has three numbers: planned, completed and remaining minutes. They are related in a precise way. A completed task still counts toward the planned total, because you did plan it, but it contributes nothing to the remaining total. So for any set of tasks, planned always equals completed plus remaining. In this checkpoint you compute all three in one pass, return them together in a record, and use that relationship as a built-in check.

A summary must describe the tasks, not change them. Taking the vector by const reference makes that a compiler-checked promise, and returning a Summary record by value hands the caller three numbers that belong together, the same way Task keeps a title with its minutes.

Predict and run

Predict before running C++
#include <iostream>
#include <vector>

struct Task { int minutes; bool done; };

int main() {
    std::vector<Task> tasks{{25, false}, {10, true}};
    int completed = 0;
    for (const Task& task : tasks) {
        if (task.done) {
            completed += task.minutes;
        }
    }
    std::cout << completed << "\n";
    return 0;
}
Check your prediction
Text
10

Change both flags to true. Completed becomes 35, which now equals the planned total, and remaining would be 0. Change both to false and the reverse happens. Every task lands in exactly one of the two totals.

What you will use

  • A Task record. Minutes and a done flag kept together.
  • A Summary record with default member initializers. planned, completed and remaining, all starting at 0, so an empty input needs no special case.
  • A const std::vector<Task>& parameter. No copy, and no way to change a task.
  • One const Task& loop. Every task adds to planned, and to exactly one of completed or remaining.
  • Return by value. The caller receives its own Summary; nothing refers back into the function's locals.
  • assert. Cases for a mix, for empty input, for all done, and for the invariant.

Plan before you type

  1. Write the expected three numbers by hand for: 25 not done with 10 done; an empty vector; a single 25 that is done.
  2. Define Summary with three int fields that default to 0.
  3. Write summarize: add each task's minutes to planned, then to completed if done, else to remaining.
  4. Write the assertions, including planned == completed + remaining for each case.
  5. Assert that the input vector is unchanged after the call.
Prove it can fail

Once the checks pass, add task.done = true; inside the loop. The build should fail with an error about assigning to a const object: the const reference is enforcing the promise. Remove it, then change the else so that remaining also adds the minutes of done tasks. The mixed case should fail on remaining == 25, and the invariant check should fail too, because now completed plus remaining is 45, not 35.

Mistakes worth checking for

  • Planned shows only the incomplete minutes: the planned += line is inside the if or else instead of before it.
  • Remaining equals planned: the condition is missing, so every task is added to remaining.
  • The empty case asserts random numbers or fails: the Summary fields have no default initializers, so they start without values. That is undefined behavior; give every field = 0.
  • A compile error when calling summarize({}): the parameter is a non-const reference, which cannot bind to a temporary.
  • The caller's tasks are marked done after a summary: the loop variable or parameter is a non-const reference and the summary changes data it should only read.

Practice

Exercise

Your turn

Define Task with minutes and a done flag, and Summary with planned, completed and remaining. Write Summary summarize(const std::vector<Task>& tasks). Assert:

  • 25 not done and 10 done give 35 planned, 10 completed and 25 remaining
  • an empty vector gives three zeros
  • a single done 25-minute task gives 25 planned, 25 completed and 0 remaining
  • for every case, planned equals completed plus remaining
  • the tasks are unchanged after the call

When all pass, print Summary checks passed.

Your workspace C++
#include <iostream>
#include <vector>
#include <cassert>

int main() {
    // Define Task, Summary and summarize above main.
    return 0;
}
Hint 1: the idea

Every task contributes to planned. Then the done flag decides whether its minutes also go to completed or to remaining, never both.

Hint 2: a first step

Start with Summary result; and loop with for (const Task& task : tasks). The first line inside the loop is result.planned += task.minutes;.

Show one solution
Run the solution C++
#include <iostream>
#include <vector>
#include <cassert>

struct Task {
    int minutes = 0;
    bool done = false;
};

struct Summary {
    int planned = 0;
    int completed = 0;
    int remaining = 0;
};

Summary summarize(const std::vector<Task>& tasks) {
    Summary result;
    for (const Task& task : tasks) {
        result.planned += task.minutes;
        if (task.done) {
            result.completed += task.minutes;
        } else {
            result.remaining += task.minutes;
        }
    }
    return result;
}

int main() {
    std::vector<Task> tasks{{25, false}, {10, true}};
    Summary mixed = summarize(tasks);
    assert(mixed.planned == 35 && mixed.completed == 10 && mixed.remaining == 25);
    assert(mixed.planned == mixed.completed + mixed.remaining);
    assert(!tasks[0].done && tasks[1].done);

    Summary empty = summarize({});
    assert(empty.planned == 0 && empty.completed == 0 && empty.remaining == 0);

    Summary all_done = summarize({{25, true}});
    assert(all_done.planned == 25 && all_done.completed == 25 && all_done.remaining == 0);
    assert(all_done.planned == all_done.completed + all_done.remaining);
    std::cout << "Summary checks passed\n";
    return 0;
}
Text
Summary checks passed

The if/else places each task in exactly one of the two totals, which is what makes the invariant hold for any input. The defaults in Summary make the empty case correct with no extra code, and the const reference guarantees the report cannot alter the plan. This is almost exactly the summarize function in the finished planner. On Day 6 you will fill the vector from text input instead of typing tasks into the source.

Was this article helpful?

Your response is saved on this device.