Sub topic 11
Desk Checking: Finding Bugs on Paper
This chapter assumes you can already build a trace table, trace a loop one iteration at a time, and pick test inputs that include edge cases. Desk checking combines those skills into one habit. Before you write any real code, you run your pseudocode by hand and compare what it produces with what it should produce.
What a Desk Check Is
A desk check is a manual run of an algorithm on a chosen input, with the expected answer worked out ahead of time. A trace table shows what the algorithm does. A desk check adds a verdict: the trace either matches the expected result or it doesn't. When it doesn't, the trace table also shows the step where the values went wrong.
Work out the expected result without using the algorithm. For example, use arithmetic, a formula, or common sense. If you derive it from the algorithm, you will copy the algorithm's bug into your expectation.
A Worked Example: Off-by-One
This loop is meant to sum the integers from 1 to n:
total ← 0
i ← 1
WHILE i < n DO
total ← total + i
i ← i + 1
END WHILE
RETURN total
Trace with n = 3 (expected 1 + 2 + 3 = 6):
i = 1, total = 0 -> total = 1, i = 2
i = 2, total = 1 -> total = 3, i = 3
i = 3: condition 3 < 3 is false, loop ends
Returns 3, not 6. Fix: change the condition to i ≤ n.The trace shows exactly where things go wrong. The loop stops at the moment i reaches the last value it should have added. That points to the loop condition, not to the body. After the fix, trace again with n = 3 and confirm the result is 6. Then try n = 1 (expected 1) and n = 0 (expected 0) to check the boundaries.
Common Logic Errors a Desk Check Catches
| Error | Symptom in the trace |
|---|---|
| Wrong loop bound (`<` vs `≤`) | One iteration too few or too many |
| Wrong starting value | First element skipped or counted twice |
| Statements in the wrong order | A variable is used before or after its intended update |
| Accumulator not initialized | The first row of the table has no defined value |
| Index past the array end | A row reads `A[n + 1]` or `A[0]` in 1-based code |
| Condition never becomes false | The loop variable stops changing across rows |
Exercises
Exercise
This function should return the sum of A[1..n] in a 1-based array. Desk check it with A = [4, 5, 6], n = 3, then identify and fix the bug. FUNCTION SumArray(A, n) total ← 0 i ← 1 WHILE i ≤ n DO i ← i + 1 total ← total + A[i] END WHILE RETURN total END FUNCTION
Show answer
The expected result is 15. In the trace, the first iteration sets i = 2 and total = 5. The second sets i = 3 and total = 11. The third sets i = 4 and then reads A[4], which is past the end of the array. The algorithm skips A[1] and goes out of bounds because i is incremented before it is used. The fix is to swap the two lines in the loop body: total ← total + A[i] first, then i ← i + 1. Tracing again gives 4, 9, 15.
Exercise
For the corrected sum-to-n loop (condition i ≤ n), which input checks the case where the loop body never runs, and what should it return?
Show answer
n = 0. The condition 1 ≤ 0 is false on the first test, so the body never runs and the function returns 0, which is the correct sum of no integers.