Find and fix the errors
"Identify the errors and correct them"
- A common Paper 2 question prints an algorithm with numbered lines and says it has, for example, four errors.
- For each error you give the line number and the corrected line. You do not rewrite the whole algorithm.
- The errors are small: a wrong starting value, a wrong comparison, a wrong loop limit, the wrong variable. They are found by tracing, not by guessing.
A method that finds them
- Read the question: write down what the algorithm should do and what it should output.
- Choose simple test data and work out the right answer by hand.
- Trace the algorithm line by line with that data, writing each variable's value.
- The first line where a value goes wrong holds an error. Fix it, then carry on tracing.
- Check the usual suspects: starting values (
0or the first item),<or>,<=or<, the loop limit, and the variable that is output.
Four errors in one algorithm
The algorithm should find the average and the highest of five temperatures.

- Line 01: the total must start at 0, or every answer is 1 too big.
- Line 03: the loop must visit all five days.
- Line 05: to find the highest, replace it when the new value is bigger.
- Line 10: after the loop, divide by the number of values, 5.
The corrected algorithm
DECLARE Temps : ARRAY[1:5] OF INTEGER
DECLARE Day, Total, Highest : INTEGER
DECLARE Average : REAL
Temps[1] ← 18
Temps[2] ← 23
Temps[3] ← 15
Temps[4] ← 27
Temps[5] ← 21
Total ← 0
Highest ← Temps[1]
FOR Day ← 1 TO 5
Total ← Total + Temps[Day]
IF Temps[Day] > Highest
THEN
Highest ← Temps[Day]
ENDIF
NEXT Day
Average ← Total / 5
OUTPUT "Average: ", Average, " Highest: ", Highest
- With the corrections the output is
Average: 20.8 Highest: 27, which matches the answer worked out by hand.
How to write the answer
| Line | Error | Correction |
|---|---|---|
| 01 | the total starts at 1 | Total ← 0 |
| 03 | the loop misses day 5 | FOR Day ← 1 TO 5 |
| 05 | the test finds the lowest | IF Temps[Day] > Highest |
| 10 | divides by the loop counter | Average ← Total / 5 |
- Write the whole corrected line, not just the changed symbol.
- Do not "fix" lines that are already correct: each wrong change can cost a mark.
Common mistakes
| Wrong | Right | Why |
|---|---|---|
| rewriting the whole algorithm | give the line number and the corrected line | the marks are for each error found |
| guessing from how the code looks | trace it with test data | a line can look strange and still be correct |
writing only 0 as the correction |
Total ← 0 |
the examiner needs the full line |
| stopping after the first error | keep tracing to the end | the question says how many errors there are |
| changing a correct line | leave it alone | an extra change can make a correct algorithm wrong |
Now you try
- Each task has a program with errors. Trace it, find each error, and correct the line.
Independent transfer
The final task gives a description only. Write a correct algorithm, then test it with data that includes zero and negative numbers.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
This program should count the marks of 50 or more. It has 二 errors. Trace it, then correct both lines so it outputs the right count.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
This program should output all five names. It has 一个 error. Find it and correct the line.
点击“运行”查看此处输出。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
In the lesson's algorithm, which line number made the program find the lowest temperature instead of the highest? Give the number only.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Read five whole numbers. Output how many of them are negative. Zero is not negative.
点击“运行”查看此处输出。