🎯 Syllabus & Goals 3 min
Cambridge 7.7 · Trace tables Cambridge 7.8 · Identify and correct errors Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Complete a trace table to document a dry run of a flowchart or pseudocode.
- Use a trace table and test data to find the error in an algorithm.
- Identify errors in given pseudocode and write the corrected version.
Textbook: Chapter 7, §7.7–7.8 (pp. 282–288)
Recap / Warm-Up 5 min
Last lesson you chose test data and expected results. A trace table is where you run that data through an algorithm, by hand, one step at a time.
Quick starter
A range check should accept 1 to 10 but is written UNTIL N > 1 AND N < 10. Which boundary value exposes the error?
Reveal the answer
Both 1 and 10 are wrongly rejected. Boundary (and extreme) data finds this; normal data such as 5 would not.
🧠 Key Concept 14 min
1 · Dry runs and trace tables
Set up one column per variable and one for OUTPUT. Then, during the dry run:
- each time a variable changes, write its new value in its column;
- each time something is output, write it in the OUTPUT column — without quotation marks;
- move down a row as the algorithm moves on (a new pass of a loop usually starts a new row).
Here is a flowchart that exam papers love — single-letter names, and you must work out what it does:
INPUT X.2 · Using a trace table to find errors
Trace tables and test data are how errors are found. If the actual output differs from the expected output, the trace shows where the values first go wrong. Look for these six common faults:
Worked Example 12 min
Worked example 1 · Dry run and purpose
The flowchart as pseudocode, dry run with test data 14, 3, 27, 9, 30, 6, 18, 1, 22, 11.
A ← 0
B ← 0
C ← 100
OUTPUT "Enter ten numbers"
REPEAT
INPUT X
IF X > B
THEN
B ← X
ENDIF
IF X < C
THEN
C ← X
ENDIF
A ← A + 1
UNTIL A = 10
OUTPUT B, " ", C| A | B | C | X | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | 100 | Enter ten numbers | |
| 1 | 14 | 14 | 14 | |
| 2 | 3 | 3 | ||
| 3 | 27 | 27 | ||
| 4 | 9 | |||
| 5 | 30 | 30 | ||
| 6 | 6 | |||
| 7 | 18 | |||
| 8 | 1 | 1 | ||
| 9 | 22 | |||
| 10 | 11 | 30 1 |
- Row 1 = initial values and the prompt.examiners award a mark for the starting values.
- X = 14: 14 > 0 so B ← 14; 14 < 100 so C ← 14; A ← 1.
- Only changes are written. X = 9 changes neither B nor C, so those cells stay blank.
- Purpose: it inputs ten numbers and outputs the largest (B) and smallest (C). Output: 30 1.
Worked example 2 · Finding and fixing the error
Now dry run the same algorithm with 250, 180, 320, 140, 210, 400, 190, 160, 300, 170. Expected output: 400 140.
| A | B | C | X | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | 100 | Enter ten numbers | |
| 1 | 250 | 250 | ||
| 2 | 180 | |||
| 3 | 320 | 320 | ||
| 4 | 140 | |||
| 5 | 210 | |||
| 6 | 400 | 400 | ||
| 7 | 190 | |||
| 8 | 160 | |||
| 9 | 300 | |||
| 10 | 170 | 400 100 |
- Compare actual with expected. Largest is right (400); smallest is wrong (100).
- Find where it went wrong. C starts at 100 and no input is below 100, so C never changes.the error is in initialisation: the starting values only suit numbers from 0 to 100. Negative numbers would break B the same way.
- Correct it. Set B and C to the first number input, then loop for the other nine:
OUTPUT "Enter ten numbers" INPUT X B ← X C ← X A ← 0 REPEAT INPUT X IF X > B THEN B ← X ENDIF IF X < C THEN C ← X ENDIF A ← A + 1 UNTIL A = 9 OUTPUT B, " ", Cprint("Enter ten numbers") x = int(input()) b = x c = x for a in range(9): x = int(input()) if x > b: b = x if x < c: c = x print(b, c)
- Re-test with the same data to prove the fix:the loop now runs 9 times, because the first number was input before it.
A B C X OUTPUT Enter ten numbers 0 250 250 250 1 180 180 2 320 320 3 140 140 4 210 5 400 400 6 190 7 160 8 300 9 170 400 140
Worked example 3 · Identify and correct four errors
This algorithm should input 8 positive numbers and output their average. It contains four errors.
01 Total ← 1 02 FOR Count ← 1 TO 8 03 REPEAT 04 INPUT Number 05 UNTIL Number < 0 06 Total ← Total + Count 07 NEXT Count 08 Average ← Total / 7 09 OUTPUT Average
| Line | Error | Correction |
|---|---|---|
| 01 | total must start at zero | Total ← 0 |
| 05 | loop only accepts negative numbers | UNTIL Number > 0 |
| 06 | adds the counter, not the number | Total ← Total + Number |
| 08 | divides by the wrong count | Average ← Total / 8 |
Total ← 0
FOR Count ← 1 TO 8
REPEAT
OUTPUT "Enter a positive number "
INPUT Number
UNTIL Number > 0
Total ← Total + Number
NEXT Count
Average ← Total / 8
OUTPUT Averagetotal = 0 for count in range(1, 9): number = 0 while number <= 0: number = int(input("Enter a positive number ")) total = total + number average = total / 8 print(average)
Test the corrected version with 4, 6, −3, 10, 2, 8, 5, 7, 6 (−3 should be rejected):
| Count | Number | Total | Average | OUTPUT |
|---|---|---|---|---|
| 0 | ||||
| 1 | 4 | 4 | ||
| 2 | 6 | 10 | ||
| 3 | -3 | |||
| 10 | 20 | |||
| 4 | 2 | 22 | ||
| 5 | 8 | 30 | ||
| 6 | 5 | 35 | ||
| 7 | 7 | 42 | ||
| 8 | 6 | 48 | 6 | 6 |
6.0
Try It Yourself 12 min
Goal: Dry run the original A/B/C algorithm with 40, 80, 19, 17, 30, 11, 60, 15, 13, 90. Complete the trace table and state the output.
Goal: The corrected A/B/C algorithm still has a weakness: nothing stops a user typing a word instead of a number. Suggest how to amend it, and give one item of test data that would check your change.
Goal: This should count how many of 5 inputs are even. Find two errors and dry run your corrected version with 3, 8, 12, 5, 6.
Even ← 0
Count ← 0
REPEAT
INPUT N
IF N MOD 2 = 1
THEN
Even ← Even + 1
ENDIF
UNTIL Count = 5
OUTPUT EvenHint
What does N MOD 2 give for an even number? And does Count ever change — how many times would this loop run?
📝 Exam Practice 10 min
Complete a trace table for this algorithm using the input data 5, −2, 7, 0.
Count ← 0
Total ← 0
REPEAT
INPUT Value
IF Value > 0
THEN
Total ← Total + Value
Count ← Count + 1
ENDIF
UNTIL Value = 0
OUTPUT Total / CountMark scheme
| Count | Total | Value | OUTPUT |
|---|---|---|---|
| 0 | 0 | ||
| 1 | 5 | 5 | |
| -2 | |||
| 2 | 12 | 7 | |
| 0 | 6 |
- Initial values 0, 0 (1)
- Value column 5, −2, 7, 0 (1)
- Count 1, 2 and Total 5, 12 — no change for −2 (1)
- Output 6 (1)
This pseudocode should output the largest of 20 numbers input. Identify the error on each of lines 01, 03, 06 and 08 and suggest a correction.
01 Largest ← 1000 02 FOR Count ← 1 TO 20 03 OUTPUT Number 04 IF Number > Largest 05 THEN 06 Largest ← Count 07 ENDIF 08 NEXT Largest 09 OUTPUT Largest
Mark scheme
- 01 — start value too high:
Largest ← 0or the first number input (1). - 03 — should be
INPUT Number(1). - 06 — should be
Largest ← Number(1). - 08 — should be
NEXT Count(1).
Explain how a trace table is used to find errors in an algorithm.
Mark scheme
- The algorithm is dry run with test data, recording each variable's value as it changes and each output (1).
- The actual results are compared with the expected results; a difference shows where the error is (1).
The original A/B/C algorithm (B ← 0, C ← 100) is used with ten negative numbers. State the output it gives for B, and explain why it is wrong.
Mark scheme
- B is output as 0 (1).
- No negative number is greater than 0, so B is never replaced; 0 was not one of the inputs (1).
🗝️ Recap & Key Terms 3 min
A dry run works through an algorithm by hand; a trace table records every change and every output. Comparing actual and expected results shows where an algorithm goes wrong. Check initialisation, conditions, loop bounds, variables, order and missing statements.
- Dry run
- The manual exercise of working through an algorithm step by step, using test data.
- Trace table
- A table recording the value of each variable each time it changes, and any output, during a dry run.
- Expected result
- The output the algorithm should produce for given test data, worked out beforehand.
- Error (in an algorithm)
- A fault that makes the actual result differ from the expected result, e.g. a wrong condition or start value.
Homework 1 min
Task (≤ 15 min): This should input 12 whole numbers, each from 1 to 50, and output their total. (a) Identify four errors. [4] (b) Rewrite it correctly. [4]
Sum ← 1
FOR Index ← 1 TO 12
REPEAT
OUTPUT "Enter a number from 1 to 50 "
INPUT Value
UNTIL Value < 1 OR Value > 50
Sum ← Sum + Index
Index ← Index + 1
NEXT Value
OUTPUT SumModel answer
Sum ← 1should beSum ← 0.- The UNTIL condition accepts only invalid values — it should be
UNTIL Value >= 1 AND Value <= 50. Sum ← Sum + Indexshould addValue.Index ← Index + 1must be removed — a FOR loop increases its own counter.NEXT Valueshould beNEXT Index. (Any four.)
Sum ← 0
FOR Index ← 1 TO 12
REPEAT
OUTPUT "Enter a number from 1 to 50 "
INPUT Value
UNTIL Value >= 1 AND Value <= 50
Sum ← Sum + Value
NEXT Index
OUTPUT Sum