Before you write steps, write a short specification: what comes in, what must come out, and what is allowed. A clear specification turns “pack the eggs” into something you can trace and test.
This lesson follows decomposing a task. It also prepares you for tracing with a state table, because you need to know which values to track.
What goes into a specification?
Write three lists.
- Inputs: each value the algorithm receives, with its type (whole number, real number, text).
- Outputs: each value it must produce, with a description of how it relates to the inputs.
- Constraints: the limits on inputs and any rules the outputs must obey.
If a value can be too small, too big, negative or not a whole number, the constraints say what should happen.
Worked example
A farm packs eggs into trays that hold exactly 12. Given the number of eggs, output how many full trays can be made and how many eggs are left over.
Step 1, inputs: Eggs, a whole number.
Step 2, outputs: Trays, the number of full trays. Left, the eggs not in a full tray.
Step 3, constraints: Eggs must be a whole number and at least 0. Left must be from 0 to 11, and Trays × 12 + Left must equal Eggs.
Step 4, algorithm:
INPUT Eggs
Trays ← Eggs DIV 12
Left ← Eggs MOD 12
OUTPUT Trays, Left
Step 5, trace with three values:
| Eggs | Trays | Left | Check Trays × 12 + Left |
|---|---|---|---|
| 100 | 8 | 4 | 96 + 4 = 100 |
| 11 | 0 | 11 | 0 + 11 = 11 |
| 0 | 0 | 0 | 0 + 0 = 0 |
For 100: 12 × 8 = 96 and 100 − 96 = 4, so DIV gives 8 and MOD gives 4. Each row satisfies the rule in the constraints, which is the check that matters.
The mistake to watch for
A common slip is using ordinary division and ignoring the constraint that trays must be whole.
Mistaken algorithm: Trays ← Eggs / 12, with Eggs = 100.
This gives 8.333…, which is not a number of trays.
The correction is to read the output description: “number of full trays” is a whole number, so DIV is the operation. A second slip is skipping the constraints, so no one decides what happens for −5 eggs or 3.5 eggs. Naming the constraint tells you to add a validation step or to state the assumption.
Check yourself
1. Write the inputs, outputs and constraints for an algorithm that gives the average of 5 test marks, each from 0 to 100.
Show answer
Inputs: five marks, each a whole number (or real number) from 0 to 100. Output: Average, the total of the marks divided by 5. Constraints: each mark must be from 0 to 100, so Average is also from 0 to 100.
2. Given 250 eggs, what are Trays and Left?
Show answer
12 × 20 = 240, so 250 DIV 12 = 20. 250 − 240 = 10, so 250 MOD 12 = 10. Check: 20 × 12 + 10 = 250.
3. Convert a number of days into whole weeks and spare days. Give the output for 30 days.
Show answer
Weeks ← Days DIV 7 and Spare ← Days MOD 7. 7 × 4 = 28, so Weeks = 4 and Spare = 2. Check: 4 × 7 + 2 = 30.
Where this leads next
Next, learn to trace a sequence with a state table so you can prove an algorithm meets its specification. The pseudocode trace trainer is useful for checking DIV and MOD results.
When a specification is vague, it is hard to know what to test, and a teacher can help you tighten it. That is part of our online one-to-one Computer Science tuition, and the practice set has more specifications to write.