cs-021 — Trace the Tally
| Field | Value |
|---|---|
| id | cs-021 |
| difficulty | Beginner |
| topics | tracing, loops |
| lecture | lecture-06 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | none |
| objective | state inputs, outputs, and assumptions, then solve a small instance by hand |
| solution | held by the instructor; discussed after the attempt |
Problem
A short accumulator loop with a twist (skip multiples of 3). Predict output by trace table, then verify.
Your deliverable is the computed aggregates for the worked instance, the accumulator update rules, and one test input that breaks a careless initialisation. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: numbers 1..12, skip multiples of 3
- Output: sum 1+2+4+5+7+8+10+11 = 48
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- Trace table columns: n, condition, total. Fill every row before you predict.
- Every aggregate here is a small machine: state + update rule.
- The initial value is part of the algorithm, not a detail.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which accumulator was hardest to get right, and what test would have caught the mistake?"
Constraints and assumptions
- Difficulty: Beginner (no prior experience assumed).
- Topics: tracing, loops.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for a first attempt; partial answers with clear reasoning are worth more than silent struggling.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Construct an input that maximises the trace length and count the steps exactly.