0% found this document useful (0 votes)
39 views51 pages

2.1.1 Error Detection in Alorithm Workbook by Inqilab Patel

The document provides information about algorithms, pseudo code, and programming concepts. It defines key terms like algorithm, pseudo code, variables, constants, data types, and control structures. Examples are given for different pseudo code constructs like variable declarations, assignments, and constant declarations.

Uploaded by

m.bilal.affan00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views51 pages

2.1.1 Error Detection in Alorithm Workbook by Inqilab Patel

The document provides information about algorithms, pseudo code, and programming concepts. It defines key terms like algorithm, pseudo code, variables, constants, data types, and control structures. Examples are given for different pseudo code constructs like variable declarations, assignments, and constant declarations.

Uploaded by

m.bilal.affan00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Contents

Syllabus content & assessment at a glance 4


About the developer of this workbook 5
Disclaimer 6
Algorithm 8
Errors in Pseudo code and Program 13
Finding output from pseudo code 37
Finding output from pseudo code 41
Syllabus content & assessment at a glance
Sections Topics
Section 1 Theory of Computer Science
1.1 Data representation
1.1.1 Binary systems
1.1.2 Hexadecimal
1.1.3 Data storage
1.2 Communication and Internet technologies
1.2.1 Data transmission
1.2.2 Security aspects
1.2.3 Internet principles of operation
1.3 Hardware and software
1.3.1 Logic gates
1.3.2Computer architecture and the fetch-execute cycle
1.3.3 Input devices
1.3.4 Output devices
1.3.5 Memory, storage devices and media
1.3.6 Operating systems
1.3.7High- and low-level languages and their translators
1.4 Security
1.5 Ethics
Section 2 Practical Problem-solving and Programming
2.1 Algorithm design and problem-solving
2.1.1 Problem-solving and design
2.1.2 Pseudocode and flowcharts
2.2 Programming
2.2.1 Programming concepts
2.2.2 Data structures; arrays
2.3 Databases

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.)

“An algorithm is a sequence of steps for a computer program to accomplish a


task.”
In general, an 'algorithm' is the name given to a defined set of steps used to complete a
task.
For instance you could define an algorithm to make a cup of tea. You start by filling the
kettle, and then place a tea bag in the cup and so on.
In computer terms, an algorithm describes the set of steps needed to carry out a software
task. This mini-web takes you through the topic of algorithm
The concept of a program
A program is a sequence of instructions or programming language statements written to
make a computer perform certain tasks.
Well-structured programs require a programming language to support the following program
constructs:
1. Sequence
2. Assignment
3. selection
4. iteration
A computer‟s processor can only run a computer program in the form of a file of machine
code, which is a sequence of binary codes representing instructions for the processor.
The instruction set for a family of processors is the machine language in which machine
code is written for that family of processors.
When machine code runs, the processor repeatedly:
⮚ Fetches an instruction from internal memory

⮚ Decodes the instruction

⮚ Executes the instruction.


Pseudo Code Guide

Atomic type names


The following keywords are used to designate atomic data types:
INTEGER: A whole number (without fractional part)
REAL: A number capable of containing a fractional part
CHAR: A single character
STRING: A sequence of zero or more
characters BOOLEAN: The logical values TRUE
and FALSE DATE:A valid calendar date

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.

Keywords identified elsewhere in this guide should never be used as variables.

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.

Input and output


Values are input using the INPUT command as follows:
INPUT <identifier>
The identifier should be a variable (that may be an individual element of a data structure
such as an array, or a custom data type).

Values are output using the OUTPUT command as follows:


OUTPUT <value(s)>
Several values, separated by commas, can be output using the same command.

Example – INPUT and OUTPUT statements


INPUT Answer
OUTPUT Score
OUTPUT "You have ", Lives, " lives left"
Note that the syllabus for IGCSE (0478) gives READ and PRINT as examples for INPUT
and OUTPUT, respectively.

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

Errors in Pseudo code and Program


There are commonly three types of errors found in program codes:

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.

Finding and correcting errors in pseudo code algorithm to Improve Efficiency

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

⮚ Pre-conditioned loop (WHILE…DO…ENDWHILE loop) should be used if loop is


checked at the beginning and condition to continue the loop is given. For example to
input only positive numbers, the numbers are validated at the time of input, when
number is invalid, they are repeatedly input.
INPUT Number
WHILE Number<0 DO
PRINT “Invalid number “
INPUT Number
ENDWHILE
⮚ Post-conditioned loop (REPEAT…UNTIL loop) should be used if loop is based upon
a condition, but it has to be repeated at least once, and then condition to stop loop is
checked. For example to input numbers, and calculate total until a rogue value like 0 is
typed.
INPUT Number
REPEAT
Total 🡨Total + Number
INPUT Number
UNTIL Number=0
Common Errors in pseudo code:
There are 8 types of errors in pseudo code:

Error 1: Faulty initial or final value of loop counter


IF Count is initialized with 0 then Count< „number of iteration‟ should be used in WHILE
loop.
IF Count is initialized with 1 then Count<= „number of iteration‟ should be used in WHILE
loop.
Example: A computer program is required which inputs 10 numbers, finally outputs the
answer (the product).
1 Count 🡨 0

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:

Error 2: Missing or Faulty initialization of a variable:


A variable must be initialized if it used in calculation without INPUT.
Total is initialized with 0, Product with 1, Highest with lowest possible value and Lowest with
highest possible value.
Example:
10 Count🡨0

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

Error 4: Missing increment in loop Counter in REPEAT…UNTIL or WHILE…


DO…ENDWHILE loop.
REPEAT…UNTIL loop and WHILE..DO..ENDWHILE loop needs increment in loop counter.

Error 5: Misplacing statement inside or outside of loop:

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.

Error 6: Missing ending keywords.


REPEAT…UNTIL or WHILE…DO…
ENDWHILE FOR…TO…NEXT IF…
THEN…ENDIF
CASE ….OF…OTHERWISE….ENDCASE
Error 7: Assignment Error.
Values or vales of variable at right side should be assigned to variables and constants at left
side.
Example
Lowest 🡨 1000

Highest 🡨 0

FOR Counter 🡨 1 TO 100


INPUT Number
IF Number > Highest THEN Number 🡨 Highest

IF Number < Lowest THEN Number 🡨 Lowest


NEXT Counter
PRINT Highest, Lowest

Error 8: Operator Error.


A common error in pseudo code is an improper operator.

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:
• 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]
Q 13.2) Summer 2005
The following algorithm contains an error.
1. SET X = 1
2. REPEAT
3. X = X + 2
4. Print X
5. UNTIL X = 10
(a) Trace the algorithm and explain what the error is.
....................................................................................................................................... [2]
Q 13.3) Winter 2006
A computer program is required which inputs 10 numbers, multiplies them together and
finally outputs the answer (the product). The following algorithm has been written to do this.
1 count = 0
2 product = 0
3 while count<= 10 do
4 input number
5 product = product * number
6 count = count + 1
7 print product
8 endwhile
(a) There are three errors in the algorithm. Locate and describe these errors.
error 1 ...........................................................................................................................
correction ......................................................................................................................
.......................................................................................................................................
error 2 ...........................................................................................................................
correction ......................................................................................................................
.......................................................................................................................................
error 3 ...........................................................................................................................
correction ......................................................................................................................
.......................................................................................................................................
A while do loop has been used in the algorithm. State another type of loop that could have
been used.
.......................................................................................................................................
.......................................................................................................................................
Q 13.4) Winter 2010
The following algorithm inputs 20 numbers and outputs how many numbers were
positive (> 0) and how many numbers were negative (< 0).
1 negative = 1
2 positive = 1
3 for count = 1 to 20 do
4 input number
5 if number < 0 then negative = negative + 1
6 if number > 0 then positive = positive + 1
7 count = count + 1
8 print negative, positive
9 next count
There are three different errors in this algorithm.
Locate each error and give the reason why you think it is an error.
Error 1: ………………………………………………………………………………………..
Correction 1: ………………………………………………………………………………….
Error 2: ………………………………………………………………………………………..
Correction 2: ………………………………………………………………………………….
Error 3: ………………………………………………………………………………………..
Correction 3: ………………………………………………………………………… [6]
Q 13.5) Summer 2011
Read the following section of 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.
Error 1: ………………………………………………………………………………………..
Correction 1: ………………………………………………………………………………….
Error 2: ………………………………………………………………………………………..
Correction 2: ………………………………………………………………………………….
Error 3: ………………………………………………………………………………………..
Correction 3: [6]
Q 13.6) Winter 2013
A piece of pseudo code was written to input 1000 positive numbers and then output the
highest and lowest numbers.
10 highest = 0
20 lowest = 0
30 for count = 1 to 100
40 input number
50 if number > highest then number = highest
60 if number < lowest then number = lowest
70 count = count + 1
80 next count
90 print highest, lowest
There are errors in the code.
Locate these errors and suggest a correction.
error 1 .........................................................................................................................
correction: ...................................................................................................................
.....................................................................................................................................
error 2: ........................................................................................................................
correction: ...............................................................................................................
.................................................................................................................................
error 3: .................................................................................................................
correction: ................................................................................................................
.................................................................................................................................
error 4: ...................................................................................................................
correction ..................................................................................................................
..................................................................................................................................
Q 13.7) Winter 2014 P12
The following section of a pseudo code algorithm should:
• input 500 numbers
• generate a ratio called k
• output each value of k
• output how many numbers were larger than 10
10 total = 1
20 FOR x = 1 TO 500
30 IF number < 10 THEN total = total + 1
40 k = x / number
50 x=x+1
60 OUTPUT
k 70 NEXT x
80 OUTPUT x
(a) There are five errors in the above code.
Locate these errors and suggest a correction.
error 1 ..................................................................................................................................
correction: ...........................................................................................................................
.............................................................................................................................................
error 2: .................................................................................................................................
correction: ...........................................................................................................................
.............................................................................................................................................
error 3: .................................................................................................................................
correction: ...........................................................................................................................
.............................................................................................................................................
error 4: .................................................................................................................................
correction ...........................................................................................................................
.............................................................................................................................................
error 5: .................................................................................................................................
correction ...........................................................................................................................
.............................................................................................................................................
(b) The corrected algorithm was converted to a computer program and run. However,
after several numbers were input, the program stopped and an error message was
generated, showing that there was a further error at line 40 (k = x / number).
State what could cause this error to occur.
.............................................................................................................................................
.............................................................................................................................................
Suggest a change to line 40 to overcome this
problem................................................................................................................................
.........................................................................................................................................[2]
Questions from Past Papers
13.8 Summer 2015 P21& 23
2 Read this section of program code that should input 10 positive numbers and then
output the smallest number input.
1 Small = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num< Small THEN Num = Small
6 Counter = Counter + 1
7 PRINT Small
8 UNTIL Counter < 10
There are four errors in this code.
Locate these errors and suggest a corrected piece of code for each error.
1 .....................................................................................................................................
.....................................................................................................................................
2 .....................................................................................................................................
.....................................................................................................................................
3 .....................................................................................................................................
......................................................................................................................................
4 .....................................................................................................................................
................................................................................................................................ [4]
Examiner Report Question 2
Most candidates located at least one error and suggested a suitable piece of corrected code. The error on line
8 was often identified, with better candidates providing a working correction.
13.9 Summer 2015 P22
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
3 WHILE Counter > 30
4 DO
5 INPUT Num
6 IF Num< Large THEN Large = Num
7 Counter = Counter - 1
8 ENDWHILE
9 PRINT Large
There are four errors in this code.
Locate these errors and suggest a corrected piece of code for each error.
1 ..............................................................................................................................................
………………...........................................................................................................................
2................................................................................................................................................
..................................................................................................................................................
3................................................................................................................................................
.................................................................................................................................................
.4
…......................................................................................................................................... ....
.........................................................................................................................................[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.
13.10 Winter 2015 P21 & 22

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

There are four errors in this code.


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. PAGE 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......................................................................................................................................
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.

13.20 Winter 2018 P23

4 This is a section of program code.


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
(a) There are three errors in this code.
State the line numbers that contain the errors and describe how to correct each error.
Error 1 …..................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
..............................
Error 2
…..............................................................................................................................................
...
..................................................................................................................................................
..................................................................................................................................................
.............................. Error 3
…..............................................................................................................................................
...
..................................................................................................................................................
..................................................................................................................................................
....................... [3]
(b) State the purpose of this program.
..................................................................................................................................................
..................................................................................................................................................
..................................................................................................................................................
.........................................[1]

13.21 March 2019 P22 (India)


Finding output from pseudo code
13.20 Winter 2001
This algorithm grades candidates on marks out of ten.
1 input a Mark
2 case Mark of
3 0, 1, 2, 3 : Grade = Fail
4 4, 5 : Grade = Pass
5 6, 7 : Grade = Merit
6 8, 9, 10 : Grade = Distinction
7 otherwise Mark = -1
8 endcase
9 if Mark = -1 then
10 print „Not a valid
mark‟ 11 else output Grade,
„Grade‟
(a) Dry run the algorithm for each of the following data and complete the table. [3]
INPUT OUTPUT
0
5
99
(b) Write down two instructions which could be inserted between lines 1 and 2 to allow the
algorithm to deal with marks out of 100. [2]
13.21Specimen 2016
Jatinder uses Internet banking. This pseudo code checks her PIN. c 0
INPUT PIN

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

Q 13.2) Summer 2005


Q 13.7) Winter 2014 P12
Q 13.3) Winter 2006

Questions from Past Papers


13.8 Summer 2015 P21& 23

Q 13.4) Winter 2010 13.9 Summer 2015 P22

13.10 Winter 2015 P21 & 22

13.11 Summer 2016 P22

Q 13.5) Summer 2011

13.12 Specimen paper 2016 P2


Q 13.6) Winter 2013
13.13 Winter 2016 P21-23

13.18 Winter 2017 P21

13.14 Winter 2016 P22

13.19 March 2018 P22 (India)

13.20 Winter 2018 P23

13.15 March 2017 P21 (India)

Finding output from pseudo code


13.20 Winter 2001

13.16 Summer 2017 P21

13.21 Specimen 2016

13.17 Summer 2017 P22


Winter 2002
Summer 2003 13.25

13.24 Winter 2003

You might also like