Writing and Refining Algorithms
Correcting an algorithm means finding the errors in given pseudocode or program code and suggesting fixes. You are not rewriting it from scratch: you keep the intended purpose and repair each fault.
First read what the algorithm is meant to do, including what it should input, calculate and output. Then choose test values and work through the algorithm line by line, tracking every variable. Compare what actually happens with what should happen; each difference is a fault. Fix one fault at a time and check again with the same values, because a fix can create a new problem.
For example, a homework reward program should give 5 points for every piece of homework handed in, plus a bonus of 10 points once 8 or more pieces have been handed in. Test with 3 pieces: the expected total is 15 points. If it outputs 8, the program has added the points rate to the number of pieces instead of multiplying them. Then test 8 pieces: the expected total is 50 (8 × 5 + 10). That boundary value catches a condition such as more than 8 instead of 8 or more.
State the line number and a precise correction. The correction must remove the fault without introducing another. For instance, if an input value is used in arithmetic or compared as a number, cast it to an integer or float; if a condition uses =, change it to ==; if a loop stops too early, increase the range end or adjust the comparison. Check your correction against the intended purpose and test values, including a boundary value.
OCR GCSE Computer Science · Trace Tables
OCR GCSE Computer Science · Writing and Refining Algorithms