A trace table (also called a dry run) is a technique used to manually track the values of variables as an algorithm executes, line by line. It lets you verify that code produces the correct output without running it on a computer.
In OCR J277 exams, you will be asked to complete trace tables for given algorithms. Each column represents a variable or output; each row represents one iteration or key step.
1. Read through the algorithm carefully before starting
2. Create a column for every variable referenced in the algorithm, plus an output column if needed
3. Initialise: write any starting values in the first row
4. Execute each line in order, updating only the variable that changes on that line
5. Leave other cells blank if their value did not change on that step
6. Continue until the algorithm ends or reaches the required condition
| i | total | OUTPUT | |
|---|---|---|---|
| Initialise | 0 | ||
| i = 1 | 1 | 1 | |
| i = 2 | 2 | 3 | |
| i = 3 | 3 | 6 | |
| i = 4 | 4 | 10 | |
| Output | 10 |
List: [5, 3, 8, 1] — trace the first full pass
| j | list[0] | list[1] | list[2] | list[3] | temp | Swap? |
|---|---|---|---|---|---|---|
| Start | 5 | 3 | 8 | 1 | ||
| j=0: 5>3? | 5 | 3 | 5 | Yes | ||
| swap | 3 | 5 | ||||
| j=1: 5>8? | 5 | 8 | No | |||
| j=2: 8>1? | 8 | 1 | 8 | Yes | ||
| swap | 1 | 8 | ||||
| End pass 1 | 3 | 5 | 1 | 8 |
Sorted list: [2, 5, 9, 14, 21, 34, 42] — search for target = 21
| low | high | mid | list[mid] | Action |
|---|---|---|---|---|
| 0 | 6 | 3 | 14 | 14 < 21 → low = mid+1 = 4 |
| 4 | 6 | 5 | 34 | 34 > 21 → high = mid-1 = 4 |
| 4 | 4 | 4 | 21 | Found! Return index 4 |
Trace tables are used to find logic errors — where the program runs but gives the wrong answer. Example: a loop starting at index 1 instead of 0 would skip the first element. Tracing reveals this because you can see when the expected value doesn't appear.
They can also detect infinite loops — if tracing shows a variable never reaches the loop exit condition, the loop will never terminate.
8 questions · 20 marks
| Term | Definition |
|---|
10 questions · 10 marks · 10 minutes