1.4 Finding and fixing errorsTracing, extra output, and other bug-hunting tools

The course lists five effective ways to find and correct errors: test cases, hand tracing, visualizations, debuggers, and adding extra output statements. Test cases show that something is wrong. The others help you find exactly where the program goes off course.
| Technique | What you do | Best for |
|---|---|---|
| Test cases | Run chosen inputs, compare expected and actual | Finding that a bug exists |
| Hand tracing | Follow the code on paper, one row per step | Seeing exactly where values go wrong |
| Extra output statements | Add DISPLAY lines to show values as it runs | Watching a loop from inside |
| Visualizations | Draw or chart what the program produces | Spotting odd patterns at a glance |
| Debuggers | Pause the program and step line by line | Big programs and tricky loops |
?Wait, what's a trace table?
Let's hunt a real bug. This procedure is supposed to return the average of only the positive numbers in a list. For [4, -2, 8], the positives are 4 and 8, so the answer should be 12 ÷ 2 = 6. It returns 4. Something is off, and a test case has told us so. Now we trace.
1PROCEDURE avgPositive(nums)2{3 total ← 04 count ← 05 FOR EACH n IN nums6 {7 IF (n > 0)18 {9 total ← total + n110 }11 count ← count + 1212 }13 RETURN(total / count)14}15DISPLAY(avgPositive([4, -2, 8]))
- line 7-9 Only positive numbers are added
- line 11 But every number is counted
?Wait, what does FOR EACH do?
| n | n > 0? | total | count |
|---|---|---|---|
| start | 0 | 0 | |
| 4 | true | 4 | 1 |
| -2 | false | 4 | 2 |
| 8 | true | 12 | 3 |
The trace shows it immediately. When n is -2, the IF correctly skips the addition, but count ← count + 1 sits outside the IF, so it runs for every number. The procedure divides the sum of the positives, 12, by the count of all the numbers, 3. The fix is to move count ← count + 1 inside the IF, so it only counts the numbers it adds.
If you couldn't trace on paper, extra output statements would find it too: put DISPLAY(total) and DISPLAY(count) inside the loop, run it, and you'd see "4 1, 4 2, 12 3" scroll by. As soon as count reads 2 while total is still 4, you know where to look. Delete those extra lines once the bug is fixed. A debugger does the same thing more comfortably: it pauses the program and lets you step through it one line at a time, watching every variable.
I thought about it. Show me
1PROCEDURE avgPositive(nums)2{3 total ← 04 count ← 05 FOR EACH n IN nums6 {7 IF (n > 0)8 {9 total ← total + n10 count ← count + 1111 }12 }13 IF (count = 0)214 {215 RETURN(0)216 }217 RETURN(total / count)18}
- line 10 Moved inside the IF: count only positives
- line 13-16 No positives at all? Do not divide by 0
- (a) Move count ← count + 1 inside the IF, right after total ← total + n. Retrace [4, -2, 8]: total 4, count 1; skip -2; total 12, count 2. It returns 12 ÷ 2 = 6.
Why?
Now count only counts the numbers that were added. - (b) Before dividing, check: IF (count = 0) { RETURN(0) }. With no positives, it returns 0 instead of dividing by zero.
Why?
This guard handles the unusual input. The program's requirements decide what to return; 0 is a reasonable choice. - (c) Tests: [4, -2, 8] expects 6 (mixed signs). [5] expects 5 (one element). [-1, -3] expects 0 (no positives). Each shows a different outcome, and the last is an extreme case.
Why?
Good tests cover each kind of outcome and the unusual inputs.
Answer: Move count inside the IF and add a count = 0 guard before dividing. Tests: [4, -2, 8] gives 6, [5] gives 5, [-1, -3] gives 0.
Stuck? Get a hint
- Which line runs for every element, even the ones the IF skips?
- What happens to total ÷ count when count is 0?
- Pick tests with different outcomes: mixed, single element, no positives.
count ← 1
FOR EACH t IN temps
{
IF (t ≥ 90)
{
count ← count + 1
}
}
DISPLAY(count)- AChange t ≥ 90 to t > 90
- BChange count ← 1 to count ← 0
- CMove DISPLAY(count) inside the loop
- DChange count ← count + 1 to count ← count + t
Show the answer
B. Two values, 92 and 90, are at least 90, so the answer should be 2. The count starts at 1 instead of 0, so every answer is one too high. Changing ≥ to > would make it display 2 for this list, but only by accident: it would then wrongly skip 90. Tracing shows the real cause.
- Aadding extra output statements
- Bpair programming
- Cdata compression
- Devent-driven programming
Show the answer
A. Temporarily displaying values while a program runs is the "adding extra output statements" technique for finding errors. It works like a trace table that the computer fills in for you.
- Ways to find and fix errors: test cases, hand tracing, visualizations, debuggers, extra output statements.
- A trace table has one column per variable and one row per step. Fill it in exactly; never guess.
- Fixing one bug can reveal another. Retest after every fix, including unusual inputs.
- Remove temporary output statements once the bug is found.