2.1.1 Error Detection in Alorithm Workbook by Inqilab Patel
2.1.1 Error Detection in Alorithm Workbook by Inqilab Patel
Assessment at a glance
Components Weighting
Paper 1 Theory 1 hour 45 minutes 60%
This written paper contains short-answer and structured questions. All
questions are compulsory.
No calculators are permitted in this paper. 75
marks Externally assessed.
Paper 2 Problem-solving and Programming 1 hour 45 40%
minutes This written paper contains short-answer and structured
questions. All
questions are compulsory. 20 of the marks for this paper are from questions
set on the pre-release material. 1
No calculators are permitted in this paper. 50
marks Externally assessed.
About the developer of this workbook
Inqilab Patel is an O &A Level Computer Teacher. He has taught in many schools
including Yaqeen Model School, Karachi Cadet School, KN Academy, Beacon House
and The City School, PAF Chapter, Hexis A Level and Nakhlah Boys Campus
Society. Cambridge has selected him as a Member of Cambridge Editorial Review
Board. He is also associated with Aga Khan University Examination Board in the
capacity of Chief Examiner, Item Writer, E-Marker, Karachi Board of Secondary
Education the capacity of Deputy Head Examiner and Sindh Board of Technical
Education.
His entire career path revolves around computer science; either he was a student or a
teacher. He got a chance to polish his skills of teaching and studying more about
computers at various levels which has given him great confidence in presenting
himself for any senior level position of transferring his knowledge to the youth.
He has not stopped, he is continuing with his education at the higher levels. It is his
second semester of MPhil computer studies from a well-known university of Pakistan;
The Institute of Business & Technology.
Inqilab Patel knows a lot of methods of teaching computers and has developed
tutorial notes, worksheets and assignments for my students. He also maintains a
website (www.inqilabpatel.com) which is specifically designed for the support of those
who want to excel in GCSE computer science. He also regularly contributes material
to CIE teacher support website, for which he receives appreciation from different
people across the world.
He has also received various training in innovative and special methods of teaching
this subject.
Disclaimer
This workbook is developed by combining different materials related to Cambridge
IGCSE & O Level Computer Science 0478 & 2210. It is combination of work
developed by me and the resources, which are available in different web sites, books,
magazines, past papers and guides, just to facilitate students and teachers in
preparation for examinations.
Examination questions and marking schemes used in this workbook are taken from
CIE (Cambridge International Examinations)
Note
Study of at least one of the following books, is compulsory for solving this
Paper 2
Practical Problem-solving and Programming
Chapter 9
Algorithm
2.1.2 Algorithm Pseudo code
An algorithm is a series of
well defined steps which
gives a procedure for
solving a type of problem.
The word algorithm comes
from the name of 9th
century mathematician al-
Khwarizmi (Muhammad
Bin Musa Al-Khwarizmi). In
fact, even the word
algebra is derived from
his book “Hisab al-jebrw’al-muqabala”
2.1.2 Pseudo code
• understand and use pseudo code for assignment, using
• understand and use pseudo code, using the following conditional statements:
IF … THEN … ELSE … ENDIF
CASE … OF … OTHERWISE … ENDCASE
• understand and use pseudo code, using the following loop structures:
FOR … TO … NEXT
REPEAT … UNTIL
WHILE … DO … ENDWHILE
• understand and use pseudo code, using the following commands and statements:
INPUT and OUTPUT (e.g. READ and PRINT)
totalling (e.g. Sum Sum + Number)
counting (e.g. Count Count + 1)
(Candidates are advised to try out solutions to a variety of different problems on a
computer using a language of their choice; no particular programming language will
be assumed in this syllabus.)
Literals
Literals of the above data types are written as follows:
Integers: Written as normal in the denary system, e.g. 5, -3
Real: Always written with at least one digit on either side of the decimal point, zeros being
added if necessary, e.g. 4.7, 0.3, -4.0, 0.0
Char: A single character delimited by single quotes, e.g. x, C, @
String: Delimited by double quotes. A string may contain no characters (i.e. the empty
string) e.g. "This is a string", ""
Boolean: TRUE, FALSE
Variable:
Variable is memory location where a value can be stored. The values stored in a variable
are changed during execution.
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mix
case. They can only contain letters (A–Z, a–z) and digits (0–9). They must start with a letter
and not a digit. Accented letters and other characters, including the underscore, should not
be used.
As in programming, it is good practice to use identifier names that describe the variable,
procedure or function they refer to. Single letters may be used where these are
conventional (such as i and j when dealing with array indices, or X and Y when dealing with
coordinates) as these are made clear by the convention.
Identifiers should be considered case insensitive, for example, Countdown and Countdown
should not be used as separate variables.
Assignments
Storing values in a variable is known as assignment . The assignment operator is .
Assignments should be made in the following format:
<identifier> <value>
For example:
Counter 0
Counter Counter + 1
TotalToPay NumberOfHours * HourlyRate
Variable declarations
It is good practice to declare variables explicitly in pseudo code.
Declarations are made as follows:
DECLARE<identifier> : <data type>
Example
DECLARE Surname : STRING
DECLARE FirstName : STRING
DECLARE DateOfBirth : DATE
DECLARE Section : CHAR
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
Constant:
Constant is memory location where a value can be stored but the stored value remaining
same during execution.
It is good practice to use constants if this makes the pseudo code more readable, as an
identifier is more meaningful in many cases than a literal. It also makes the pseudo code
easier to update if the value of the constant changes.
Constant declaration
Constants are normally declared at the beginning of a piece of pseudo code (unless it is
desirable to restrict the scope of the constant).
Constants are declared by stating the identifier and the literal value in the following format:
CONSTANT<identifier> = <value>
Example
CONSTANT HourlyRate = 6.50
CONSTANT DefaultText = “N/A”
Only literals can be used as the value of a constant. A variable, another constant or an
expression must never be used.
Arithmetic operations
Standard arithmetic operator symbols are used:
• + Addition
• - Subtraction
• * Multiplication
• / Division
Care should be taken with the division operation: the resulting value should be of data type
REAL, even if the operands are integers.
The integer division operators MOD and DIV can be used. However, their use should be
explained explicitly and not assumed.
Multiplication and division have higher precedence over addition and subtraction (this is the
normal mathematical convention). However, it is good practice to make the order of
operations in complex expressions explicit by using parentheses.
Logic operators
The only logic operators (also called relational operators) used are AND, OR and NOT. The
operands and results of these operations are always of data type BOOLEAN.
In complex expressions it is advisable to use parentheses to make the order of operations
explicit.
Counting
It is sometimes necessary to count how many times something happens.
To count up or increment by 1, we can use statements such as:
Count Count + 1 INCREMENT Count by 1
Totaling
To keep a running total, we can use a variable such as Total or Sum to hold the
running total and assignment statements such as:
Total Total + Number ADD Number to Total
Chapter 13
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.
2 Product🡨 1
3 WHILE Count<= 10 DO
4 INPUT Num
5 Product 🡨 Product * Num
6 Count 🡨 Count + 1
7 ENDWHILE
8 PRINT Product
Example:
20 Total 🡨 1
30 Product 🡨 0
40 Highest 🡨 0
50 Lowest 🡨 0
Example:
10 Count 🡨 0
20 REPEAT
30 INPUT Num
40 Sum 🡨 Sum + Num
50 UNTIL Sum>=100
60 PRINT Sum
Error 3: Increment in loop Counter in FOR…TO…NEXT loop.
FOR…TO…NEXT loop doesn‟t need increment in loop counter.
Example:
10 Sum 🡨 0
20 FOR
Count 🡨 1 TO 500
30 INPUT Num
40 Sum 🡨 Sum + Num
50 Count 🡨 Count + 1
60 NEXT Count
70 PRINT Sum
Example:
10 Highest 🡨 0
20 FOR Count 🡨 1 TO 5
30 INPUT Num
40 IF Num >Highest THEN Highest 🡨 Num
50 Count 🡨 Count + 1
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.
Highest 🡨 0
Example
Lowest 🡨 1000
Highest 🡨 0
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 each error.
1. ......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[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.
13.11 Summer 20162210,0478 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]
13.12 Specimen paper 20162210,0478 P2
4 Read this section of program code that inputs twenty (20) numbers and then outputs the
largest number input.
1h=0
2c=0
3 REPEAT
4 READ x
5 IF x > h THEN x = h
6 c=c+1
7 PRINT
h 8 UNTIL c <
20
There are three errors in this code.
Locate these errors and suggest a corrected piece of code.
1: ......................................................................................................................................
2: ......................................................................................................................................
3: ................................................................................................................................. [3]
13.13 Winter 2016 P21-23
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
(a) 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 ..................................................................................................................................
...............................................................................................................................................[4]
(b) Decide, with reasons, whether the numbers 10 and 20 are within or outside the range. [4]
Withi Outsid
Numb n range e range Reas
er (✓) (✓) on
……………………………………………………………………
10
..
……………………………………………………………………
..
……………………………………………………………………
20
..
……………………………………………………………………
..
13.14 Winter 2016 P22
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
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]
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
2 INPUT Age
3 WHILE Age >= 0 DO
4 IF Age >= 18 THEN
5 Num18 = Num18 + Age
6 ENDIF
7 ENDWHILE
8 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]
13. PAGE 16 Summer 2017 P21
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
(a) Find the error in the pseudo code and suggest a correction.
Error......................................................................................................................................
Correction ............................................................................................................................
.........................................................................................................................................[2]
(b) Rewrite the correct algorithm using a more suitable loop structure.
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
........................................................................................................... [3]
13.18 Winter 2017 P21
2 This section of program code asks for 80 numbers between 100 and 1000 to be entered.
It checks that the numbers are in the correct range, and stores them in an array. It counts
how many of the numbers are larger than 500 and then outputs the result when the
program is finished.
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'
There are four lines of code that contain
errors.
State the line number for each error and write the correct code for that line.
Error 1 Line Number .............................
Correct Code ..........................................................................................................................
Error 2 Line Number .............................
Correct Code .........................................................................................................................
Error 3 Line Number .............................
Correct Code ..........................................................................................................................
Error 4 Line Number .............................
Correct Code .......................................................................................................................[4]
13.19 March 2018 P22 (India)
2 An algorithm has been written in pseudo code to input some numbers and print out any
numbers that are greater than or equal to 100. The number 999 stops the algorithm.
INPUT Number
WHILE NUMBERS <> 999 DO
IF Number > 100 THEN PRINT Number ENDIF
ENDWHILE
PRINT Number
(a) Find the four errors in the pseudo code and suggest corrections.
Error 1 .....................................................................................................................................
Correction ..............................................................................................................................
...............................................................................................................................................
Error 2 ..................................................................................................................................
Correction ............................................................................................................................
.............................................................................................................................................
Error 3 .................................................................................................................................
Correction .............................................................................................................................
..............................................................................................................................................
Error 4 ................................................................................................................................
Correction ...........................................................................................................................
............................................................................................................................................ [4]
(b) Show, using pseudo code, how you would change the corrected algorithm to print
out any numbers between 100 and 200 inclusive.
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
.........................................................................................................................................[2]
Comments on Question 2
(a) Most candidates correctly identified one or two errors. A few candidates showed good u
nderstanding of the pseudo code by correctly identifying the problem with the variable
name and the need to add INPUT Number before ENDWHILE. A common error was to
suggest th at the WHILE condition was incorrect.
(b) Some candidates realised that as well as introducing an upper bound, there was a chan
ge required to the value of the lower bound of the selection test, as the number 100 would
n ow be included.
x PIN REPEAT
x x/10 c c + 1
UNTIL x < 1
IF c <> 5THEN
PRINT “error in PIN entered”
ELSE
PRINT “PIN OK”
ENDIF
(a) What value of c and what message would be
output if the following PINs were entered?
5 1 0 2 0 Value of c: 5
Message: PIN OK
5 1 2 0 Value of c: 4 .
Message: “error in PIN entered” [2]
(b) What type of validation check is being carried out here?
Length check (Checks number of digits in PIN. [1]
13.22Winter 2002
Read this algorithm.
set Total_1 to zero
set Total_2 to zero
set Counter to one
while Counter < eight
Counter = Counter + 1
input Number
if Number > zero then Total_1 = Total_1 + Number
if Number < zero then Total_2 = Total_2 + Number
endwhile
output Total_1
output Total_2
(a) Write down the output if the following set of numbers are input. 4, 1, -3, 2, -5, 0, 6
................................................................................................................................ [2]
C PIN X OUTPUT
0
51020 51020
1 5102
2 510.2
3 51.02
4 5.102
5 0.510
2
PIN OK
(b) Modify the algorithm so that it will accept any number of numbers, the input is
terminated by a rogue value and the output is the Total of all the numbers input except
the rogue value.
................................................................................................................................ [4]
13.23 Summer 2003
Read this algorithm.
input A, B
if A > B then
T=A
A =
B B
=T
endif
output A,
B
(a) Write down the output if the following two numbers are input:
41, 38 ...............................................................................................................................[1]
(b) Explain the purpose of the variable T ....................................................................... [1]
(c) Explain why an algorithm is written as a subroutine (procedure) and stored in a program
library. ...............................................................................................................[2]
13.24 Winter 2003
The following algorithm inputs air speeds (which must be in multiples of 100) and outputs a
suitable message.
1 input a speed
2 whole = speed/100
3 case whole of
4 0,1,2 : result = slow
5 3, 4, 5, 6 : result = normal
8 7, 8, 9 : result = high
7 otherwise whole = -1
8 endcase
9 if whole = -1 then
10 output “abnormal
reading” 11 else output result,
“speed”
Dry run the above algorithm for the following Input data and complete the Output column
in the table: [3]
Input Output
150
400
800
State what would be happen if line 2 had been missed out of the algorithm?
..................................................................................................................................................
.................................................................................................................................. [2]
13.25
(b) Write an algorithm which uses a While..Do..End while loop and outputs the numbers 2,
4, 6 and 8[3]
Marking Scheme
Q 13.1) Winter 2014 P13