GoalFind and correct logic errors in pseudocode, using line-by-line reading and a table of variable values.
New words
logic error · a mistake in the steps of an algorithm: it runs, but the result is wrongdebug · to find the errors in an algorithm and correct themvariable table · a table that records the value in each variable after each line
Explanation
A logic error is a mistake in the steps themselves. The algorithm still runs, so there is no warning, but the result is wrong. To find it, first work out the correct answer for some test data. Then follow the algorithm in one of two ways. One way is to take the lines in turn and ask of each: does it do its job? The other is to draw a table with a column for each variable, fill in the new value after every line, and check each value. The first wrong value shows you the line with the error. Correct that line and then run all the test data again, because a repair can break a case that worked before. Common causes are a wrong operator, < instead of <=, AND instead of OR, a missing bracket, steps in the wrong order and a wrong variable name.
Line
a
b
average
INPUT a (user types 6)
6
INPUT b (user types 10)
6
10
average ← a + b / 2
6
10
11 (should be 8)
Worked examples
Pseudocode
1INPUT a
2INPUT b
3average ← (a + b) / 24OUTPUT "Average: ", average
The table above follows the average of two numbers. The line is wrong because division happens before addition, so only b is halved: 10 / 2 is 5 and 6 + 5 is 11. The correct average of 6 and 10 is 8. Brackets fix it: average ← (a + b) / 2. Test again with 6 and 10 (8) and with 3 and 4 (3.5).
Age typed
Should show
Algorithm shows
Right?
8
Child ticket
Child ticket
yes
12
Child ticket
Adult ticket
no
15
Adult ticket
Adult ticket
yes
A bug on the boundary. The rule is: a child ticket is for ages up to and including 12. The algorithm uses < 12, so a 12-year-old gets the adult ticket. Only the test with exactly 12 found it. The fix is age <= 12.
Steps in the wrong order. The line that shows the total comes before the line that works it out, so total has no value yet. In Python this program stops with NameError: name 'total' is not defined. The fix is to swap lines 3 and 4. A variable table would show an empty total at line 3.
Class activity
Bug swap (no computer). Each player writes a correct five-line algorithm on paper, for example a tip calculator or a swimming-lane chooser, and works out the right answer for two test inputs. Then they secretly add one bug: change an operator, swap two lines or remove brackets. Swap papers. Use a variable table drawn on paper to find the bug, say which line is wrong and why, and write the corrected line. Check the fix with both test inputs. If you have a computer or tablet, an adult can help you check your fixed algorithm in Python.
Practice
1
This algorithm should show the average rainfall of two days. The user types 4 and 8. Follow it, say what is wrong, and correct it.
Pseudocode
1INPUT monday
2INPUT tuesday
3total ← monday + tuesday
4average ← total / 35OUTPUT "Average rain: ", average
total is 4 + 8 = 12. average is 12 / 3 = 4, and the output is Average rain: 4. The correct average is 6, because there are two days, not three. Change line 4 to average ← total / 2.
2
The choir takes children who are at least 9 and at most 13. The user types 5, then 20. What does the algorithm show each time? Find the error and correct it.
Pseudocode
1INPUT age
2IF age >= 9 OR age <= 13 THEN
3 OUTPUT "Welcome"4ELSE
5 OUTPUT "Sorry"6ENDIF
5: 5 >= 9 is false but 5 <= 13 is true, and with OR one true part is enough, so it shows Welcome. 20: 20 >= 9 is true, so it shows Welcome. Every age is accepted. The error is OR; it must be AND, so both parts have to be true.
3
Draw a variable table for this algorithm and fill it in for the inputs 4, 10 and 15. Show the value of result after the IF and the output.
Pseudocode
1INPUT n
2result ← n
3IF n > 10 THEN
4 result ← n - 105ENDIF
6OUTPUT result * 2
n = 4: result is 4 after the IF (4 > 10 is false), output 8. n = 10: result is 10 (10 > 10 is false), output 20. n = 15: result is 15 − 10 = 5, output 10.
4
An algorithm gives the right answer for the first test data, and a learner says: ‘It ran without a message, so it is correct.’ Give two reasons why this is not good enough.
A logic error gives no message, so the program can run and still be wrong. One test only checks one case; other data, especially boundary values, may fail. Also, after a repair all the test data must be tried again.