2.5 PsedoCodeErrors
2.5 PsedoCodeErrors
2.5 PsedoCodeErrors
Chapter 16
A syntax error is a 'grammatical' error, in which a program statement does not follow the rules of
the high-level language constructs. Due to syntax error program code can’t be executed.
Logic error: an error in the logic of the solution that causes it not to behave as intended. Due to
logical error a program is executed but doesn’t produce required result.
Run-time error: an error that causes program execution to crash or freeze. E.g. divide-by-zero
error.
It is important to be able to identify errors and suggest corrections in a pseudo code algorithm.
If algorithm is correct but less efficient, students are asked to suggest improvements.
When task is changed, students are asked to modify pseudo code.
In loops following should points be considered:
Count-controlled loop (FOR…TO…NEXT loop) should be used if number of repetition is
given. For example input marks for 30 students,
FOR Count=1 TO 30
INPUT marks
NEXT Count
Example: Example:
10Sum 0 10 Highest 0
20 FOR Count 1 TO 500 20 FOR Count 1 TO 5
30 INPUT Num 30 INPUT Num
40 Sum Sum + Num 40 IF Num>Highest THEN Highest
50 Count Count + 1 Num
60 NEXT Count 50 Count Count + 1
70 PRINT Sum 60 NEXT Count
70 PRINT Highest
If Final output like greatest value or average is required it should be after loop.
IF running output is required it should be inside loop.
Example
Lowest 1000
Highest 0
FOR Counter 1 TO 100
INPUT Number
IF Number < Highest THEN Highest Number
IF Number > Lowest THEN Lowest Number
NEXT Counter
PRINT Highest, Lowest
Examination Questions
Q 13.1) Winter 2014 P13
The following pseudo code algorithm should:
COMPUTER SCIENCE WITH Ali Raza
• input up to 20 numbers
• stop if the sum of the input numbers exceeds 50
• output the final sum
10 count = 0
20 REPEAT
30 INPUT n
40 n + sum = sum
50 IF sum = 50 THEN count = 20
60 count = count + 1
70 UNTIL count = 20
80 OUTPUT n
There are five errors in this algorithm.
Locate these errors and suggest a correction.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 5 ................................................................................................................................................
Correction ...........................................................................................................................................
......................................................................................................................................................[5]
COMPUTER SCIENCE WITH Ali Raza
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
....................................................................................................................................................... [4]
Examiner’s comments on Question 2
Most candidates located at least one error and suggested a suitable piece of corrected code. The
error on line seven was the one identified and corrected by nearly all candidates. The error on line
3 was often identified, with better candidates providing a working correction.
COMPUTER SCIENCE WITH Ali Raza
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
There are four errors in this code. Locate these errors and suggest code corrections to remove eac
h error.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
....................................................................................................................................................... [4]
Examiners Comments Question 2
Many candidates located at least one error and suggested a suitable piece of corrected code. The errors on lines 4 and 5 were
frequently identified, with stronger responses providing a working correction. The question asked the candidates to identify and
correct each error; a few candidates either identified the error or corrected the error, but both actions were required to gain each
mark.
COMPUTER SCIENCE WITH Ali Raza
13.11 Summer 2016 P22
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
This code works, but it is inefficient.
(i) Suggest three improvements that could be made.
1 ..........................................................................................................................................
...........................................................................................................................................
2 ..........................................................................................................................................
.............................................................................................................................................
3 ..........................................................................................................................................
.........................................................................................................................................[3]
(ii) Rewrite the program code with your improvements.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[3]
COMPUTER SCIENCE WITH Ali Raza
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
There are four errors in this code.
Locate these errors and suggest a correction to remove each error.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
....................................................................................................................................................... [8]
COMPUTER SCIENCE WITH Ali Raza
13.15 March 2017 P21 (India)
2 Read this section of code that inputs the ages of people entering an event. The input sequence
is ended by inputting a negative value for age. The code outputs the number of people at the
event over the age of 18.
01 Num18 = 0
02 INPUT Age
03 WHILE Age >= 0 DO
04 IF Age >= 18 THEN
05 Num18 = Num18 + Age
06 ENDIF
07 ENDWHILE
08 PRINT Num18 – Age
There are four errors in this code.
Locate these errors and suggest code correction to remove each error.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
...................................................................................................................................................... [4]
COMPUTER SCIENCE WITH Ali Raza
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
...................................................................................................................................................... [4]
COMPUTER SCIENCE WITH Ali Raza
13.17 Summer 2017 P22
4 An algorithm has been written in pseudo code to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.
Count ← 0
Sum ← 0
REPEAT
INPUT Number
Sum ← Sum + Number
Count ←Count + 1
UNTIL Count > 100
PRINT Sum
(a) Find the error in the pseudo code and suggest a correction.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
......................................................................................................................................................[2]
(b) Rewrite the correct algorithm using a more suitable loop structure.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [3]
COMPUTER SCIENCE WITH Ali Raza
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
Comments on Question 2
(a) Most candidates correctly identified one or two errors. A few candidates showed good understanding of the pseudo
code by correctly identifying the problem with the variable name and the need to add INPUT Number before ENDWHIL
E. A common error was to suggest that the WHILE condition was incorrect.
(b) Some candidates realised that as well as introducing an upper bound, there was a change required to the value of t
he lower bound of the selection test, as the number 100 would now be included.
COMPUTER SCIENCE WITH Ali Raza
Find the four errors in the pseudocode and suggest a correction for each error.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
...................................................................................................................................................... [4]
(b) Show how you would change the corrected algorithm to only total numbers greater than 0and
less than 20.
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [2]
COMPUTER SCIENCE WITH Ali Raza