Error Detection & Code Improvements
Error Detection & Code Improvements
STUDENTS NAME:
_____________________________________________
_____________________________________________
_____________________________________________
Follow On Facebook :
https://www.facebook.com/shahza
CONTACT NUMBER: dah.ashraf
_____________________________________________
1 Small = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
6 Counter = Counter + 1
7 PRINT Small
Locate these errors and suggest a corrected piece of code for each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
2210/21/M/J/15
2 Read this section of program code that should input 30 positive numbers and then output the
largest number input.
1 Large = 9999
2 Counter = 0
4 DO
5 INPUT Num
7 Counter = Counter - 1
8 ENDWHILE
9 PRINT Large
Locate these errors and suggest a corrected piece of code for each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
1 Small = 1000
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Small = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Small
(i) Identify three changes you would need to make to find the largest number input instead
of the smallest number.
1 .........................................................................................................................................
...........................................................................................................................................
2 .........................................................................................................................................
...........................................................................................................................................
3 .........................................................................................................................................
.......................................................................................................................................[3]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[3]
2210/21/M/J/16
2 Read this section of program code that inputs 10 positive numbers and then outputs the total.
1 Total = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 Total = Total + Num
6 PRINT Total
7 Counter = Counter + 1
8 UNTIL Counter = 10
1 .................................................................................................................................................
...................................................................................................................................................
2 .................................................................................................................................................
...................................................................................................................................................
3 .................................................................................................................................................
...............................................................................................................................................[3]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
2210/22/M/J/16
2 This section of program code asks for 50 numbers to be entered. The total and average of the
numbers are calculated.
1 Total = 0
2 Counter = 50
3 PRINT ′When prompted, enter 50 numbers, one at a time′
4 REPEAT
5 PRINT ′Enter a number′
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT ′The average of the numbers you entered is ′, Average
State the line number for each error and write the correct code for that line.
Count ← 0
Sum ← 0
REPEAT
INPUT Number
Sum ← Sum + Number
Count ← Count + 1
UNTIL Count > 100
PRINT Sum
Error ...........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
[2]
(b) Rewrite the correct algorithm using a more suitable loop structure.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
2210/22/M/J/17
2 Read this section of program code that should input 50 numbers and then output the average.
1 Total = 0
2 For Counter = 1 TO 50
3 INPUT Num
4 Total = Total + 1
5 Counter = Counter + 1
6 Average = Total/Counter
7 NEXT Counter
8 PRINT Average
Locate these errors and suggest code corrections to remove each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
2210/22/O/N/15
2 Read this section of program code that should input 50 numbers and then output the average of
the positive numbers only.
1 Total = 0
2 PosCount = 0
3 FOR Counter = 1 TO 50
4 INPUT Num
7 Average = Total/PosCount
8 NEXT Counter
9 PRINT Num
Locate these errors and suggest code corrections to remove each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
2210/23/O/N/15
2 Read this section of program code that inputs positive numbers, discards any negative numbers
and then outputs the average. An input of zero ends the process.
1 Total = 0
2 Counter = 100
3 REPEAT
4 REPEAT
5 INPUT Num
6 UNTIL Num < 0
7 Total = Total + 1
8 Counter = Counter + Num
9 UNTIL Num = 0
10 Average = Total / (Counter - 1)
11 Print Average
Error 1 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 2 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 3 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 4 ..............................................................................................................................................
Correction .........................................................................................................................................
..................................................................................................................................................... [8]
2210/22/O/N/16
2 Read this section of program code that:
• inputs 10 numbers
• checks whether each number is within a specified range
• totals the numbers within the range and outside the range
1 InRange = 0
2 OutRange = 1000
3 FOR Count = 1 TO 10
4 INPUT Num
5 IF Num > 10 AND Num < 20 THEN InRange = InRange + 1
6 ELSE OutRange = OutRange - 1
7 Count = Count + 1
8 NEXT X
9 PRINT InRange, OutRange
Error 1 ........................................................................................................................................
Correction ..................................................................................................................................
...................................................................................................................................................
Error 2 ........................................................................................................................................
Correction ..................................................................................................................................
...................................................................................................................................................
Error 3 ........................................................................................................................................
Correction ..................................................................................................................................
...................................................................................................................................................
Error 4 ........................................................................................................................................
Correction ..................................................................................................................................
...............................................................................................................................................[4]
2210/23/O/N/16
(b) Decide, with reasons, whether the numbers 10 and 20 are within or outside the range.
Within Outside
Number Reason
range (✓) range (✓)
10
……………………………………………………………………..
……………………………………………………………………..
20
……………………………………………………………………..
……………………………………………………………………..
[4]
1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'
State the line number for each error and write the correct code for that line.
1 Total = 100.00
2 PRINT ′Enter the height of each member of your class, one at a
time, when prompted′
3 FOR Count = 1 TO 30
4 PRINT ′Enter a height in metres′
5 INPUT Height
6 Total = Total + Height
7 PRINT Total / 30
8 Count = Count + 1
9 NEXT Count
State the line numbers that contain the errors and describe how to correct each error.
Error 1 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Error 2 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Error 3 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
[3]
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[1]
2210/23/O/N/18
Page 5 Mark Scheme Syllabus Paper
Cambridge O Level – May/June 2015 2210 21
1 Large = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num > Large THEN Large = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Large
[3]
Page 4 Mark Scheme Syllabus Paper
Cambridge O Level – May/June 2016 2210 22
2 1 mark for each error identified and suggested correction (the corrected 4
code must be written in full)
« use of NEXT
-
line 7 Average = Total/Poscount: this should come after the end of the repeat loop
2 1 mark for identifying each error, 1 mark for the corresponding change
Counter = 0
(b)
… … … … … … … … … … … … … … … … … …
[4]
2 1 mark for each error identified plus suggested correction (the corrected 4
lines must be written in full)
Line 8 or Count = Count + 1: correction This line should be removed (not required in a FOR loop) // use of
REPEAT…UNTIL or WHILE…DO…ENDWHILE
Line 7 or PRINT Total /30: correction This line should be outside the loop (or it will print each iteration)