AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Words before code

Week 1 · 1.1 Pseudocode · 3 lessons
3

Bug hunters

Computing 9: Topic 1.1, pp. 11–24
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.

Lineabaverage
INPUT a (user types 6)6
INPUT b (user types 10)610
average ← a + b / 261011 (should be 8)
Worked examples
Pseudocode
1INPUT a
2INPUT b
3average ← (a + b) / 2
4OUTPUT "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 typedShould showAlgorithm showsRight?
8Child ticketChild ticketyes
12Child ticketAdult ticketno
15Adult ticketAdult ticketyes
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.
Pseudocode
1INPUT price1
2INPUT price2
3OUTPUT "Total: ", total
4total ← price1 + price2
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 / 3
5OUTPUT "Average rain: ", average
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
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 - 10
5ENDIF
6OUTPUT result * 2
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.