1.2.4 - Trace tables and algorithm outputs
An algorithm can look fixed on the page while its variables keep changing underneath. In this lesson, you will expose those changes in a trace table, use supplied data to determine exact output, and find the value held by a variable at a named point.
State and output
To determine what an algorithm does, follow its instructions in order using the data you are given. At any point, the values currently held by its variables are its state. Output is different: a value becomes output only when an OUTPUT instruction is reached.
Trace table
A trace table records variable values, condition results and outputs as an algorithm is followed step by step.
In this lesson, SET total TO 7 assigns the value 7 to total. For an instruction such as SET total TO total + 3:
- read the current value of
totalon the right; - calculate
7 + 3; - replace the old value of
totalwith10.
It is an assignment, not an algebra equation, and the old value is no longer held by that variable.
Worked example: a value is not automatically output
1 SET counters TO 7
2 SET counters TO counters + 3
3 OUTPUT counters - 4
| Point reached | counters | Output |
|---|---|---|
| After line 1 | 7 | - |
| After line 2 | 10 | - |
| After line 3 | 10 | 6 |
The variable counters finishes with the value 10, but the algorithm outputs 6. Line 3 calculates 10 - 4 for display; it does not assign the result back to counters. In every Output column in this lesson, - means no output at this point, not zero.
Building a trace table
A useful trace table makes time visible. Its rows identify when each recorded value applies, so an intermediate value cannot be confused with the final one.
Use this method:
- Mark every supplied input and initial value.
- Follow one instruction at a time; never do several passes mentally at once.
- For an assignment, calculate the right-hand side using the current state, then replace the variable on the left.
- Record a condition result when it decides which path is followed.
- Record output only when the
OUTPUTinstruction executes.
We will copy current variable values into later rows even when they have not changed. Other layouts can also be correct, but the state and timing must remain unambiguous.
Worked example: following one selected path
Supplied input: temperature = 18
1 INPUT temperature
2 SET fee TO 4
3 IF temperature < 20 THEN
4 SET fee TO fee + 2
5 ENDIF
6 OUTPUT fee
| Point reached | temperature | temperature < 20 | fee | Output |
|---|---|---|---|---|
| After line 1 | 18 | not tested | not set | - |
| After line 2 | 18 | not tested | 4 | - |
| Test at line 3 | 18 | True | 4 | - |
| After line 4 | 18 | True | 6 | - |
| After line 6 | 18 | True | 6 | 6 |
The condition is True, so line 4 executes. Immediately after line 4, fee holds 6; line 6 then outputs 6. With a supplied input of 23, the condition would be False, line 4 would be skipped, and fee would remain 4 for the output.
Tracing repetition
When instructions repeat, the state at the end of one pass becomes the starting state for the next. A reliable table therefore gives each pass its own row.
In the following pseudocode, FOR pass FROM 1 TO 3 means that the loop body executes three times, with pass equal to 1, then 2, then 3.
Worked example: output inside and after a loop
1 SET total TO 1
2 FOR pass FROM 1 TO 3
3 SET total TO total * 2
4 OUTPUT total
5 ENDFOR
6 OUTPUT total + 1
| Point reached | Loop pass | total | Output |
|---|---|---|---|
| After line 1 | before loop | 1 | - |
| Pass 1, after line 4 | 1 | 2 | 2 |
| Pass 2, after line 4 | 2 | 4 | 4 |
| Pass 3, after line 4 | 3 | 8 | 8 |
| After line 6 | after loop | 8 | 9 |
The complete output sequence is 2, 4, 8, 9. Line 4 produces output on every pass, while line 6 executes once after the loop. The final value of total is still 8: outputting total + 1 does not change total.
A branch inside a loop
A condition inside a loop must be tested again on every pass. Do not copy its previous True or False result: earlier assignments may have changed the value being tested.
In this example, FOR round FROM 1 TO 4 uses the four values 1, 2, 3, 4.
1 SET score TO 2
2 FOR round FROM 1 TO 4
3 IF score < 6 THEN
4 SET score TO score + 2
5 ELSE
6 SET score TO score - 1
7 ENDIF
8 OUTPUT score
9 ENDFOR
Worked trace
round | score < 6 at line 3 | score after the branch | Output at line 8 |
|---|---|---|---|
| 1 | True | 4 | 4 |
| 2 | True | 6 | 6 |
| 3 | False | 5 | 5 |
| 4 | True | 7 | 7 |
On round 3, the current score is exactly 6. The strict comparison score < 6 is therefore False, so the ELSE branch changes the score to 5. That change makes the condition True again on round 4.
Pinpointing a value
Some questions ask for a variable's value at a given point, not its final value. Use the line number and wording literally. If a line executes several times, count its executions and stop immediately after the one named.
For the following algorithm, the body repeats while counter <= 4. The condition is checked before each pass.
1 SET counter TO 1
2 SET total TO 0
3 WHILE counter <= 4
4 SET total TO total + counter
5 SET counter TO counter + 1
6 ENDWHILE
7 OUTPUT total
Worked trace focused on line 4
| Execution of line 4 | counter used | total immediately after line 4 |
|---|---|---|
| 1st | 1 | 1 |
| 2nd | 2 | 3 |
| 3rd | 3 | 6 |
| 4th | 4 | 10 |
Immediately after the third execution of line 4, total holds 6. That is the answer at the named point, even though tracing further gives a final output of 10. After the fourth pass, line 5 changes counter to 5; the line 3 condition is then False, so the loop stops.
Before giving an answer, check four things: the supplied starting values, the branch taken on each pass, the exact moment output occurs, and whether the question asks for an intermediate value or final output.