Exam papers often give you an algorithm that almost works, with a few lines wrong. Your job is to find each error, give its line number and write the corrected line. Most errors fall into a handful of familiar types, so learn to look for them.
To find errors, first work out the algorithm's intended purpose, then dry-run it with simple test data and compare the actual result with the expected result. Common errors are a wrong operator (< for >), counting instead of totalling, a wrong initial value or one set inside the loop, wrong loop bounds, a missing input inside a loop and a swap without a temporary variable. For each error give the line number and the corrected line of pseudocode.
Try the find-the-error tab in the lab. Each program has a stated purpose and a mistake; run it with test data, see the wrong result, then fix the line.
| Error type | Faulty line | Corrected line |
|---|---|---|
| Counting instead of totalling | Total ← Total + 1 | Total ← Total + Mark |
| Wrong relational operator | IF Mark < Highest | IF Mark > Highest |
| Initial value inside the loop | Total ← 0 inside the FOR loop | Move Total ← 0 before the loop |
| Wrong loop bounds | FOR Count ← 0 TO 10 (11 passes) | FOR Count ← 1 TO 10 |
| Missing input in a loop | No INPUT before ENDWHILE | Add INPUT Number inside the loop |
| Wrong divisor | Average ← Total / 9 for 10 values | Average ← Total / 10 |
"Line 5 is wrong" earns no mark on its own. Give the line number and the full corrected statement in pseudocode.
This algorithm should input 10 positive numbers, then output the highest number and the average. It contains three errors.
01 Highest ← 0 02 Total ← 0 03 FOR Count ← 1 TO 10 04 INPUT Number 05 Total ← Total + 1 06 IF Number < Highest 07 THEN 08 Highest ← Number 09 ENDIF 10 NEXT Count 11 Average ← Total / 9 12 OUTPUT Highest, " ", Average
Do not rewrite the whole algorithm. Change only the faulty lines, and keep the original variable names.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
What is the best first step when looking for errors in an algorithm?
An algorithm meant to total marks has Total ← Total + 1. What does it actually calculate?
Why is Total ← 0 inside a FOR loop an error?
A loop is written FOR Count ← 0 TO 10 but should run 10 times. How many times does it run?
Some errors make a loop run the wrong number of times, or never stop. Ask: what changes the loop condition, and does it ever change?
01 Total ← 0 02 OUTPUT "Enter a number, -1 to stop" 03 INPUT Number 04 WHILE Number <> -1 DO 05 Total ← Total + Number 06 ENDWHILE 07 OUTPUT Total
Values[I] ← Values[I + 1] then Values[I + 1] ← Values[I]. What goes wrong, and what is the fix?A loop condition that uses a variable never changed inside the loop is a warning sign: either the loop never runs, or it never ends.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
An algorithm should find the smallest of 20 values but starts with Smallest ← 0. All values are positive. What is output?
Which change corrects WHILE Number = -1 DO in a loop meant to stop when -1 is entered?
A WHILE loop processes inputs until -1, but never asks for another input inside the loop. What happens if the first input is 8?
Which line correctly averages 12 values stored in Total?
This skill is now recorded on your profile. Keep the chain going.