7.8 Identifying errors in algorithms

← Topic 7.7 Trace tables and dry runsComputer Science contentsTopic 7.9 Writing and amending algorithms →
Chapter 7 · Algorithm design and problem solving

7.8 Identifying errors in algorithms

Trace tables and carefully chosen test data do more than show what an algorithm does. They can expose errors, show exactly where a wrong result appears, and help you improve the algorithm so that it works for a wider range of inputs.

Trace tablesTest dataErrorsCorrectionsRobust algorithms

Using trace tables to find errors

The previous topic showed how a trace table records each variable whenever its value changes. The same technique can be used to identify and correct errors. You run the algorithm with suitable test data, compare the actual output with the expected result, and investigate any step that produces the wrong value.

The textbook revisits the algorithm that is intended to find the largest and smallest values from ten inputs. In its first version, the starting values are:

A ← 0
B ← 0
C ← 100

For the test data 400, 800, 190, 170, 300, 110, 600, 150, 130, 900, the trace table finishes with B = 900 and C = 100. B is correct, but C is wrong because the smallest input is 110.

ABCXOUTPUT
00100
1400400
2800800
3190
4170
5300
6110
7600
8150
9130
10900900
900  100
Why the error occurs: C begins at 100. Every input in this test set is greater than 100, so the condition X < C is never true. C therefore never changes, even though 100 was not one of the input values.

The same type of problem occurs with negative test data. If all ten values are negative, B begins at 0 and no negative input can satisfy X > B. The algorithm can therefore output 0 as the maximum even though 0 was never entered.

Can you spot why the original algorithm fails?

First improvement: use much wider starting limits

A simple improvement is to choose provisional values that are far outside the range expected in most test data. The textbook changes the starting values to B = -1,000,000 and C = 1,000,000.

Figure 7.16 flowchart of a better largest and smallest value algorithm
Figure 7.16 — A better algorithm

This version works for a much larger range of positive and negative numbers. A normal value is likely to be greater than -1,000,000, so B can be replaced by a real input. A normal value is also likely to be less than 1,000,000, so C can be replaced by a real input.

However, the algorithm is still not guaranteed to work for every possible set of numbers. If all values are less than -1,000,000, B could remain -1,000,000 even though that value was never entered. Likewise, if all values are greater than 1,000,000, C could remain 1,000,000.

Important idea: using bigger limits reduces the chance of failure, but it does not remove the underlying problem. The starting values are still artificial values chosen by the programmer rather than values taken from the actual data.
Test the improved version.

A robust solution: initialise from the first input

To make the algorithm work for any set of numbers, the provisional maximum and minimum should come from the data itself. The standard method shown in the textbook is to input the first value and assign it to both B and C.

Figure 7.17 flowchart of a more robust largest and smallest value algorithm
Figure 7.17 — A much better algorithm

The improved sequence is:

A ← 0
INPUT X
B ← X
C ← X

REPEAT
  INPUT X
  IF X > B
    THEN
      B ← X
  ENDIF
  IF X < C
    THEN
      C ← X
  ENDIF
  A ← A + 1
UNTIL A = 9

OUTPUT B, C

The first input is already counted by being placed directly into B and C. The loop therefore processes only the remaining nine values, which is why the counter is tested against 9 rather than 10.

This removes the arbitrary starting limits. Whether the values are all positive, all negative, very large, very small or mixed, both provisional values begin as a genuine member of the input list.

Testing the corrected algorithm

The textbook supplies this test set for the corrected version:

-97, 12390, 0, 77, 359, -2, -89, 5000, 21, 67

The correct maximum is 12390 and the correct minimum is -97. A dry run should confirm these values.

Check the corrected algorithm.

A reliable error-finding method

When an algorithm gives the wrong result, use a structured approach rather than guessing:

StepWhat to do
1State what the algorithm is supposed to do.
2Choose test data that can expose likely weaknesses, including unusual or extreme values.
3Work through the algorithm exactly as written using a trace table.
4Compare the actual output with the result you know should be produced.
5Find the first point where the trace stops behaving as expected.
6Change the algorithm to remove the cause of the error.
7Dry run the amended algorithm again, using several suitable test sets.

Topic 7.8 demonstrates why choice of test data matters. The original algorithm appeared to work with the earlier positive values between 0 and 100, but different data revealed that its initial values were unsafe assumptions.

Topic 7.8 revision checklist

Explain how trace tables and test data can identify algorithm errors.
Recognise why an initial minimum of 100 fails when every input is greater than 100.
Recognise why an initial maximum of 0 fails when every input is negative.
Explain why wider artificial limits reduce but do not eliminate the problem.
State why B = -1,000,000 and C = 1,000,000 can still fail.
Explain why setting maximum and minimum to the first input is more reliable.
Explain why only nine further values are processed after the first of ten inputs is used for initialisation.
Use a trace table to compare actual output with expected output.
Correct an algorithm and retest it using suitable test data.
Ready for a mixed Topic 7.8 check?
← Topic 7.7 Trace tables and dry runsComputer Science contentsTopic 7.9 Writing and amending algorithms →