How do you know an algorithm works? A trace table lets you follow it step by step on paper, recording the value of every variable. It is a core exam skill and a powerful debugging technique.
Building a trace table
Make one column for each variable and one for output (and sometimes one for any condition tested). Each row records the values after a line or loop iteration executes. Start with the initial values and update only the variable that changes on each step.
Example:
Total <- 0
FOR i <- 1 TO 4
IF i MOD 2 = 0 THEN
Total <- Total + i
ENDIF
NEXT i
OUTPUT Total
| i | i MOD 2 = 0 | Total | Output |
|---|---|---|---|
| (start) | 0 | ||
| 1 | FALSE | 0 | |
| 2 | TRUE | 2 | |
| 3 | FALSE | 2 | |
| 4 | TRUE | 6 | |
| 6 |
The algorithm adds the even numbers up to 4 and outputs 6. Tracing reveals the purpose of an unknown algorithm, which is a common question: “State the purpose of the algorithm.”
Finding errors
A trace table exposes mistakes such as:
- Off-by-one errors, for example looping to N-1 instead of N.
- Wrong initial values, for example forgetting to set a counter to 0.
- Infinite loops, where a condition never becomes false.
- Logic errors, where the program runs but gives wrong results.
Error types in general: syntax errors break the rules of the language and are caught by the translator; runtime errors crash the program (division by zero); logic errors produce wrong output.
Verification and testing
Validation checks input is reasonable (range check, length check, type check, presence check). Verification checks input was copied correctly (double entry, visual check).
Test data should include:
- Normal data: typical valid values.
- Boundary (extreme) data: values at the limits of what is allowed, such as 0 and 100 for a percentage.
- Abnormal (erroneous) data: invalid values such as -5 or “abc”, which should be rejected.
Good practice: dry run the algorithm with each test set, predict the expected output first, compare it with the actual output, then fix and retest. A white-box tester knows the code and tests every path; a black-box tester checks only inputs and outputs. Programs are tested as modules (unit), then together (integration), then by the user (acceptance).
Check your understanding
What is the main purpose of a trace table?
- To make a program run faster
- To record variable values step by step to check an algorithm
- To compress data
- To design a database
Answer: To record variable values step by step to check an algorithm. It reveals both purpose and errors.
Which type of error produces the wrong output without crashing?
- Syntax error
- Logic error
- Runtime error
- Hardware error
Answer: Logic error. The program runs but the result is incorrect.
For an input that must be 1 to 100, which is boundary test data?
- 50
- 100
- -5
- “abc”
Answer: 100. Boundary data sits at the edge of the valid range.
What is the difference between validation and verification?
- Validation checks data is reasonable; verification checks it was entered accurately
- They are identical
- Validation encrypts data
- Verification deletes data
Answer: Validation checks data is reasonable; verification checks it was entered accurately. Double entry is a verification method.
