22 min read
Trace Tables
Dry-run an algorithm step by step in a table to find its output and to catch logic errors before coding.
What Is a Trace Table?
A dry run means following an algorithm on paper, without a computer. A trace table keeps it organised:
- Make a column for each variable, each condition, and the output.
- Work through the algorithm line by line.
- Each time a value changes, write it in the next row.
Example: Multiples of 3
What is output?Pseudocode
BEGIN count ← 1 total ← 0 WHILE count <= 4 total ← total + count × 3 count ← count + 1 ENDWHILE OUTPUT totalEND| Trace table | |||
|---|---|---|---|
Finding a Logic Error by Tracing
Dinesh planned an algorithm to input three marks and output their average. Trace it with the marks 60, 70 and 80 — the average should be 70.
Is this flowchart correct?
Read this flowchart as text
- Terminator: Start (then → total = 0 count = 1)
- Process: total = 0 count = 1 (then → count < 3?)
- Decision: count < 3? (Yes → INPUT mark; No → average = total ÷ 3)
- Input/output: INPUT mark (then → total = total + mark count = count + 1)
- Process: average = total ÷ 3 (then → OUTPUT average)
- Process: total = total + mark count = count + 1 (then → count < 3?)
- Input/output: OUTPUT average (then → Stop)
- Terminator: Stop
| Tracing with marks 60, 70, 80 | ||||
|---|---|---|---|---|
Practice
A loop should output 1 to 10 but outputs 1 to 9. What kind of error is this?
More lessons in Algorithms, Flowcharts and Pseudocode · Next: Flowcharts and Pseudocode Worksheet · Previous: Pseudocode for Selection and Loops
