2 Problem Solving and Programming V21
2 Problem Solving and Programming V21
Contents
About the developer of this workbook ............................................................................................................................. 2
Syllabus content & assessment at a glance ...................................................................................................................... 3
Introduction to problem solving ....................................................................................................................................... 5
Algorithm .......................................................................................................................................................................... 9
Selection:......................................................................................................................................................................... 27
Iteration (Repetition, Loop) ............................................................................................................................................ 48
Structure Chart.............................................................................................................................................................. 102
Flowchart ...................................................................................................................................................................... 105
Finding Output from flowchart ................................................................................................................................. 129
Algorithm pseudo code ................................................................................................................................................. 170
Errors in Pseudo code and Program ............................................................................................................................. 202
Arrays: Data Structure & Pre-Release Materials........................................................................................................... 244
Database ....................................................................................................................................................................... 282
Introduction to Logo ..................................................................................................................................................... 331
2 | Page
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.
3 | Page
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 minutes 40%
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.
4 | Page
Paper 2
Practical Problem-solving and Programming
5 | Page
Chapter 8
Understand the need for validation and verification checks to be made on input data
(validation could include range checks, length checks, type checks and check digits)
Use trace tables to find the value of variables at each step in an algorithm
Identify errors in given algorithms and suggest ways of removing these errors
Produce an algorithm for a given problem (either in the form of pseudo code or flowchart)
Computer system is often divided up into sub-systems. This division can be shown using top-down
design to produce structure diagrams that demonstrate the modular construction of the system.
Each sub-system can be developed by a programmer as sub-routine or an existing library routine
may be already available for use. How each sub-routine works can be shown by using flowcharts
or pseudo code.
6 | Page
Top-down design
Structure diagrams
Flowcharts
Pseudo code
Library routines
Sub-routines
1. Top-Down Design
Top-down design is the breaking down of a computer system into a set of subsystems, then
breaking each sub-system down into a set of smaller sub-systems, until each sub-system just
performs a single action.
This is an effective way of designing a computer system to provide a solution to a problem, since
each part of the problem is broken down into smaller more manageable problems. The process of
breaking down into smaller sub-systems is called ‘stepwise refinement’.
This structured approach works for the development of both large and small computer systems.
When large computer systems are being developed this means that several programmers can
work independently to develop and test different subsystems for the same system at the same
time. This reduces the development and testing time.
2. Structure Diagrams
The STRUCTURE DIAGRAM shows the design of a computer system in a hierarchical way, with
each level giving a more detailed breakdown of the system into sub-systems.
7 | Page
3. Flowcharts
A FLOWCHART shows diagrammatically the steps required for a task (sub-system) and the order
that they are to be performed. These steps together with the order are called an ALGORITHM.
Flowcharts are an effective way to communicate the algorithm that shows how a system or sub-
system works.
4. Pseudo code
PSEUDO CODE is a simple method of showing an algorithm, using English-like words and
mathematical operators that are set out to look like a program.
5. Library routines
A LIBRARY ROUTINE is a set of programming instructions for a given task that is already
available for use. It is pre-tested and usually performs a task that is frequently required. For
example, the task ‘get time’ in the checking-for-the-alarm-time algorithm would probably be readily
available as a library routine.
6. Sub-routines
A SUB-ROUTINE is a set of programming instructions for a given task that forms a subsystem, not
the whole system. Sub-routines written in high-level programming languages are called
‘procedures’ or ‘functions’ depending on how they are used.
7. Function
A Function is a sub-routine that always returns a value.
8. Procedure
A Procedure is a sub-routine that doesn’t have to returns a value.
Winter 2018 P22
3 Four programming concepts and four descriptions are shown.
Draw a line to connect each programming concept to the most appropriate description. [3]
Programming Description
concept
A subroutine that does not have to
Library routine
return a value.
A standard subroutine that is
Structure diagram
available for immediate use.
A subroutine that always returns a
Procedure
value.
An overview of a program or
Function
subroutine.
8 | Page
Winter 2019 P23
2 Tick () one box in each row to identify if the statement about structure diagrams is true or false. [2]
True False
Statement
() ()
A structure diagram is a piece of code that is available throughout the structure of a
program.
A structure diagram shows the hierarchy of a system.
A structure diagram is another name for an array.
A structure diagram shows the relationship between different components of a
system.
Winter 20 P22
2 Tick () one box in each row to identify if the statement about subroutines is true or false. [2]
Statement true false
A subroutine is called from within a program.
A subroutine is not a complete program.
A subroutine is a self-contained piece of code.
A subroutine must return a value to the code
from which it was called.
9 | Page
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.)
10 | P a g e
“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
Atomic type names
The following keywords are used to designate atomic data types:
1. INTEGER:
A whole number (without fractional part) can be positive or negative and used in calculations.
For example COUNT which never requires fractional part and may store 56, 89, 1
2. REAL:
A number capable of containing a fractional part can be positive or negative and used in
calculations.
For example Weight may contain fractional Part like 56.8, 89.0, 1.2
3. CHAR:
A single character (may be letter, special character or number but number cannot be used in
calculation).
4. STRING:
A sequence of alphanumeric and special characters but number cannot be used in
calculation.
For example “Abdullah”, “0300-2724734”, “House No 56 Block 2, PECHS Karachi”
Literals
Literals of the above data types are written as follows:
Identifiers should be considered case insensitive, for example, Countdown and Countdown should
not be used as separate variables.
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.
14 | P a g e
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).
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.
Comments
Comments are preceded by two forward slashes // . The comment continues until the end of the
line. For multi-line comments, each line is preceded by //.
Normally the comment is on a separate line before, and at the same level of indentation as, the
code it refers to. Occasionally, however, a short comment that refers to a single line may be at the
end of the line to which it refers.
Example – comments
// This is example of comments
// swapping values of X and Y
Temp ← X // temporarily store X
X←Y
Y ← Temp
15 | P a g e
2.1.2 Flowchart
Symbol Description
Off-Page Connector
On-Page Conncetor
16 | P a g e
COUNTING
Counting is used to find how many items are there by incrementing by 1 during each time loop is
executed.
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
(new) (old)
i.e. INCREMENT (old) Count by 1 to get (new) Count
TOTALLING
Totalling is used to calculate 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
(new) (old)
i.e. ADD Number to (old) Total to obtain (new) Total
Q 1 Summer 2015 P21& 23
5 Explain the difference between a variable and a constant in a program.
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
....................................................................................................................................................................[2]
Examiner Report Question 5
Well answered by many candidates.
Q 2 Summer 2015 P21& 23
4 Five data types and five data samples are shown below.
Draw a line to link each data type to the correct data sample. [4]
Q 4 A marathon runner records their time for a race in hours, minutes and seconds.
An algorithm is shown below in structured English.
INPUT race time as hours, minutes and seconds
CALCULATE race time in seconds
STORE race time in seconds
OUTPUT race time in seconds
The identifier table needs to show the variables required to write a program for this algorithm.
Complete the table.
Identifier Data type Description
RaceHours INTEGER The hours part of the race time.
18 | P a g e
Q 5 A program contains the following code to calculate the circumference of a bicycle wheel, using
the wheel size (diameter).
CONSTANT Pi = 3.14
INPUT WheelSize
Circumference = Pi * WheelSize
OUTPUT Circumference
(a) The code uses one constant and two variables.
(i) State the names of the constant and the variables.
Constant: ............................................................................................................................................
Variables: .......................................................................................................................................[2]
(ii) Explain one difference between a constant and a variable.
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[2]
(b) The data type of WheelSize is integer and the data type of Circumference is real number.
Explain the difference between an integer and a real number.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [2]
Q 6 Computer programs have to evaluate expressions.
Study the sequence of pseudo code statements.
Write down the value assigned to each variable.
DECLARE h, z, w, r, Perimeter, Area: REAL
DECLARE A: BOOLEAN
h 13.6
w 6.4
Perimeter (h + w) * 2 Perimeter =……………………. (1)
r 10
Area 3.14 * (r ^ 2) Area= (1)
z 11 + r / 5 + 3 Z = (1)
A NOT (r > 10) A= (1)
19 | P a g e
Q 7 Computer programs have to evaluate expressions.
Study the sequence of pseudo code statements.
Give the value assigned to each variable.
The statement may generate an error. If so, write ERROR.
The & operator is used to concatenate strings.
DECLARE N1 : INTEGER
DECLARE N2 : INTEGER
DECLARE Answer : REAL
DECLARE Found : BOOLEAN
DECLARE IsValid : BOOLEAN
N1 ← 3
N2 ← 9
Answer ← (N1 + N2) / 6 Answer = [1]
Answer ← 3 * (N1 – 2) + N2 / 2 Answer = [1]
IsValid← (N1 > N2) AND (N2 = 9) IsValid = [1]
Found ← FALSE
IsValid← (N1 > N2 / 2) OR (Found = FALSE) IsValid = [1]
Answer "1034" & " + " & "65" Answer = [1]
.................................................................................................................................................... [2]
22 | P a g e
The concept of a program
A program is a sequence of instructions or programming language statements written to make a
computer perform certain tasks.
Basic Control Constructs:
Following are the basic constructs of algorithm and program which controls execution of
statements:
1. Sequence: One statement is being executed after another in the order they are written
In following example statement number ‘i’ will be executed at 1st and then ‘ii’ then ‘iii’ and at
last statement number ‘iv’ will be executed:
i. INPUT Num1
ii. INPUT Num2
iii. Total Num1 + Num2
iv. PRINT Total
Flowchart is also drawn in the sequence in which the program is intended to be executed.
START
INPUT Num1,Num2
Avg(Num1+Num2)/2
OUTPUT Num1,Num2
STOP
Problem 2: Input daily wages and number of day worked and output monthly pay.
23 | P a g e
Q 9.1) Describe the term Computer System and name it’s components.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [5]
Q 9.2 a) Define the term algorithm, name the two ways of representing algorithm.
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [1]
1. ………………………………………………………………………………………………………………
2. ................................................................................................................................................. [2]
Answer Key: A series of instructions//sequence of steps;(Designed to) perform a particular task//solve a
problem.
Flowchart and pseudo code
b) Simple algorithms usually consist of three different stages.
Complete the table below. Write each example statement in program code.
The second stage has already been given. [5]
Stage Example statement
Process
<identifier> ← <expression>
For example:
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate
Counter ← 0
Counter ← Counter + 1
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.
27 | P a g e
Selection:
Selection determines program flow path on the basis of given condition.
Selection decides which statement(s) are to be executed depending upon the result of a given
condition.
IF statements
IF statements are used when there are one or two options.
When there is only one option IF statements without an ELSE clause is written as follows:
IF<condition>THEN
<statements if true>
ENDIF
Example
IF Number>Largest THEN
LargestNumber
ENDIF
When there are two options IF statements with an ELSE clause is written as follows:
IF <condition>THEN
<statements if true>
ELSE
<statements if false>
ENDIF
IS Yes
if condition is true
Conditio
n
No
if condition is false
28 | P a g e
Example
IF Marks>=50 THEN
Result “Pass”
ELSE
Result “Fail”
ENDIF
PRINT Result
Note that the THEN and ELSE clauses are only indented by two spaces. (They are, in a sense, a
continuation of the IF statement rather than separate statements).
When IF statements are nested, the nesting should continue the indentation of two spaces. In
particular, run-on THENIF and ELSE IF lines should be avoided.
Start
INPUT Marks
IS Yes
Marks>=50 Result “Pass”
?
No
Result “Fail”
PRINT Result
End
29 | P a g e
CASE statements
CASE is a conditional statement to deal with many possible outcomes.
CASE statements allow one out of several branches of code to be executed, depending on the value
of a variable.
In case selection number of statements are reduced so code become more simplified.
CASE statements are written as follows:
CASE OF<identifier>
<value 1> : <statement>
<value 2> : <statement>
...
ENDCASE
CASE OF Yes
<Identifier
> Condition1
Statement
OTHERWISE Condition2
Statement
Statement Condition 3
Statement
30 | P a g e
Example – formatted CASE statement
INPUT Marks
CASE Marks OF
>=90: Grade “A*”
>=80: Grade “A”
>=70: Grade “B”
>=60: Grade “C”
>=50: Grade “D”
>=40: Grade “E”
OTHERWISE : : Grade “U”
ENDCASE
PRINT Grade
Start
INPUT Marks
CASE OF
Marks
Marks>=90
Grade”A*”
OTHERWISE Marks>=80
Grade”A”
Grade”U”
Marks>=70
Grade”B”
Marks>=60
Grade”C*”
Marks>=50
Grade”D”
Marks>=40
Grade”E”
PRINT Grade
End
31 | P a g e
IF…THEN…ELSE…ENDIF CASE….OF….OTHERWISE…ENDCASE
Problem: input marks and output result Problem: input marks and output grade
Problem 3: A formula for calculating the body mass index (BMI) is:
Using Flowchart, write an algorithm that will input weight (kg) and height (m) of students, calculate
their body mass index (BMI) and output their BMI and comments on BMI.
BMI <19 Under weight
BMI < =25 Normal Weight
BMI>25 Over weight
32 | P a g e
Problem: Write algorithm using pseudo code for the following problems:
Problem 4: Input total weight of passengers in a lift output "Overloaded, step out" if Total Weight is
above 600 otherwise output "Ready to go".
Problem 5: Input age of candidates for driving license, output "Not allowed to drive" or "Kindly fill in
the form". The minimum allowed age for driving is 18 years.
33 | P a g e
Problem 6: Input age of candidate in an employment center, output "You are not eligible due to age".
Allowed age is between 18 and 60 both inclusive.
Problem 7: which inputs price and quantity calculates amount and if billing amount is above 5000
then allows a 5% discount on the billing amount.
Output billing amount, discount and amount after discount
34 | P a g e
Problem 8) Summer 2018 P22
3 This pseudo code algorithm inputs two non-zero numbers and a sign, and then performs the
calculation shown by the sign. An input of zero for the first number terminates the process.
INPUT Number1, Number2, Sign
IF Sign = '+' THEN Answer = Number1 + Number2 ENDIF
IF Sign = '-' THEN Answer = Number1 - Number2 ENDIF
IF Sign = '*' THEN Answer = Number1 * Number2 ENDIF
IF Sign = '/' AND Number2<>0 THEN Answer = Number1 / Number2 ENDIF
IF Sign<>’+’ AND Sign<>’-‘ AND Sign<>’*’ AND Sign<>’/’
THEN
Answer = 0
ENDIF
IF Answer <> 0 THEN OUTPUT Answer ENDIF
(a) Complete the trace table for the input data:
5, 7, +, 6, 2, –, 4, 3, *, 7, 8, ?, 10, 0, / [3]
(b) Show how you could improve the algorithm written in pseudo code by writing an alternative
type of conditional statement in pseudo code.
[3]
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
35 | P a g e
Example 2
Description
Reason [4]
5 Explain the difference between the programming concepts of counting and totalling.
Include an example of a programming statement for each concept in your explanation. [4]
38 | P a g e
Problem 17) Summer2019 P21
3 (a) Give an example of a conditional statement using pseudo code. [2]
Q1a) Using pseudo code or otherwise, write an algorithm which will input any three numbers and
then print the smallest number.
b) Write an algorithm to input three different numbers, and then output the largest number. Use
either pseudo code or a flowchart.
42 | P a g e
Q 2) Write an algorithm, using pseudo code, to input three different numbers, multiply the two larger
numbers together and output the result. Use the variables: Number1, Number2 andNumber3 for
your numbers and Answer for your result.
Q 4) Write an algorithm, using pseudo code, to input a number between 0 and 100 inclusive.
The algorithm should prompt for the input and output an error message if the number is outside
this range.
43 | P a g e
Q 8) Input price and quantity, calculates amount and if billing amount is above 5000 then allows a
5% discount on the billing amount.
Output billing amount, discount and amount after discount
Q 9) Employees of a shop are entitled to a discount of 10% on the value of goods bought from the
shop. However if an employee has worked at the shop for five or more years they are entitled to a
discount of 20%. Only employees are allowed discounts. The discount on electrical goods is fixed
at only 10%.
Using pseudo code or otherwise, write an algorithm which will determine what discount applies
when any person buys an item.
44 | P a g e
Q 10) Customers can withdraw cash from an Automatic Teller Machine (ATM).
• withdrawal is refused if amount entered > current balance
• withdrawal is refused if amount entered > daily limit
• if current balance < $100, then a charge of 2% is made
• if current balance $100, no charge is made
Write an algorithm which inputs a request for a sum of money, decides if a withdrawal can
be made and calculates any charges. Appropriate output messages should be included.
45 | P a g e
Q 11) A formula for calculating the body mass index (BMI) is:
Using Flowchart, write an algorithm that will input weight (kg) and height (m) of students, calculate
their body mass index (BMI) and output their BMI and comments on BMI.
BMI <19 Under weight
BMI < =25 Normal Weight
BMI>25 Over weight
46 | P a g e
Q12) A system uses 5 digit numbers with an additional sixth digit used as a check digit.
(b) Each of the six digits in the number has a digit position. [Total=6]
6 5 4 3 2 1 Digit position
a b c d e f
Check
digit
digit in position 1 is the check digit i.e. f
The validity of the check digit is found using the following calculation:
• multiply each digit by its digit position (i.e. ax6, bx5, so on)
• add together the results of the multiplications
• divide the sum by 11
• If the remainder is ZERO then the number is valid
Write an algorithm, using flowchart only, which
• inputs six-digit barcodes in the form a, b, c, d, e and f
• re-calculates the check digit for each number and checks whether the input check digit (e) is correct
47 | P a g e
Q 13) Summer 2013
A small shop uses barcodes which represent 5 digits. The last digit is used as a check digit.
For example:
abcde
01234
The check digit (e) is found by:
• multiplying the first and third digits (i.e. a and c) by 3
• multiplying the second and fourth digits (i.e. b and d) by 2
• adding these four results together to give a total
• dividing this total by 10
• remainder is check digit (e)
Write an algorithm, using flowchart only, which
inputs five-digit barcodes in the form a, b, c, d, e
re-calculates the check digit for each number and checks whether the input check digit (e)
is correct
48 | P a g e
Iteration (Repetition, Loop)
Repetition is used to execute a set of instructions multiple times.
Repetition is also referred as LOOP or ITERATION.
There are following three types of loops:
1. Count-controlled loop
2. Pre-condition loop
3. Post-condition loop
The identifier must be a variable of data type INTEGER, and the values should be expressions that
evaluate to integers.
The increment must be an expression that evaluates to an integer. In this case the identifier will be
assigned the values from value1 in successive increments of increment until it reaches value2. If it
goes past value2, the loop terminates. The increment can be negative.
Example: To input a series of numbers and calculate total and stops if a –ve number
is entered:
The condition is checked at the beginning of the loop. If condition is true loop statements
are executed again and again.
Total0
Start INPUT Num
WHILE Num>0 DO
Total Total + Num
Total 0 INPUT Num
END WHILE
INPUT Num PRINT Total
IS No
Num>0 PRINT Total
?
Yes
INPUT Num
End
50 | P a g e
Post-condition (REPEAT UNTIL) loops
A loop in which condition is given at the end of loop and which is executed only when the condition
is false is called post-condition loop.
It is are written as follows:
REPEAT
<Statements>
UNTIL<condition to stop the loop>
The statements in the loop will be executed at least once. The condition is tested after the statements
are executed and if it evaluates to TRUE the loop terminates, otherwise the statements are executed
again.
Example: To input and validate a number and to reject it if a negative number is entered and
ask to re-enter another number
The condition is checked at the end of the loop. If condition is false loop statements are executed
again and again.
REPEAT
INPUT Num
UNTIL Num>0
Start
INPUT Num
PRINT “Error:
IS No re-enter number”
Num>0
?
Yes
51 | P a g e
Example: to input 10 numbers and output their final total
52 | P a g e
Control Construct: Iteration: Iteration is used to execute a set of instructions multiple times. It is
also referred as LOOP or ITERATION.
In the following example statement number ‘ii’ will be executed 10 times:
Problem: Print the name of Allah 10 times.
LOOPING STATEMENTS:
1. FOR … TO…NEXT: Count Controlled loop
2. REPEAT … UNTIL : Post Condition loop
3. WHILE…DO…ENDWHILE: Pre-Condition Loop
53 | P a g e
Pre-Condition Loop: (WHILE … DO … ENDWHILE)
When condition to continue the loop is given
Problem: To input a series of positive numbers and add in total. Continue this process when
positive numbers are entered only.
WHILE
Differences between
Pre-Condition Post Condition
54 | P a g e
55 | P a g e
Problem 1: Write an algorithm using flowchart and pseudo code to print following number series:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
0 + 1 1
1 + 1 = 2
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
5 + 1 = 6
6 + 1 = 7
7 + 1 = 8
8 + 1 = 9
9 + 1 = 10
Problem 2: Write an algorithm using flowchart and pseudo code to print following even number
series:
2, 4, 6, 8, 10, 12, 14, 16, ,18, 20
56 | P a g e
Problem 3: Write an algorithm using flowchart and pseudo code to print following odd number
series:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19
Problem 4: Write an algorithm using flowchart and pseudo code to print following number series:
20, 19, 18, 17, 16, 15, 14, 13 12, 11
57 | P a g e
Problem 5: Write an algorithm using flowchart and pseudo code to print multiplication table of 2.
Problem 6: Write an algorithm using flowchart and pseudo code to input a number and print its
multiplication table.
58 | P a g e
Problem 7: Input daily wages and number of day worked and output monthly pay for 100
employees.
Problem 9: Input marks of 30 students in a class output result of each student. Passing marks is
40
59 | P a g e
Problem 10: Print name of Allah 10 times
Flowchart of pre-condition and post condition loops
Problem: Print name of Allah 10 times using all types of loops
60 | P a g e
11 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]
61 | P a g e
12 There are three different types of looping structures. Write pseudo code for each of following thr
ee problems using different looping structure:
a) Input daily temperature for a month of 30 days, calculate and output their total and average.
……………………………………………………….................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
..................................................................................................................................................... [6]
13 Summer 2018 P22
b) Draw a flowchart for an algorithm to input numbers. Reject any numbers that are negative
and count how many numbers are positive. When the number zero is input, the process
ends and the count of positive numbers is output.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [6]
62 | P a g e
14 Extracted from March 2018 P22 (India)
c) Input the weight in kilograms of passengers stepping into a lift. The lift can take a maximum
weight of 640 kilograms. Stop input when total weight is greater than or equal to maximum
allowed weight and display message “Ready to Go”
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
.......................................................................................................................................................
................................................................................................................................................ [6]
Q 15) Write an algorithm, using pseudo code and a FOR … TO … NEXT loop structure, to input
1000 numbers.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
Summer 2015 P22
5 (a) Write an algorithm, using pseudo code and a FOR … TO … NEXT loop structure, to input
1000 numbers into an array.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[2]
67 | P a g e
(b) Rewrite your algorithm using another loop structure.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[4]
5 REPEAT ... UNTIL and WHILE ... DO ... ENDWHILE are two different loop structures you can
use when writing pseudo code.
Explain, using examples, why you would choose to use each type of loop.
Example 1 ..........................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Reason for choice ..............................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Example 2 ..........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Reason for choice ...........................................................................................................................
..........................................................................................................................................................
......................................................................................................................................................[6]
69 | P a g e
Winter 2016 P22
4 IF ... THEN ... ELSE ... ENDIF and CASE ... OF ... OTHERWISE ... ENDCASE
are two different conditional statements that you can use when writing pseudo code.
Explain, using examples, why you would choose to use each conditional statement.
Example 1 ........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Reason for choice ............................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Example 2 ........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Reason for choice ............................................................................................................................
..........................................................................................................................................................
..................................................................................................................................................... [6]
March 2017 P21 (India)
5 (a) Rewrite the following pseudo code algorithm using a WHILE … DO … ENDWHILE loop.
INPUT Num
FOR Counter ← 1 TO 12
Num ← Num * Counter
A[Counter] ← Num
NEXT
[4]
70 | P a g e
(b) Explain the differences between a WHILE … DO … ENDWHILE and a REPEAT … UNTIL loop
.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[4]
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]
71 | P a g e
Summer 2017 P21
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
(b) Identify, using pseudo code, another loop structure that the algorithm in part (a) could
have used.
............................................................................................................................................................
........................................................................................................................................................[1]
(c) Write an algorithm, using pseudo code, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside this
range.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[3]
72 | P a g e
Winter 2017 P21
4 (a) Four pseudo code descriptions and five pseudo code statements are shown. Draw one line
to link each pseudo code description to the correct pseudo code statement. Not all pseudo code
statements will be used.[4]
A conditional statement to
deal with many possible IF…THEN…ELSE…ENDIF
outcomes.
WHILE…DO…ENDWHILE
A loop that will iterate a
set number of times.
CASE…OF…OTHERWISE…ENDCASE
A conditional statement
with different outcomes for REPEAT…UNTIL
true and false.
5 Explain the difference between the programming concepts of counting and totalling.
Include an example of a programming statement for each concept in your explanation.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [4]
74 | P a g e
Winter 2018 P22
4 A programmer wants to test that the readings from 2000 electricity meters are greater than
400units and less than 900 units. The programmer uses selection and repetition statements as
part of the program. Explain, using programming statements, how selection and repetition could
be used in this program.
Selection ....................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................
Repetition...................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
..............................................................................................[4]
Summer2019 P21
3 (a) Give an example of a conditional statement using pseudocode.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [2]
(b) Describe the purpose of a conditional statement.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [2]
77 | P a g e
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]
Validation is an automatic computer check to ensure that the data entered is sensible and
reasonable. It does not check the accuracy of data.
For example, a secondary school student is likely to be aged between 11 and 16. The computer
can be programmed only to accept numbers between 11 and 16. This is a range check.
However, this does not guarantee that the number typed in is correct. For example, a student's
age might be 14, but if 11 are entered it will be valid but incorrect.
A validation check is a rule that is built into a database to check that the data entered is:
Sensible
Reasonable
Within acceptable boundaries
Complete
It does NOT mean that the data is actually correct, that requires verification.
There are a number of different validation rules that can be used in a database:
Type Checks - Field data types provide a basic method of validation. Field data types are
assigned to fields during the creation of the database table and data types such as Numeric,
Boolean, Date/Time and Image restrict what can be entered. If a user tries to enter text in a date
field or alphabetic characters in a numeric field, their entry will be rejected.
Range checks - these are used to limit the range of data a user can enter. The 'day' part of a date
must be in the range 1 to 31. An exam grade should be in the range 'A'...'G' or 'U'.
Presence checks - these simply check that an entry has been made in a particular field i.e. a null
value (empty field) is not permitted. Usually, not every field in a record needs to be filled in,
however there are likely to be some that must have a value and the presence check means that
the system will not allow the record to be saved until an entry is made. An application for a
passport must have the applicant's surname.
80 | P a g e
Length Checks - All alphanumeric data has a length. A single character has a length of 1 and a
string of text such as “Hello World” has a length of 11 (spaces are counted in text strings). A length
check ensures that such data is either an exact length or does not exceed a specified number of
characters. Mobile phone numbers are stored as text and should be 11 characters in length.
Lookup - A lookup check takes the value entered and compares it against a list of values in a
separate table. It can then return confirmation of the value entered or a second list based on the
value. One use of lookups restricts users to pre-defined input using drop-down lists. A user is
forced to use a list box to select from a predetermined list of valid values.
Check digits - this type of check is used with numbers. An extra 'check digit' is calculated from the
numbers to be entered and added to the end. The numbers can then be checked at any stage by
recalculating the check digit from the other numbers and seeing if it matches the one entered. One
example where a check digit is used is in the 10 digit ISBN number which uniquely identifies
books.
The last number of the ISBN is actually the check digit for the other numbers, for example - the
ISBN 0192761501.
Following two Methods are used to calculate check digit
Modulo-11 Method:
Steps Example
1. Find sum of digits at odd position
2. Find sum of digits at even position and
multiply result by 3
3. Extreme Data
When testing algorithms with numerical values, sometimes only a given range of values should be
allowed. For example, percentage marks should only be in the range 0 to 100. The algorithm
should be tested with EXTREME DATA, which, in this case, are the largest and smallest marks
that should be accepted. Extreme data are the largest and smallest values that normal data can
take.
Extreme data: 0, 100
Expected results: these values should be accepted
4. Boundary Data
This is used to establish where the largest and smallest values occur. For example, for percentage
marks in the range 0 to 100, the algorithm should be tested with the following boundary data; at
each boundary two values are required, one value is accepted and the other value is rejected.
Boundary data for 0 is –1, 0
Page | 84
Expected results: –1 is rejected, 0 is accepted
Rogue Values
A value that stops input is called Rogue Value.
A sequence of inputs may continue until a specific value is input. This value is called a rogue
value and must be a value which would not normally arise.
A rogue value lets the computer know that a sequence of input values has come to an end.
Example: A number of marks are to be input (terminated by a rogue value of -1). How many of
them are over 50?
Counter 0
INPUT Marks
REPEAT
IF Marks >50 THEN Above50 Above50 +1
INPUT Marks
UNTIL Marks=-1
OUTPUT Above50
Example: The flowchart below inputs the weight of a number of parcels in kilograms. Parcels
weighing more than 25 kilograms are rejected. A value of –1 stops the input (a rogue
value).The following information is output: the total weight of the parcels accepted and number of
parcels rejected.
Page | 85
2) What validation type would make sure a post code was entered in the correct format?
a) Length check b) Format Check c) Presence check
3) What validation type would you use to check that numbers fell within a certain range?
a) Range check b) Presence Check c) Check digit
5) What validation type uses the last one or two digits to check the other digits are correct?
a) Length check b) Format check c) Check digit
6) What validation type checks a minimum number of characters have been entered?
a) Length check b) Format check c) Range check
7) Data is to be entered into a computer in the format YYMMDD. Which of the following is not a valid
date?
a) 310921 b) 211113 c) 21st June 2004
patient’s height
patient ID
Contact telephone
number
Marking scheme
Field Name Name of validation check Example of data which would fail the
validation check
Date of visit Format/ type/character check e.g. 2012/12/04e.g. 3rd March 2012
Patient’s height range check/limit check can’t be < 0 or > 2.5m. e.g. –5, fivee.g. 8, -3,
Patient ID type check (can’t be < 0 or > 99999999)
length check e.g. 3142ABCDe.g. 2131451, 136498207
range check e.g. –3, 851341625
Contact telephone length check, type/character e.g. 0773141621834e.g. 7H215GD
number check, format check e.g. 01223/123456/8901234
Page | 88
Winter 2015 P23
4 A routine checks the age and height of children who are allowed to enter a play area. The
children must be less than 5 years of age and under 1 metre in height.
(a) The first set of test data used is age 3 and height 0.82 metres.
State what type of test data this is.
............................................................................................................................................................
Give a reason for using this test data.
............................................................................................................................................................
.......................................................................................................................................................[2]
(b) Provide two additional sets of test data. For each, give
• the type of each set of test data
• the reason why it is used
Each type of test data and reason for use must be different.
Set 1 ..................................................................................................................................................
Type ...................................................................................................................................................
Reason ..............................................................................................................................................
............................................................................................................................................................
Set 2 ..................................................................................................................................................
Type ...................................................................................................................................................
Reason ..............................................................................................................................................
....................................................................................................................................................... [6]
4 A routine checks the weight of melons to be sold in a supermarket. Melons weighing under0.5
kilograms are rejected and melons weighing over 2 kilograms are also rejected.
Give an example of each type of test data for this routine
Normal ...............................................................................................................................................
Extreme ..............................................................................................................................................
Abnormal ........................................................................................................................................[3]
Examiners’ Comments Question 4
Most candidates could identify at least one correct example of test data. Examples of normal and abnormal test data were usually
correct. Some candidates’ examples of extreme test data were incorrect. A correct example of extreme test data would be 0.5 or
2.0.
Page | 89
Winter 2016 P21-23
4 Four validation checks and four descriptions are shown below.
Draw a line to link each validation check to the correct description. [3]
Summer 16 P11, 13
9) Check digits are used to ensure the accuracy of entered data. ()
A 7-digit number has an extra digit on the right, called the check digit.
Summer 17 P21
4 For each of the four statements in the table, place a tick in the correct column to show whether it
is an example of validation or verification. [4]
Statements Validation Verification
To automatically check the accuracy of a bar code
To check if the data input is sensible
To check if the data input matches the data that has been
supplied
To automatically check that all required data fields have
been completed
Value 1 .............................................................................................................................................
Reason .............................................................................................................................................
..........................................................................................................................................................
Value 2 .............................................................................................................................................
Reason .............................................................................................................................................
......................................................................................................................................................[4]
Page | 93
Summer 2018 P21
4 A programmer has written a routine to check that prices are below $10.00. These values are
used as test data.
10.00 9.99 ten
Explain why each value was chosen.
10.00 .................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
9.99 ....................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
ten ......................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[3]
(b) The programmer has also decided to validate the email address and the password.
Describe validation checks that could be used.
Email address.....................................................................................................................................
............................................................................................................................................................
...........................................................................................................................................................
Password ..........................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [2]
Page | 94
Summer 18 P22
5 A program checks that the weight of a basket of fruit is over 1.00 kilograms and under1.10
kilograms. Weights are recorded to an accuracy of two decimal places and any weight not in this
form has already been rejected.
Give three weights as test data and for each weight state a reason for choosing it. All your
reasons must be different.
Weight 1.............................................................................................................................................
Reason...............................................................................................................................................
............................................................................................................................................................
Weight 2.............................................................................................................................................
Reason...............................................................................................................................................
............................................................................................................................................................
Weight 3.............................................................................................................................................
Reason...............................................................................................................................................
....................................................................................................................................................... [6]
Page | 95
Winter 2018 P22
2 (a) Write an algorithm, using pseudo code, to input three different numbers, multiply the two
larger numbers together and output the result. Use the variables: Number1, Number2 and
Number3 for your numbers and Answer for your result.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [5]
(b) Give two sets of test data to use with your algorithm in part (a) and explain why you chose
each set.
Test data set 1 ....................................................................................................................................
Reason ...............................................................................................................................................
............................................................................................................................................................
Test data set 2 ....................................................................................................................................
Reason ...............................................................................................................................................
.......................................................................................................................................................[4]
Page | 96
March 2019 P22
5 A programmer restricts input values to less than 90 and greater than 60.
(a) State whether this is called validation or verification.
...................................................................................................................................................
Name the check that needs to be used.
...................................................................................................................................................[2]
(b) State three different types of test data the programmer would need to use. Give an example of
each type and the reason that the programmer chose that test data.
Type 1 .......................................................................................................................................
Example ....................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
Type 2 .......................................................................................................................................
Example ....................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
Type 3 .......................................................................................................................................
Example ....................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................[9]
Q 12.83 Summer2019 P22
5 Explain what is meant by validation and verification.
Give an example for each one.
Validation ..........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Example ...........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Verification ........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Page | 97
Example ...........................................................................................................................................
..........................................................................................................................................................
...................................................................................................................................................... [6]
Summer2019 P21
4 This section of program code may be used as a validation check.
1 PRINT "Input a value between 0 and 100 inclusive"
2 INPUT Value
3 WHILE Value < 0 OR Value > 100
4 PRINT "Invalid value, try again"
5 INPUT Value
6 ENDWHILE
7 PRINT "Accepted: ", Value
(a) Give a name for this type of validation check.
............................................................................................................................................. [1]
(b) Describe what is happening in this validation check.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [2]
(c) Complete the trace table for this program code using the test data: 200, 300, –1, 50, 60[3]
Value OUTPUT
Complete a trace table for each of the two sets of input data.
Set 1 5, 2, 4, 3, 1, 5
Set 2 3, 2, 1, 0, 7, 3
Trace table set 1: 5, 2, 4, 3, 1, 5
Structure Chart
A Structure Chart is a chart which shows the breakdown of a system to its lowest manageable
parts. They are used in structured programming to arrange program modules into a tree. Each
module is represented by a box, which contains the module's name. The tree structure visualizes
the relationships between modules, showing data transfer between modules using arrows.
Structured Charts are an example of a top-down design where a problem (the program) is broken
into its components. The tree shows the relationship between modules, showing data transfer
between the models.
Q 10.3 The structure chart shows part of the design of a program for an online shopping system.
Parameter Information
A
B
C
D
E
F
G
Page | 105
Chapter 11
Flowchart
2.1.2 Flowchart
Symbol Description
Off-Page Connector
On-Page Conncetor
Page | 106
Q 11.1a) Draw a flowchart that
Inputs marks
Find out student is pass or fail (passing marks are 50 or above)
Output “Pass” or “Fail”
TOTALLING
Totalling is used to calculate 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
(new) (old)
i.e. ADD Number to (old) Total to obtain (new) Total
Count-controlled (FOR) loops
Count-controlled loop is used when the number of repetition is already known.
Example Question
a) Draw a flowchart to input 20 numbers and find the total and average of positive numbers
Page | 111
b) Explain how do you change your flowchart to work for 30 numbers that are between 0 and 100.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[3]
Redraw the flowchart for part b
Page | 112
Conditional Loop:
A loop which is executed on the basis of a condition.
Pre-condition (WHILE) loop in which condition is given at the start of loop and which is executed
only when the condition is true, is called pre-condition loop.
Post-condition (REPEAT UNTIL) loop in which condition is given at the end of loop and which is
executed only when the condition is false is called post-condition loop.
Rogue Value A value which stops input, used to terminate loop.
Q 11.8) Draw a flowchart that
Inputs the weight of a number of parcels in kilograms.
Validate parcel (parcels weighing more than 25 kilograms are rejected).
A value of –1 stops the input.
Outputs the total weight of the parcels accepted and number of parcels rejected.
Past Paper Question of same type in Summer 2015 P21& 23 Q 3
Page | 113
Q11.9)Summer 2009
Study the flowchart very carefully.
(a) Complete the table to show what outputs you would expect for the two inputs.[2]
Input N Output T
5
1
(b) Write down a possible LOOP construct for the section A to B in the flowchart using pseudo
code.
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
……………………………………………………………………………... [2]
Page | 114
Q 11.10) Draw an algorithm using flowchart that:
Inputs the height of children who want to ride on a rollercoaster
Validates height children under 1.2 metres are rejected.
When eight children have been accepted, outputs message “Ready to go” and number of
children rejected.
Complete a trace table for each of the two input values 33 and 75. [4]
Trace table for input value 33
X A B OUTPUT
Test your flowchart by completing following trace table to convert denary number 20 into binary
[4]
X A B OUTPUT
Page | 125
Q 11.20)Winter 2015 P23 Q 3
(a) This pseudo code inputs an integer. The predefined function DIV gives the value of the division,
e.g. Y 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the value of the remainder,
e.g. Y 10 MOD 3 gives the value Y = 1.gives the value Y = 1.
6 Draw four different flowchart symbols and describe how they are used in a program flowchart.[4]
Flowchart symbol Description of use
Page | 127
Draw flowchart for the above pseudo code
Past paper question of same type in Winter 2015 P21 & 22
Page | 128
Q 11.20) Following flowchart is used to count digits in a number
DECLARE Count: Integer
DECLARE Num, x: Real
Count ← 0
INPUT Num
x ← PIN
REPEAT
x ← x/10
Count ← Count + 1
UNTIL x < 1
OUTPUT Count
Draw flowchart for the above pseudo code
Page | 129
Finding Output from flowchart
Q 11.21)Summer 2006
Trace the flow chart using the numbers 2 and 3. Write down each of the values of N in the order that
they are printed out.
(a) 2 ................................................................................................................................[1]
(b) 3 ....................................................................................................................................[2]
Page | 130
Q11.22)Summer 2007
Study the following flowchart very carefully.
(a) Complete the following table showing the expected output from the flowchart for the three
sets of input data: [3]
INPUT X OUTPUT S
48
9170
- 800
(a) What output would be produced from the following input? [2]
Input Output
place hours (H) minutes (M) H M
Tokyo 11 15
Mumbai 15 10
Page | 132
(b) What problem would occur if place = Mumbai and H = 15 and M = 30?
………………………………………………………………………………………… [1]
(c) What problem would occur if place = Washington and H = 4 and M = 0?
………………………………………………………………………………………… [1]
Q 11.24) Summer 2008
The following flowchart inputs two numbers, carries out a calculation and then outputs the result.
(a)Complete the following table for the three sets of input data. [3]
INPUT OUTPUT
U V
5 5
6 -6
12 4
(b) The above algorithm has been placed in a library of routines. Give one advantage of doing this.
………………………………………………………………………..................................…………………
.…………………………………………………………………………………………………………[1]
Page | 133
Q 11.25)Winter 2009. P11
Study the flowchart.
Complete the table to show what outputs you would expect for the three inputs. [3]
INPUT N OUTPUT C
55
2100
1
Page | 134
Q 11.26) Summer 2010 P12
Study the following flowchart very carefully:
What output would you expect if the following data was input into the flowchart? [3]
X OUTPUT
-150
540
0
`
Page | 135
Past paper questions from Flow Chart
Q 11.27 Specimen paper 2016 P2
3 The flowchart inputs the size of a number of car engines; a value of –1 stops the input.
This information is output: average engine size and number of engines with size > 1.5
Complete the trace table for the input data.: 1.8, 2.0, 1.0, 1.3, 1.0, 2.5, 2.0, 1.3, 1.8, 1.3, –1[6]
Engine Count Number Size Average OUTPUT
Page | 136
Complete the trace table for the mark input data: 50, 70, 65, 30, 95, 50, 55, 85, 65, 35, –1, 45[4]
Total Count Distinction Mark OUTPUT
Page | 146
Q 11.34 Winter 2016 P21-23
3 The flowchart below inputs the price of an item under $10. The change from a $10 note is
output.
Any amount less than 5 cents is rounded up to 5 cents.
The predefined function INT rounds a number down to the nearest whole number; for example
Z ←INT(5.7) gives the value Z = 5
Page | 147
Complete the trace table for the input data: 6.29 [5]
Price Change Dollars TenCents OUTPUT
Page | 148
Q 11.35 Winter 2016 P22
3 The flowchart below inputs an integer. The predefined function DIV gives the value of the
division, for example Z ←11 DIV 3 gives the value Z = 3. The predefined function MOD gives the
value of the remainder, for example Z ←11 MOD 3 gives the value Z = 2.
Complete a trace table for each of the two input values 33 and 75. [4]
Trace table for input value 33
X A B OUTPUT
Complete the trace table for the input values 4, 3, -1: [4]
A B C OUTPUT
Page | 150
Q 11.37 Summer 2017 P21
6 This flowchart inputs a range of temperatures in degrees Fahrenheit.
As each temperature is input, it is compared with the previous highest temperature. If it is higher
than the current highest, it replaces the previous highest temperature and then it is converted to
degrees Celsius. For ease of calculation, the final step of the Fahrenheit to Celsius conversion has
been approximated as division by 2.When –1 is entered, the input process stops and the highest
temperature (in both Fahrenheit and Celsius) is output.
Page | 151
Complete the trace table for the input data:
68, 46, 50, 86, 65, 50, 40, 30, –1 [5]
Complete the trace table using the data given in the array. [5]
(b) The size of the batch has increased to 50 sacks. It has been decided to only reject sacks that
are underweight.
State the changes that need to be made to the flowchart.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
Page | 158
Q 11.41 March 2018 P22 (India)
3 This flowchart inputs the weight in kilograms of a passenger stepping into a lift. The lift can take
a maximum of eight passengers or a maximum weight of 640 kilograms.
Page | 159
Complete the trace table for the passenger input data: [4]
50, 70, 65, 100, 95, 50, 55, 85, 70, 75
Weight TotalWeight TotalNumber OUTPUT
Comments on Question 3
Most candidates showed the skill of using a trace table. Some candidates provided a ‘rough answe
r’ in penciland a final answer in ink; this is not recommended as extra values can be seen in the tra
ce table. Candidates found the output the most challenging column to complete correctly and com
mon errors seen were to incorrectly include quotation marks around the output or repeats of the ou
tput.
Page | 160
Q 11.42 Summer 2018 P21
3 The global trade item number (GTIN-8) barcode has seven digits and a check digit.
This pseudo code algorithm inputs seven digits and calculates the eighth digit, then outputs
theGTIN-8.
DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2.
MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3.
(a) Complete the trace table for the input data: 5, 7, 0, 1, 2, 3, 4 [5]
(b) Explain how you would change the algorithm to input eight digits (seven digits and the
checkdigit) and output if the check digit entered is correct or not.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [3]
Page | 161
(b) Show how you could improve the algorithm written in pseudo code by writing an alternative
type of conditional statement in pseudo code.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[3]
Page | 162
Q 11.44 Winter 2018 P22
5 The flowchart checks the level of chlorine and the depth of water compared to the height of the
swimming pool. Error messages are output if a problem is found.
Page | 163
(a) Complete the trace tables for each set of input data. [6]
Input data: 6, 2.5, 2
Height Depth Chlorine OK OUTPUT
(b) Identify a problem with the algorithm that the flowchart represents.
............................................................................................................................................................
...................................................................................................................................................... [1]
Page | 164
Q 11.45 March 2019 P22
3 This flowchart inputs the type of passenger for a survey: S for Senior, A for Adult, C for Child. All
other values are ignored, apart from Z which ends the process.
Page | 165
(b) (i) It has been decided to only reject batches of rope that contain ropes that are too short.
State the change required to the algorithm.
............................................................................................................................................................
.................................................................................................................... [1]
(ii) Explain how the algorithm to reject batches could be improved to make it more effective.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.................................................................................. [2]
Page | 168
Summer 20 P22
4 This flowchart inputs the points won and the points lost when playing a game. The difference
between the points won and lost is calculated and depending on the result the player can: move
up to the next level, stay at the same level, or move down to the previous level. The flowchart
finishes when the input for points won is –1.
Page | 169
(a) Complete a trace table for this set of input data:
5000, 4474, 6055, 2000, 7900, 9800, 3000, 2150, –1, 6700, 7615 [3]
PointsWon PointsLost Difference OUTPUT
(b) The flowchart needs to be changed. When the difference is more than 5000 the output
message is ‘Fantastic leap up two levels’.
Describe the changes that will need to be made to the flowchart.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [3]
Page | 170
Chapter 12
Using pseudo code or otherwise, write an algorithm that will input weight (kg) and height (m) of
students, calculate their body mass index (BMI) and output their BMI.
Test data: 80, 2, 100, 1.9, 60, 2, 70, 1.8
First draw trace table write down column headings
Calculate BMI using trace table:
Weight Height BMI Setup in pseudo code using
declaration of variable
SECTION SHOWS YOU HOW
THIS WOULD WORK’
Using pseudo code or otherwise, write an algorithm that will input Distance Travelled (km) and
Fuel Used (litres) of cars, calculate their fuel economy and output their fuel economy.
Test data: 80, 10, 100, 5, 60, 2, 70, 5
First draw trace table write down column headings
Distance Fuel Fuel
Economy
Setup in pseudo code using
declaration of variable
Now convert the real number into whole number using INT()
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
.......................................................................................
Page | 174
Q12.7)This code is supposed to find out if a positive integer entered by a user is exactly
divisible by the number 3.
Note: line numbers have been included and are not part of the code.
1 INPUT n
2 WHILE n ≥ 0
3 n←n–3
4 ENDWHILE
5 IF n = 0 THEN
6 OUTPUT 'is divisible by 3'
7 ELSE
8 OUTPUT 'is not divisible by 3'
9 ENDIF
The programmer realizes there is an error because a user input of 6 incorrectly outputs ‘is not
divisible by 3'.
(a) In Table place a tick next to the type of error that the programmer has found. [1]
(b) State the line number of the code containing the mistake that causes this error to occur.
............................................................................................................................................[1]
(c)What change needs to be made to the line of code you have identified in your answer to(b)
........................................................................................................................................... [1]
(d)What type of error could occur if the user enters the value eight?
............................................................................................................................................[1]
12.7
a Logical
b2
c Any correct answer, examples include:
If the answer given for 9 (b) is 4 then
WHILE n > 0
WHILE n ≥ 1
WHILE n ≥ 3
If the answer given for 9 (a) (ii) is 7 then
IF n = -3 THEN
d Runtime error // Type error
Page | 175
Q12.8) The following pseudo code calculates the second hand price of different models of car.
The condition is an integer with a value between 1 and 4 where 1 is excellent and 4 is very bad.
INPUT Model, Condition, Age
cost ← 0
IF model = 'Daley' THEN
cost ← 6000
ELSE IF model = 'Minty' THEN
cost ← 4000
ELSE
cost ← 2000
ENDIF
CASE condition OF
1: cost ← cost – 100
2: cost ← cost – 300
3: cost ← cost – 500
4: cost ← cost – 1000
ENDCASE
cost ← cost / age
PRINT cost
a) Tick the most appropriate data type of the variable cost. [1]
Data Type Tick one box
Boolean
Character
Real
String
b) Complete the trace table below showing the changes in the variable cost when the following
values are input: [4]
“Tidy”, 4, 2
Cost
12.8
a) Real
b) 1 mark for every correct row that appears in the correct sequence:
cost
0
2000
1000
500
Page | 176
Q12.9) Write an algorithm, using pseudo code or flowchart only, which:
• inputs 1000 numbers
• outputs how many of these numbers were whole numbers (integers)
(You may use INT(x) in your answer, e.g. y = INT(3.8) gives the value y = 3)
....................................................................................................................................... [4]
(You may use INT(x) in your answer, e.g. y = INT(3.8) gives the value y = 3)
INPUT Y=INT(X) Is CountINT
Y=INT(X) Initial value
X X=Y?
INT function CountINT 0
0
removes
3.8 3 No
fractional part
4 4 Yes 1
CountINTCountINT+1
5 5 Yes 2
Increment if X is an
9.1 9 No
integer
7 7 Yes 3
For integer
numbers X and
y will be equal
Page | 177
Quick Revision Questions of flowchart and pseudo code
Q9.4) Identify three different loop structures that you can use when writing pseudo code.
1........................................................................................................................................................
2 .......................................................................................................................................................
3................................................................................................................................................... [3]
Summer 2015 P21 23
Examiner Report Question 6
Most candidates could identify at least one loop structure. A common wrong answer was to incorrectly identify IF as part of a
loop structure.
Q .12 Describe the purpose of each statement in this algorithm.
Count 0
WHILE Count<10 DO
PRINT Count
Count Count + 1
ENDWHILE
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
Q 9.14b) Explain how do you change your flowchart to work for 30 numbers that are between 0 an
d 100.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[3]
Page | 179
Candidate Example response
Page | 180
Page | 181
Page | 182
Page | 183
Page | 184
Page | 185
Past paper questions on basic concepts of algorithm
3 The following diagram shows four data structures and four descriptions. [3]
Draw a line to connect each data structure to the correct description.
Data structure Description
Constant A collection of related data
2 (a) Write an algorithm to input 1000 numbers. Count how many numbers are positive and how
many numbers are zero. Then output the results. Use either pseudo code or a flowchart.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................... [6]
(b) Give one change you could make to your algorithm to ensure initial testing is more
manageable.
...................................................................................................................................................
.............................................................................................................................................. [1]
Page | 191
Summer 2018 P22
2 (a) Draw a flowchart for an algorithm to input numbers. Reject any numbers that are negative
and count how many numbers are positive. When the number zero is input, the process ends and
the count of positive numbers is output.
(b) Explain the changes you will make to your algorithm to also count the negative numbers.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
Page | 192
Q 12.79 Winter 2018 P23
5 The algorithm allows a number to be entered. It then calculates and outputs the next number in
the mathematical series.
Fib 1
Prev2 0
Prev1 1
INPUT Number
IF Number = 0
THEN Fib = 0
ENDIF
WHILE Number > 2
Fib Prev2 + Prev1
Prev2 Prev1
Prev1 Fib
Number Number-1
ENDWHILE
OUTPUT Fib
(a) Complete the trace table for the input data: 7 [4]
Fib Prev2 Prev1 Number OUTPUT
(b) Complete the trace table for the input data: 2 [2]
Fib Prev2 Prev1 Number OUTPUT
Page | 193
Q 12.83 Summer2019 P22
3 This flowchart inputs the marks gained in an examination. An input of –1 ends the routine.
Complete the trace table for the mark input data: 50, 70, 65, 30, 95, 50, 55, 85, 65, 35, –1, 45[4]
Total Count Distinction Mark OUTPUT
Page | 194
Linear Search
5 Customer names are stored in the array Customer.
An algorithm is to be designed to perform a serial search of the array for a requested customer
name.
The algorithm will use the variables shown in the table.
(a) Study the table and the algorithm and fill in the gaps.
IsFound ← FALSE
Index ← 1
REPEAT
IF …………………………… =SearchName
THEN
IsFound ← TRUE
ELSE
……………………………………………………..
ENDIF
IF ……………………………………………………….. THEN
ENDIF [7]
(b) How many comparisons on average will be needed to find a requested customer from
the Customer array?
......................................................................................................................................................[1]
Page | 195
3 (a) Customer names are stored in the array Customer.
An algorithm is to be designed to perform a serial search of the array for a requested customer na
me.
The algorithm will use the variables shown in the table.
Study the table and the algorithm and fill in the gaps.
Identifier Data Type Description
Customer ARRAY[2000] OF STRING The customer names
Index INTEGER Index position in the customer array
IsFound
SearchName STRING The requested customer name
//Serial search algorithm
INPUT ……………………………………
IsFound ← FALSE
Index ← 1
REPEAT
IF Customer [ ………………….] = SearchName THEN
IsFound ← TRUE
OUTPUT “FOUND – at position ” Index “ in the array”
ELSE
Index ← ……………………………….
ENDIF
UNTIL (IsFound = TRUE) OR
IF ………………………………. THEN
OUTPUT “Customer name was NOT FOUND”
ENDIF [7]
(b) Comment on the efficiency of the serial search algorithm in part (a) for retrieving a data
item from an array with 2000 items.
............................................................................................................................................................
..................................................................................................................................................... [2]
Page | 196
March 20 P22
3 This flowchart inputs student percentage marks for three examinations. If the average of these
marks is 80% or over then a distinction grade is awarded. If the average of these marks is less
than 40% then a fail grade is awarded. Otherwise a pass grade is awarded.
Page | 197
(a) Complete a trace table for each set of input data: [5]
Set 1: 88, 74, 60
Mark1 Mark2 Mark3 Total Average OUTPUT
(b) It has been decided to include an extra grade of Merit when the average of the marks is 60%
or more, and less than 80%. Describe the changes that will need to be made to the flowchart.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [3]
Page | 198
Summer 20 P21
5 The flowchart represents an algorithm.
The predefined function DIV gives the value of the result of integer division,
for example, y ← 9 DIV 4 gives y a value of 2
An input value of –1 ends the algorithm.
Page | 199
(a) Complete the trace table for the input data:
50, 33, 18, 15, 30, –1, 45, 12, 90, 6 [4]
Value Calc1 Calc2 OUTPUT
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 Num
50 Count Count + 1 50 Count Count + 1
60 NEXT Count 60 NEXT Count
70 PRINT Sum 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
Lowest1000
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
Page | 206
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]
Page | 207
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.
............................................................................................................................................................
...........................................................................................................................................................
Page | 208
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 ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
...................................................................................................................................................... [6]
Page | 209
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 ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
......................................................................................................................................................[6]
Page | 210
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 ...........................................................................................................................................
............................................................................................................................................................
Page | 211
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]
Page | 212
Candidate Example Response
Page | 213
Page | 214
Page | 215
Summer 2019 P22
2 (a) An algorithm has been written in pseudocode to input 100 numbers, select and print the
largest number and smallest number.
Count 1
INPUT Number
High Number
Low Count
REPEAT
INPUT Number
IF Number > High
THEN
High Number
ENDIF
IF Number > Low
THEN
Low Number
ENDIF
Count Count + 1
UNTIL Count = 99
PRINT "Largest Number is ", Number
PRINT "Smallest Number is ", Low
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]
Page | 216
(b) Show how you would change the corrected algorithm to total the numbers and print the total.
Use a variable Total.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
Page | 217
Page | 218
Page | 219
Page | 220
Page | 221
Page | 222
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.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
...................................................................................................................................................... [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.
Page | 223
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.
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.
Page | 224
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.
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.
Page | 225
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]
Page | 226
13.12 Specimen paper 2016 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.
Error 1 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
.......................................................................................................................................................[3]
Page | 227
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]
Withinrange Outside
Number Reason
(✓) range (✓)
……………………………………………………………………..
10
……………………………………………………………………..
……………………………………………………………………..
20
……………………………………………………………………..
Page | 228
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]
Page | 229
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]
Page | 230
13.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 1 ................................................................................................................................................
Correction ...........................................................................................................................................
......................................................................................................................................................[2]
(b) Rewrite the correct algorithm using a more suitable loop structure.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [3]
Page | 232
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 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
......................................................................................................................................................[4]
Page | 233
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 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.
Page | 234
13.20Winter 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 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 2 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 3 ................................................................................................................................................
Correction ...........................................................................................................................................
............................................................................................................................................................
Error 4 ................................................................................................................................................
Correction ...........................................................................................................................................
......................................................................................................................................................[3]
(b) State the purpose of this program.
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[1]
Page | 235
13.21 March 2019 P22 (India)
2 (a) An algorithm has been written in pseudocode to input 50 numbers and total only the positive
numbers.
Count ←1
Total ← Count
REPEAT
INPUT Number
IF Number <> 0
THEN
Total ← Total + Count
ENDIF
Count ← Count + 1
UNTIL Count < 50
PRINT Total
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]
Page | 236
13.22 Summer 2019 P22
2 (a) An algorithm has been written in pseudocode to input 100 numbers, select and print the
largest number and smallest number.
Count 1
INPUT Number
High Number
Low Count
REPEAT
INPUT Number
IF Number > High
THEN
High Number
ENDIF
IF Number > Low
THEN
Low Number
ENDIF
Count Count + 1
UNTIL Count = 99
PRINT "Largest Number is ", Number
PRINT "Smallest Number is ", Low
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]
Page | 237
(b) Show how you would change the corrected algorithm to total the numbers and print the total.
Use a variable Total.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
Page | 238
Winter 19 P23
5 The algorithm performs an operation on the array named MyData DIV means integer division, so
only the whole number part of the result is returned e.g. 7 DIV 2 returns a value of 3
First 0
Last 16
Found FALSE
INPUT UserIn
WHILE (First <= Last) AND (Found = FALSE) DO
Middle (First + Last) DIV 2
IF MyData[Middle] = UserIn THEN
Found TRUE
ELSE
IF UserIn < MyData[Middle] THEN
Last Middle - 1
ELSE
First Middle + 1
ENDIF
ENDIF
ENDWHILE
OUTPUT Found
This table shows the contents of the array: MyData e.g. MyData[2] stores the value 5
MyData
Index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
Value 2 3 5 6 8 10 12 13 14 16 18 20 25 27 29 34 36
(a) Complete the trace table for the input data: 10 [6]
First Last UserIn Middle Found OUTPUT
Find the four errors in the pseudocode and suggest a correction to remove each error.
Error 1 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 2 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 3 ..............................................................................................................................................
Page | 240
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 4 ..............................................................................................................................................
Correction .........................................................................................................................................
...................................................................................................................................................... [4]
March 20 P22
2 (a) An algorithm has been written in pseudocode to input 50 numbers. Positive numbers are
stored in the array PosNum[ ]. Negative numbers are stored in the array NegNum[ ].
Zeros are not included in the positive and negative counts.
Count ← 0
PosCount ← Count
NegCount ← Count
REPEAT
INPUT Number
IF Number > 0
THEN
PosCount ← PosCount + 1
PosNum[PosCount] ← Number
ELSE
NegCount ← NegCount + 1
NegNum[NegCount] ← Number
ENDIF
Count ← Count + 1
UNTIL Count >= 50
OUTPUT "There are ", PosCount," positive numbers"
OUTPUT "There are ", NegCount," negative numbers"
Describe the error in the pseudocode and write the correction for this error.
Error ..........................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
................................................................................................................................................... [4]
Page | 241
(b) The algorithm needs to be changed so there is no limit to how many numbers can be input.
When the number 9999 is input, the algorithm stops more numbers being input and outputs the
results. The number 9999 is not to be stored nor counted as a positive number.
Explain how you would change the algorithm.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
Summer 20 P22
3 (a) An algorithm has been written in pseudocode to input the names and marks of 35 students.
The algorithm stores the names and marks in two arrays Name[ ] and Mark[ ]. The highest mark awarded is found and
the number of students with that mark is counted. Both of these values are output.
Give line numbers where the four errors are to be found in the pseudocode. Suggest a
correction for each error.
Error 1 line number ...................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 2 line number ...................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 3 line number ...................................................................................................................
Correction .................................................................................................................................
Page | 242
...................................................................................................................................................
Error 4 line number ...................................................................................................................
Correction .................................................................................................................................
................................................................................................................................................... [4]
(b) Explain how you could extend the algorithm to also find the lowest mark awarded, count the number of students
with that mark, and output both these values.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [6]
Page | 243
Winter 20 P22
3 This pseudocode algorithm is used as a validation check.
PRINT "Input a number from 1 to 5000"
REPEAT
INPUT Number
IF Number < 1 OR Number > 5000
THEN
PRINT "Invalid number, please try again"
ENDIF
UNTIL Number >= 1 AND Number <= 5000
PRINT Number, " is within the correct range"
Identify three different types of test data. For each type, give an example of the test data you
would use to test this algorithm and state a reason for your choice of test.
Type of test data 1 ...........................................................................................................................
Test data ...........................................................................................................................................
Reason .............................................................................................................................................
..........................................................................................................................................................
Type of test data 2 ...........................................................................................................................
Test data ...........................................................................................................................................
Reason .............................................................................................................................................
..........................................................................................................................................................
Type of test data 3 ...........................................................................................................................
Test data ...........................................................................................................................................
Reason .............................................................................................................................................
.................................................................................................................................................... [6]
Page | 244
Chapter 14
Example
StudentName[30] StudentName[1:30] StudentName[0:29]
As per the above illustration, following are the important points to be considered.
Index starts with 1.
Array length is 10 which means it can store 10 elements.
Each element can be accessed via its index. For example, we can fetch an element at index
6 as 19.
The terms associated with Arrays
Name: The identifier of the array is called Array Name. E.g. StudentName[]
Element: Each data item stored in arrayis called element. Array can store only single types of
elements.
Size: The number elements the array can store. E.g. StudentName[1:30] can store 30 names while
StudentName[30] can store 31 names as by default it is 0 to 30.
Index: The position of each element is referred as Index Number. Index of Abdullah in array
example is 1.
Type: Data type of all elements in a single array have same data types.
Page | 245
Dimension: Dimension is the organisational structure of array. It may be 1D that has single index
or 2D that has two indexes.
Example
DECLARE StudentName[1:30] : STRING
StudentName[1] “Abdullah”
Array Name
StudentName
1 Abdullah
2 Rumaisa
Array Elements
3 Rashid
4 Afeera
5 Laiba
6 Patel
Index No
7 Salahuddin
29 Mani
30 Muzna
Declaring an array
It is important declare the arrays before assigning values in it so that program can reserve that
amount of space in its memory; otherwise, there may not be enough space when the program
uses the data.
Declaration consists of telling the computer program:
the identifier name of the array
the sort of data that is going to be stored in the array, i.e. its data type
How many items of data are going to be stored, so that it knows how much space to reserve.
Different programming languages have different statements for initialising the array but they all do
the same thing. In Visual Basic, the statement is:
Dim Name(20) As String
This Dim statement declares:
● the identifier name: Name
● the upper bound: 20
● the data type: String.
The upper bound of 20 specifies that there can be a maximum of 21 data items, since Visual
Basic starts with a subscript of zero. We do not have to fill the array; the upper bound of 20
indicates the maximum size.
Page | 246
The array that has been described in one dimension array so far is really only a list of single data
items. It is possible to have an array which can be visualised as a two-dimensional table with rows
and columns and a data value in each cell.
Page | 247
Reading data into an array
To assign data values to the elements of the array, we do this with assignment statements such
as:
Name(6) = “Patel”
This places the string “Patel” at index position 6 in the array.
Similarly, the following statement places the string “Rashid” at index position 3 in the array.
Name(19) = “Mani”
Quick Revision Questions
Q 17.1) Explain the following terms regarding arrays:
Array: ……………………………………………………………….…………………………………………
………………………………………………………………………………………………………………….
Size of Array: ………………………………………………………………………………………………..
………………………………………………………………………………………………………………….
Element: ………………………………………………………………….…………………………..………
………………………………………………………………………………………………………………….
Index: ………………………………………………………………….………………………………………
………………………………………………………………………………………………………………….
Type: ………………………………………………………………….………………………………………
………………………………………………………………………………………………………………….
Dimension: ………..………………………………………………….………………………………………
………………………………………………………………………………………………………………….
Q 17.2) Explain with the help of examples when arrays are used in programming.
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………
……………………………………………………………………….…………………………………………
………………………………………………………………………….………………………………………
………………………………………………………….……………………………………………………
Q 17.3) Declare arrays to Explain with the help of examples when arrays are used in programming
.
Complete the trace table using the data given in the array. [5]
3 The global trade item number (GTIN-8) barcode has seven digits and a check digit.
This pseudocode algorithm inputs seven digits and calculates the eighth digit, then outputs
theGTIN-8.
DIV(X,Y), finds the number of divides in division for example DIV(23,10) is 2.
MOD(X,Y), finds the remainder in division for example MOD(23,10) is 3.
(a) Complete the trace table for the input data: 5, 7, 0, 1, 2, 3, 4 [5]
(b) Explain how you would change the algorithm to input eight digits (seven digits and the check-
digit) and output if the check digit entered is correct or not.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [3]
Page | 252
Q 17.6) An algorithm to reset the contents of the array Coins after each sale is shown below. Ther
e are 10 different coins. This algorithm contains a logic error.
i=1
REPEAT
Coins(i) = 0
i=i+1
UNTIL i = 10
5 A programmer writes a program to store a patient’s temperature every hour for a day. State the
data structure that would be most suitable to use and give the reason for your choice.
Data structure ......................................................................................................................
Reason ..............................................................................................................................
.......................................................................................................................................[2]
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
(b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have
used.
............................................................................................................................................................
.....................................................................................................................................................[1]
(c) Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside this
range.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
Page | 254
Q 17.10Winter 2017 P22
3 The following diagram shows four data structures and four descriptions. [3]
Draw a line to connect each data structure to the correct description.
Data structure Description
Constant A collection of related data
A restaurant table will have its data stored in its own booking record. Alessio decides to use an
array of records.
Write program code to declare the array TableBookingsfor the 12 table records.
Programming language.......................................................................................................................
Code ...................................................................................................................................................
........................................................................................................................................................[1]
Summer 2016 P21 &P23
(ii) The swimming club has 50 members.
State the data structure that would be most suitable to use and give a reason for your choice.
Data structure .....................................................................................................................
Reason ...............................................................................................................................
.......................................................................................................................................[2]
Page | 255
Practice Questions
Q 1a) Declare an array called Marks whose index values ranges from 0 to 999 and whose element
type is integer
............................................................................................................................................................
.....................................................................................................................................................[1]
b) Initialise the array declared in part a, all values in the array should be equals to 0.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
Q3a) Write a pseudo code that uses an array to store marks of 10 students of a class. Enter
marks of each student. After input all the marks output the list of marks.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Page | 256
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
b) Calculate the average marks of the class by traversing the array.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
c) Output the smallest and the greatest marks of the class by traversing the array.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
d) Create a second array to input and store name of students of the class. Output the list of name
of students and their marks by traversing the two arrays. At the end of list print average marks of
the class.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Page | 257
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
Q 4a) Write a program to input name and telephone of your friends in
two one dimension arrays. Each array can store up to 5 elements. Name[1:5] Tel[1:5]
1 1
...............................................................................................................
2 2
............................................................................................................ ..
3 3
..............................................................................................................
4 4
.............................................................................................................
5 5
.............................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[3]
b) Write a program that inputs the name of your friend search in the array and output telephone
number.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[3]
c) After the user selects a name, give him option to display and change the telephone number in
the array. After change of telephone number, output the entire list.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Page | 258
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
Q 5b ) Declare 3rd array and copy data items from 1st array 1 75 1 88
Q 7a) Write pseudocode to show how the RAND() function can be used to generate a single integer
in the range 1 to 150.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [3]
b) Declare an array to store 10 integers then store random numbers between 1 to 50 in the array
using RAND() functions.
............................................................................................................................................................
............................................................................ ................................................................................
.................................... ........................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
c) Declare another array to store 10 integers then store random numbers between 1 to 50 in the
array using RAND() functions. All random numbers must be unique.
............................................................................................................................................................
............................................................................ ................................................................................
.................................... ........................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Page | 261
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[4]
Q 8) John works in a supermarket, where he is given a task to find which item has the highest
price and which item has the lowest price. There are 900 items in the supermarket.
Declare suitable arrays to store name and price of each product.
In put price of each product with its name.
Output the number of items which have a price greater than 100and number of items which
have price less than 50.
Output the highest and the lowest price.
……………………………………………………....................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [4]
...............................................................................................................................................[2]
(b) The programmer writes a program from the following pseudocode design.
x0
FOR DayNumber1 TO 7
IF SalesX[DayNumber] + SalesY[DayNumber] >= 10
THEN
X X + 1
OUTPUT SalesDate[DayNumber]
ENDIF
ENDFOR
OUTPUT x
Trace the execution of this pseudocode by completing the trace table below. [4]
x DayNumber OUTPUT
0
Page | 263
4 The pseudocode algorithm shown should allow numbers to be entered and should allow 50
numbers to be stored in an array.
Count ← 0
REPEAT
INPUT Values[Count]
Count ← Count + 1
UNTIL Count = 0
(a) Explain why the algorithm will never end.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [2]
(b) Re-write the original pseudocode so that it terminates correctly and also prevents numbers
below 100 from being stored in the array Values[ ]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
(c) Describe how you could change your pseudocode in part (b) so that it prevents numbers
below 100 and above 200 from being stored in the array Values[ ]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [2]
Page | 267
Q 12) Summer 20 P22
5 Arrays are data structures used in programming. Explain what is meant by the terms dimension
and index in an array. Use examples of arrays in your explanations.
Dimension ........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Index ................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..................................................................................................................................................... [3]
Q 13) Winter 20 P22
4 This pseudocode algorithm allows 5000 numbers to be entered and stored in an array
called Number.
FOR Count = 1 TO 5000
INPUT Number[Count]
NEXT Count
Extend and re-write the algorithm using pseudocode to also count and output how many of the
numbers stored in the array are greater than 500, using the variable Higher. Only output Higher
once with an appropriate message.
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Page | 268
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
.................................................................................................................................................... [6]
Page | 269
Marking Scheme of Practice Questions
Practice Questions
Q 1a) Declare an array called Marks whose index values ranges from 0 to 999 and whose element
type is integer [1]
DECLARE Marks[0:999] AS INTEGER
b) Initialise the array declared in part a, all values in the array should be equals to 0. [2]
FOR Count 0 TO 999
Marks[Count] 0
NEXT Count
Q3a) Write a pseudo code that uses an array to store marks of 10 students of a class.
Enter marks of each student.
After input all the marks output the list of marks. [4]
DECLARE Marks[1:10] AS REAL
DECLARE Count : INTEGER
FOR Count 1 TO 10
INPUT Marks[Count]
NEXT Count
FOR Count 1 TO 10
PRINT Marks[Count]
NEXT Count
b) Calculate the average marks of the class by traversing the array. [3]
Total 0
FOR Count 1 TO 10
Total Total + Marks[Count]
NEXT Count
Average Total / 10
PRINT Average
Page | 270
d) Output the smallest and the greatest marks of the class by traversing the array. [3]\
Greatest 0
Smallest 100
FOR Count 1 TO 10
IF Marks[Count] > Greatest THEN Greatest Marks[Count]
IF Marks[Count] < Lowest THEN Lowest Marks[Count]
NEXT Count
d) Create a second array to input and store name of students of the class.
Output the list of name of students and their marks by traversing the two arrays.
At the end of list print average marks of the class. [4]
FOR Count 1 TO 10
PRINT Name[Count], Marks[Count]
Total Total + Marks[Count]
NEXT Count
Average Total / 10
PRINT Average
Q 4a) Write a program to input name and telephone of your friends in two one dimension arrays.
Each array can store up to 5 elements. [3]
b) Write a program that inputs the name of your friend search in the array and output telephone
number. [3]
Q 5b ) Declare 3rd array and copy data items from 1st array into 3rd array in reverse order.
DECLARE Num3[1:5] AS INTEGER
DECLARE Index : INTEGER
Index 5
FOR Count 1 TO 5
Num3[Index] Nu1[Count]
Index Index – 1
NEXT Count
Page | 272
Q 6) An array has 10 integers. Create two new arrays and copy 1 st 5 integers from 1st array into 2nd
array and last integers in 3rd array.
DELCARE Num2[1:5] AS INTEGER
DECLARE Num3[1:5] AS INTEGER
FOR Count 1 TO 5
Num2[Count] Num1[Count]
Num3[Count] Num1[Count+5]
NEXT Count
Q 7a) Write pseudocode to show how the RAND() function can be used to generate a single integer
in the range 1 to 150.
Num INT(RAND(150)+1)
b) Declare an array to store 10 integers then store random numbers between 1 to 50 in the array
using RAND() functions.
DECLARE Num[1:10] AS INTEGER
DECLARE Count : INTEGER
FOR Count 1 TO 10
Num[Count] INT(RAND(50)+1)
NEXT Count
c) Declare another array to store 10 integers then store random numbers between 1 to 50 in the
array using RAND() functions. All random numbers must be unique.
Q 8) John works in a supermarket, where he is given a task to find which item has the highest
price and which item has the lowest price. There are 900 items in the supermarket.
Declare suitable arrays to store name and price of each product.
Input price of each product with its name.
Output the number of items which have a price greater than 100 and number of items which
have price less than 50.
Output the highest and the lowest price. [4]
DECLARE ProductName[1:900] OF STRING
DECLARE Price[1:900] OF REAL
DECLARE Highest, Lowest : REAL
DECLARE Count, Above100, Below50 : INTEGER
Above100 0
Below50 0
Highest 0
Lowest 99999
FOR Count 1 TO 900
INPUT ProductNAme[Count]
INPUT Price[Count]
IF Price[Count] > 100 THEN Above100 Above100 + 1
IF Price[Count] < 50 THEN Below50 Below50+1
IF Price[Count] > Highest THEN Highest Price[Count]
IF Price[Count] < Lowest THEN Lowest Price[Count]
NEXT Count
PRINT Highest, Lowest, Above100, Below50
4 The pseudocode algorithm shown should allow numbers to be entered and should allow 50
numbers to be stored in an array.
Count ← 0
REPEAT
INPUT Values[Count]
Count ← Count + 1
UNTIL Count = 0
(a) Explain why the algorithm will never end.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [2]
(b) Re-write the original pseudocode so that it terminates correctly and also prevents numbers
below 100 from being stored in the array Values[ ]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
(c) Describe how you could change your pseudocode in part (b) so that it prevents numbers
below 100 and above 200 from being stored in the array Values[ ]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [2]
Page | 280
Q 12) Summer 20 P22
5 Arrays are data structures used in programming. Explain what is meant by the terms dimension
and index in an array. Use examples of arrays in your explanations.
Dimension ........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Index ................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..................................................................................................................................................... [3]
Q 13) Winter 20 P22
4 This pseudocode algorithm allows 5000 numbers to be entered and stored in an array
called Number.
FOR Count = 1 TO 5000
INPUT Number[Count]
NEXT Count
Extend and re-write the algorithm using pseudocode to also count and output how many of the
numbers stored in the array are greater than 500, using the variable Higher. Only output Higher
once with an appropriate message.
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Page | 281
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
.................................................................................................................................................... [6]
Page | 282
Chapter 15
Database
2.3 Database
• definea single-table database from given data storage requirements
• choose and specify suitable data types
• choose a suitable primary key for a database table
• perform a query-by-example from given search criteria
A database is persistent as it uses secondary storage (HDD or SSD) as work area rather than
memory. It directly stores records on disc as soon as it is entered, automatically.
The collected information could be in any number of formats (electronic, printed, graphic, audio,
statistical, combinations). There are physical (paper/print) and electronic databases.
Examples:
phone book
address book
Census Bureau data
Data in a database table is organised into rows (records) and columns (fields). Each record in a
relational database table corresponds to an entity. In the example table of 'Students' above there
are 5 records. Each record corresponds to an individual student. Note that although there are two
students called Philip Barker with the same date of birth, they have different Student IDs and are
different students.
Database Record
Collection of related fields is called a record or tuple. Records are organized in rows.
Database field
An attribute is a piece of information or a characteristic of an entity. Attributes of entities are
represented in database tables by fields (columns). A field stores one item of data for a record. In
the table above, each student is represented in the relational database by a record and the
student attributes are stored in the following fields:
Student ID
Forename
Surname
Date of Birth
Fields have the following characteristics:
Each field in a table has a unique name. Note, however, that the same field name can occur in
other tables of the same relational database.
Each field stores a single item of data - For example, a field called Date of Birth would store no
more than one date of birth value.
Each field has a particular data type – for example, text, Boolean, integer, date/time, etc.
Each field can have its own validation rules - these ensure that data recorded in the field is of the
Page | 284
right type and format.
Data types
Different data types are identified so that a computer can store and process the data appropriately.
Data types include:
text ()
number (numeric) may include:
o Auto number
o Currency
date/time
Boolean (or Yes/No).
Primary Keys
Each table has a primary key. This is a field chosen so that it can uniquely identify each record.
Sometimes an existing attribute can be used because it is unique but most of the time some sort
of ID is created. Primary keys can be used to link to foreign keys in other tables. A foreign key is
the primary key in a different table and it is not necessarily unique.
Example Question:
A picture gallery owner has decided to set up a database to keep information about the pictures he
has for sale. The database table, PICTURE, will contain the following fields:
Title; Artist; Description; Catalogue Number; Size (area in square centimeters); Price; Arrived (date
picture arrived at gallery); Sold (whether picture is already sold)
(a) (i) State what data type you would choose for each field.
Title: ....................................................................................................................................................
Artist: ..................................................................................................................................................
Description: ........................................................................................................................................
Catalogue Number: ............................................................................................................................
Size: ...................................................................................................................................................
Price: ..................................................................................................................................................
Arrived: ...............................................................................................................................................
Sold: ..............................................................................................................................................[4]
(ii) State which field you would choose for the primary key
………………………………………………………………………………...........................................[1]
Page | 285
Query
The prime function of a relational database is to store data in an organised way so that users can
interrogate (search) and manipulate (sort) the data. The interrogation of a database is called
querying the database and a question used to interrogate the data is called a query.
Query by Example (QBE) is a database query language for relational databases. It was devised
by Moshé M. Zloof at IBM Research during the mid-1970s, in parallel to the development of SQL.
It is the first graphical query language, using visual tables where the user would enter commands,
example elements and conditions.
Database user-interface in which the user fills out a form to retrieve data. The database makes the
search on the basis of the example(s) provided by the user.
The query to find students in form 10B
A complex query looks for data in two or more fields and uses the logical operators OR, AND or
NOT.
The following example uses a complex query to find all of the pupils in Form 10B who were born
before 1995. This query uses the logical operator AND:
(Form = “10B”) AND (Date of Birth < 01/01/1995).
Operators can be used to refine search results.
Operator Meaning
= Equals
Below is a new complex query that uses the logical operator OR to find pupils who are in Form
10A or Form 10C: (Form = “10A” OR “Form = “10C”) this time, in the query definition there will be
two criteria lines. The query and its results are shown below:
Wildcards in Queries
Wildcard characters can be used in database queries. For example you may want a list of all
pupils born in November, or all of the pupils whose surname starts with a ‘C’. Wildcard searches
allow you to specify the part of the data that you know and leave the data handling software to fill
in the blanks.
Surname Like “C*” would find all records where the surname begins with a C.
Page | 287
Quick Revision Questions:
Q 1) Define following terms:
Database: ...........................................................................................................................................
............................................................................................................................................................
Entity: .................................................................................................................................................
............................................................................................................................................................
Table: .................................................................................................................................................
............................................................................................................................................................
Field/ Attribute: ..................................................................................................................................
............................................................................................................................................................
Record/Tupple: ...................................................................................................................................
............................................................................................................................................................
Primary Key: ......................................................................................................................................
...........................................................................................................................................................
Query: ................................................................................................................................................
............................................................................................................................................................
Page | 288
Candidate Example Response Summer 2015 P22
6 A database, MARKS, was set up to record the test results for a class of students. Part of the
database is shown below.
Criteria: =Y = 10:00
or:
Explain why the query-by-example is incorrect, and write a correct query-by-example. [7]
Explanation .........................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 295
Page | 296
Page | 297
Page | 298
Examination Questions
14.9 Specimen paper 2016 P2
7 A database ELEMENTS was set up to show the properties of certain chemical elements. Part of
the database is shown below.
6 A database, MARKS, was set up to record the test results for a class of students. Part of the
database is shown below.
(d) Complete the query-by-example grid below to select and show the student names only of all
students with less than 40 marks in both Maths and English. [3]
(b) Give a validation check that you can perform on each of these fields. Each validation check must
be different.
Catalogue Number ............................................................................................................................
Size ....................................................................................................................................................
Price ...................................................................................................................................................
Arrived ............................................................................................................................................[4]
(c) Complete the query-by-example grid below to select and show the Catalogue Number, Title and
Price of all unsold pictures by the artist ‘Twister’.
(c) Complete the query-by-example grid below to select and show the Boat Name, Model and Day
Price of a day’s hire for all boats with 4 seats and an Engine Power of more than 100 hp.
Page | 304
14.14 Summer 2016 P21 &P23
6 A database, STAFFPHONE, was set up to show the telephone extension numbers for members
of staff working in a department store.
Name Department Extension number
Jane Smith Toys 129
Sue Wong Books 124
David Chow Toys 129
Amy Tang Household 123
Joe Higgs Books 124
Jane Smith Shoes 125
Adel Abur Shoes 125
Peter Patel Toys 129
(a) Explain why none of the fields in the database can be used as a primary key.
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[2]
(b) State a field that could be added as a primary key.
...........................................................................................................................................................
Give a reason for choosing this field.
............................................................................................................................................................
...................................................................................................................................................... [2]
(c) Use the query-by-example grid below to provide a list of all members of staff, in alphabetical
order, grouped by department. [5]
Page | 305
14.15 Summer 2016 P22
7 A database, SOFASELECT, was set up to show the prices of suites, sofas and chairs for sale from an
online furniture warehouse. Part of the database is shown below.
......................................................................................................................................[1]
(b) State which field you would choose for the primary key.
...........................................................................................................................................................................
Give a reason for choosing this field.
...........................................................................................................................................................................
.......................................................................................................................................................................[2]
(c) State the data type you would choose for each of the following fields.
Number of Seats ..............................................................................................................
Price in $ ......................................................................................................................[2]
(d) The query-by-example grid below selects all the furniture in cream leather.
Field
Table
Sort
Show:
Criteria
or
Page | 307
(c) The query-by-example grid below selects all the productions with more than 100 seats left in
either the stalls or the circle.
(b) Complete the query-by-example grid below to select all Desktop devices that were either
purchased before 31/12/2016 or cost under $1000. Only show the Device ID and DeviceType.
Page | 310
14.19 Summer 2017 P21
7 A television (TV) store has a database table, TVSTOCK, for its new range of televisions. The
table stores the screen size of each TV, whether it will show 3D, whether the screen is curved or
flat, if the internet is available on the TV, if it has a built-in hard disk drive and the price. Part of the
database table is shown below.
5 A database table, SHEEP, is used to keep a record of the sheep on a farm. Each sheep has a
unique ear tag, EARnnnn; n is a single digit. The farmer keeps a record of the date of birth, the
gender and the current weight of each sheep in kilograms.
(a) Identify the four fields required for the database. Give each field a suitable name and data
type. Provide a sample of data that you could expect to see in the field. [8]
Field 1 name.......................................................................................................................................
Data type ............................................................................................................................................
Data sample .......................................................................................................................................
Field 2 name........................................................................................................................................
Data type ............................................................................................................................................
Data sample .......................................................................................................................................
Field 3 name........................................................................................................................................
Data type ............................................................................................................................................
Data sample .......................................................................................................................................
Field 4 name.......................................................................................................................................
Data type ...........................................................................................................................................
Data sample ......................................................................................................................................
(b) State the field that you would choose as the primary key.
......................................................................................................................................................[1]
(c) Using the query-by-example grid below, write a query to identify the ear tags of all male sheep
weighing over 10 kilograms. Only display the ear tags. [3]
Page | 312
14.21 Winter 2017 P21
6 A wildlife park has a database table, called LIVESTOCK, to classify and record its animal
species. Part of the database table is shown.
Species Classification Diet Legs
Giraffe Mammal Herbivore 4
Elephant Mammal Herbivore 4
Crocodile Reptile Carnivore 4
Ostrich Bird Omnivore 2
Gorilla Mammal Herbivore 2
Bear Mammal Omnivore 4
Rhinoceros Mammal Herbivore 4
Hippopotamus Mammal Herbivore 4
Flamingo Bird Omnivore 2
Lion Mammal Carnivore 4
Turtle Reptile Omnivore 4
Penguin Bird Carnivore 2
(a) Suggest another appropriate field that could be added to this database by stating its name and
data type. State its purpose and give an example of the data it could contain.
Field name ................................................................................................................................
Data Type .................................................................................................................................
Purpose ....................................................................................................................................
...................................................................................................................................................
Example of data ...................................................................................................................[2]
(b) Use the query-by-example grid below to provide a list of all four legged mammals that are
herbivores, sorted alphabetically by species, with only the species displayed.
Page | 313
14.22 Winter 2017 P22
6 A database table, TRAIN, is to be set up for a railway company to keep a record of the engines
available for use. Each engine has a unique number made up of 5 digits, nnnnn. The engines are
classified as freight (F) or passenger (P) together with a power classification that is a whole
number between 0 and 9, for example F8. The railway company keeps a record of the date of the
last service for each engine.
(a) Identify the three fields required for the database. Give each field a suitable name and data
type. Provide a sample of data that you could expect to see in the field.
Field 1 Name ............................................................................................................................
Data type ..................................................................................................................................
Data sample .............................................................................................................................
Field 2 Name ............................................................................................................................
Data type ..................................................................................................................................
Data sample .............................................................................................................................
Field 3 Name ............................................................................................................................
Data type ..................................................................................................................................
Data sample .........................................................................................................................[6]
(b) State the field that you should choose as the primary key.
...............................................................................................................................................[1]
(c) Using the query-by-example grid below, write a query to identify all passenger engines that
have not been serviced in the past 12 months. Only display the engine numbers. [3]
Page | 314
14.23 March 2018 P22 (India)
6 A database table, JEWEL, is used to keep a record of jewellery for sale in a shop. Each item of
jewellery can be made of silver, platinum or gold metal. The shop stocks rings, bracelets and
necklaces. The number in stock and the price is also stored.
(a) Identify the four fields required for the database. Give each field a suitable name and data
type. Explain why you chose the data type for each field.
Comments on Question 6
(a) Nearly all candidates identified some appropriate fields and could also provide a suitable data
type and explanation. Many candidates provided excellent answers worth full marks.
(b) Nearly all candidates gave a correct explanation as to why none of the fields were suitable use
as a primary key.
(c) Nearly all candidates correctly identified the fields required in the query-by-example grid. Most
candidates correctly identified which fields to show. Many candidates provided suitable criteria
to identify that only details of silver bracelets were required.
Page | 315
14.24Summer 2018 P21
6 A database table, PERFORMANCE, is used to keep a record of the performances at a local
theatre.
Show Sold
Type Title Date
Number Out
SN091 Comedy An Evening at Home 01 Sept Yes
SN102 Drama Old Places 02 Oct No
SN113 Jazz Acoustic Evening 03 Nov No
SN124 Classical Mozart Evening 04 Dec Yes
SN021 Classical Bach Favourites 01 Feb Yes
SN032 Jazz 30 Years of Jazz 02 Mar Yes
SN043 Comedy Street Night 03 Apr No
SN054 Comedy Hoot 04 May No
(b) Give two validation checks that could be performed on the Show Number field.
Validation check 1 .....................................................................................................................
...................................................................................................................................................
Validation check 2 .....................................................................................................................
............................................................................................................................................... [2]
(c) Using the query-by-example grid, write a query to identify jazz performances that are not sold
out. Only display the date and the title. [4]
Page | 316
14.25Summer 2018 P21
6 A database table, TREES, is used to keep a record of the trees in a park. Each tree is given a
unique number and is examined to see if it is at risk of dying. There are over 900 trees; part of the
database table is shown.
Tree
Type Map Position Age in Years At Risk
Number
TN091 Acacia A7 250 Y
TN172 Olive C5 110 N
TN913 Cedar B9 8 N
TN824 Banyan A3 50 Y
TN021 Pine D5 560 Y
TN532 Teak C8 76 Y
TN043 Yew B1 340 N
TN354 Spruce D4 65 N
TN731 Elm B10 22 Y
TN869 Oak C9 13 N
TN954 Pine E11 3 N
(a) State the number of fields in the table.
...............................................................................................................................................[1]
(b) The tree numbering system uses TN followed by three digits. The numbering system will not
work if there are over 1000 trees.
Describe, with the aid of an example, how you could change the tree numbering system to allow
for over 1000 trees. Existing tree numbers must not be changed.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[2]
(c) Using the query-by-example grid, write a query to identify at risk trees over 100 years old.
Display only the type and the position on the map. [4]
Page | 317
14.26 Winter 2018 P21
6 A database table, PORTRAIT, is used to keep a record of the portraits available from a
photographic studio. Each portrait has a unique reference number PICnnn, where n is a single
digit, for examplePIC123. The studio keeps a record of the size (for example 20 × 15), the type
(black and white or colour), and the price in dollars.
(a) Complete the table to show the most appropriate data type for each of the fields.[4]
Field Data type
Reference Number
Size
Type
Price in $
(b) The results from the query-by-example grid should show the reference number, price, type
and size of all portraits under $50. Identify the three errors in the query-by-example grid.
Criteria: >50.00
or:
Error 1 ...............................................................................................................................................
…………..............................................................................................................................................
Error 2 ................................................................................................................................................
………….............................................................................................................................................
Error 3 ...............................................................................................................................................
…..................................................................................................................................................[3]
Page | 318
14.27 Winter 2018 P23
6 An online fruit tree specialist sells fruit trees in various sizes. A database table, TREETAB,
shows the tree type and, for each size, the price and whether they are in stock.
Criteria: <10.00
or:
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.....................................................................................................................................................[4]
(d) Using the following query-by-example grid, write a query to identify all types of the fruit trees
that are out of stock for all three sizes. Make sure the type of the tree and the various ‘in stock’
fields are shown. The trees should be listed in alphabetical order by type.
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 320
14.28 March 2019 P22
6 A database table, BIKETYRES, is used to keep a record of tyres for sale in a cycle shop.
Tyres are categorised by width and diameter in milimetres, whether they have an inner tube and
the type of terrain for which they are designed.
Tyre Code Width Diameter Tube Terrain Stock Level
SLTT 23 700 YES Asphalt 18
MLNT 24 700 NO Asphalt 23
LLNT 28 700 NO Asphalt 19
SLTM 23 700 YES Mixed 22
MLTM 24 700 YES Mixed 14
LLTM 28 700 YES Mixed 12
SLTH 23 700 YES Hard 10
MLTH 24 700 YES Hard 5
LLNH 28 700 NO Hard 7
SLNM 23 700 NO Mixed 12
MLNM 24 700 NO Mixed 22
LLNM 28 700 NO Mixed 18
SSNT 23 650 NO Asphalt 10
MSNT 24 650 NO Asphalt 8
SSTM 23 650 YES Mixed 5
MSNM 24 650 NO Mixed 4
The query-by-example grid below displays the tyre code and the stock level of all 28mm width
tyres suitable for mixed terrain.
Field: Tyre Code Stock Level Width Terrain
Table: BYKETYRES BYKETYRES BYKETYRES BYKETYRES
Sort:
Show:
Alter the query to show the tyre code and stock level in ascending order of stock level for all
24mm asphalt terrain tyres. Write the new query in the following query-by-example grid.
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 321
14.29 Summer 2019 P21
5 The table, BEVERAGES, shows the number of calories in 100ml of a range of popular
beverages. It also shows the availability of these drinks in a can, a small bottle and a large bottle.
Calorie Ca Small Large
BevNo BevName
s n Bottle Bottle
Bev01 Cola 40 Yes Yes Yes
Bev02 Lime 45 Yes No Yes
Bev03 Energy Drink 1 52 Yes Yes No
Bev04 Energy Drink 2 43 Yes No No
Bev05 Mango 47 Yes No Yes
Bev06 Lemon Iced Tea 38 Yes No Yes
Bev07 Lemonade 58 Yes Yes Yes
Bev08 Orange Juice 46 Yes Yes No
Bev12 Apple Juice 50 Yes Yes No
Bev15 Chocolate Milk 83 Yes Yes No
(a) Give a reason for choosing BevNo as the primary key for this table.
...................................................................................................................................................
............................................................................................................................................. [1]
(b) State the number of records shown in the table BEVERAGES.
............................................................................................................................................. [1]
(c) List the output that would be given by this query-by-example.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Page | 322
............................................................................................................................................. [3]
(d) Complete the query-by-example grid to output a list showing just the names and primary keys
of all the beverages with a calorie count greater than 45. The list should be in alphabetical
order of names. [4]
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 323
14.30 Summer 2019 P22
6 A database table, FLIGHT, is used to keep a record of flights from a small airfield. Planes can
carry passengers, freight or both. Some flights are marked as private and only carry passengers.
Departure
Flight number Plane Notes Passengers
time
FN101 Caravan 1 Private passenger flight 08:00 Y
CN101 Caravan 2 Freight only 08:30 N
CN102 Piper 1 Freight only 09:00 N
FN104 Piper 2 Passengers only 09:20 Y
FN105 Piper 1 Freight and passengers 10:00 Y
FN106 Caravan 1 Passengers only 10:30 Y
CN108 Caravan 2 Freight only 08:00 N
CN110 Lear Private passenger flight 08:00 Y
(a) State the field that could have a Boolean data type.
Field .................................................................................................................................... [1]
(b) A query-by-example has been written to display just the flight numbers of all planes leaving
after 10:00 that only carry passengers.
Field: Departure
Flight number Passengers
time
Table: FLIGHT FLIGHT FLIGHT
Sort:
Show:
Criteria: =Y = 10:00
or:
Explain why the query-by-example is incorrect, and write a correct query-by-example. [7]
Explanation .........................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 324
Winter 19 P23
7 A teacher has decided to use a database table as her mark book for her Computer Science
class, which she has called MARKBOOK. For each student, the following data will be recorded:
first name, last name, their year 10 test score and their year 11 test score. The class has 32
students.
(a) State the number of fields and records required for this database.
Number of Fields ......................................................................................................................
Number of Records ..................................................................................................................[2]
(b) The data in MARKBOOK is stored under category headings: LastName, FirstName,
Y10TestScore and Y11TestScore.
State, with a reason, whether any of these headings would be suitable as a primary key.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [2]
(c) Complete the query-by-example grid to only display the first name, last name and year 10
test score of each student who achieved 50 or more in their year 10 test. The output should
be in test score order with the highest marks at the top of the list.
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 325
Winter 19 P22
7 A database table, SALES, is used to keep a record of items made and sold by a furniture maker.
Item umber Order number Notes Amount Status
CH001 1921 Smith – six dining chairs 6 Delivered
TB003 1921 Smith – large table 1 In progress
CH001 1924 Hue – extra chairs 4 In progress
CH003 1925 For stock 2 Cancelled
BN001 1927 Patel – replacement bench 1 Not started
ST002 1931 Sola – small table 1 Delivered
CH003 1927 Patel – eight dining chairs with arms 8 Not started
TB003 1927 Patel – large table 1 Not started
(a) Explain why the field Item number could not be used as a primary key.
...................................................................................................................................................
............................................................................................................................................. [1]
(b) A query-by-example has been written to display only the order number and item numbers of
any items in progress or not started.
Criteria:
or:
Page | 326
March 20 P22
5 A database table, BOX, is used to keep a record of boxes made and sold by a craftsman. Boxes
are categorised by:
SIZE – small, medium or large
SHAPE – brief description for example ‘star shaped’
WOOD – maple, beech, walnut or ebony
PRICE – price in $
SOLD – whether the box is sold or not.
A database management system uses these data types:
Text Number Currency Boolean
(a) Select the most appropriate data type for each field from the four types shown. State the
reason why you chose the data type.
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 327
(ii) The query-by-example grid from part (b)(i) needs to be changed to show both walnut and beec
h boxes and display the wood used. Only one column needs to be changed. [2]
Write the changed column.
Field:
Table:
Sort:
Show:
Criteria:
or:
6 A garden centre sells garden tools and stores details of these in a database table named TOOLS.
Code is the primary key in the TOOLS table.
Code Description Price ($) Quantity_Stock Quantity_Ordered
GFLG Garden Fork 50.00 1 50
GSLG Garden Spade 50.00 11 0
GHLG Garden Hoe 45.00 8 0
HFSM Hand Fork 9.99 42 0
HSSM Hand Spade 9.99 40 0
HWSM Hand Weeder 9.99 11 0
HS20 Hose (20 metres) 45.00 10 0
HS35 Hose (35 metres) 60.00 2 0
HS50 Hose (50 metres) 75.00 20 60
YBLG Yard Brush 24.99 100 0
LMHD Lawn Mower 99.99 5 0
LMBT Lawn Mower (Battery) 249.99 7 0
LMPT Lawn Mower (Petrol) 349.99 10 25
TRBT Edge Trimmer (Battery) 79.99 15 0
TRPT Edge Trimmer (Petrol) 59.99 20 0
SHSM Shears 40.00 40 0
HCSM Hedge Clippers 40.00 45 0
(a) State the purpose of the primary key in the TOOLS table.
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [1]
Page | 328
(b) List the output from the data shown in the table TOOLS that would be given by this query-by-example.
Field: Code Description Price ($) Quantity_Stock Quantity_Ordered
Table: TOOLS TOOLS TOOLS TOOLS TOOLS
Sort: Descending
Show:
Criteria:
or:
Page | 329
Summer 20 P22
6 A database table, JUICE, is used to keep a record of cartons of fresh fruit juice available for sale.
Juice code Fruit 1 Fruit 2 Size Volume (ml) Stock level
LMO10 Mango Orange Large 1000 18
MOO05 Orange Orange Medium 500 8
SAM02 Apple Mango Small 200 25
SAA02 Apple Apple Small 200 50
SPP02 Pineapple Pineapple Small 200 10
MMM05 Mango Mango Medium 500 12
LMM10 Mango Mango Large 1000 5
MGG05 Guava Guava Medium 500 5
SMO02 Mango Orange Small 200 7
MOP05 Orange Pineapple Medium 500 12
LAA10 Apple Apple Large 1000 32
SGO02 Guava Orange Small 200 10
LPP10 Pineapple Pineapple Large 1000 3
LOO10 Orange Orange Large 1000 25
SOO02 Orange Orange Small 200 40
(a) Identify a suitable field to use as the primary key. State a reason for your choice.
Field ..........................................................................................................................................
Reason ......................................................................................................................................
................................................................................................................................................... [2]
(b) Complete the query-by-example grid to display only the stock level and size of all cartons containing only apple
juice.
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 330
Winter 20 P22
7 The table AUDIOPARTS stores the part number, description, cost and quantity in stock of the
items sold by a music shop.
PartNum Description Cost Quantity
A01 Compact Amplifier Case 50.00 15
A02 Deluxe Amplifier Case 75.00 1
A03 Amplifier Standard 79.99 48
A04 Amplifier Midrange 149.99 50
A05 Amplifier Megablaster 299.99 48
S01 Tweeter 59.99 10
S02 Midrange Woofer 99.99 0
S03 Subwoofer 139.99 16
S04 Tower Speaker Basic 159.99 25
S05 Tower Speaker Skyscraper 219.99 9
S06 Centre Speaker 149.99 25
S07 Soundbar 89.99 2
S20 Soundbar 129.99 0
S21 Ceiling Surround Speaker 75.00 15
S22 Ceiling Full Range Speaker 100.00 1
S25 Surround Speaker 100.00 60
T19 Speaker Stands (Pair) 75.00 60
(a) State the number of records in the table AUDIOPARTS
............................................................................................................................................. [1]
(b) Identify the field that is most suitable to be a primary key and give a reason for your choice.
Fieldname .................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
............................................................................................................................................ [2]
(c) Complete the query-by-example grid to show the items where the quantity in stock is fewer
than 10. Show all the fields from the database table in descending order of cost. [4]
Field:
Table:
Sort:
Show:
Criteria:
or:
Page | 331
Chapter 15
Introduction to Logo
LOGO is a programming language that was developed at the MIT Artificial Intelligence Lab in the
1970s. It was designed to be used as an introduction for people to both programming and artificial
intelligence. However, while it is easy to learn, Logo is a powerful language. Once you know the
basics, Logo can be used to do extremely complicated things. Logo code has been used in
telecommunications, multimedia software and robotics. Finally Logo is FUN!
Turtle
When Logo started, the Turtle was a robotic creature which sat on the floor and was directed to
move around by a user typing commands on a computer. Today the turtle is represented by an
icon on the screen and can be made to draw on the screen using the same commands. In some
environments the turtle looks like a turtle (with head, tail and feet) in others represent the turtle
with a triangle.
Logo Commands
The following are some of the most use full commands in the Logo Language, which you will want
to become familiar with:
FORWARD - Follow this command with a number (such as: 10 or 1000.) A small number will
cause the turtle to move forward a short distance. A larger number will cause it to move further. If
you select a large enough number the turtle will go off the canvas and wrap around to the other
side.
BACK- Follow this command with a number, the same as FORWARD, only this time the turtle will
move backwards.
RIGHT - Follow this command with a number between 0 and 360. The turtle will turn right specified
number of degrees.
LEFT - Follow this command with a number between 0 and 360. This command is the same as
RIGHT only it will turn the turtle left, not right.
PENUP - This command will cause the turtle to pick up its “pen” up so that you can move the turtle
without drawing a line.
PENDOWN - This is the command you would use to put the “pen” back down so you can draw
again.
SETPENCOLOR - You can change the colour your turtle draws in. Follow the command with a
number to get different colours. For example
“SETPENCOLOR 0” would give you a black pen.
CLEAN- This command will erase the canvas
HOME- This command will move the turtle back to the centre of the canvas
The Repeat Command
You can get your turtle to do one (or several) things repeatedly, without typing them again and
again using the REPEAT command. Typing
REPEAT 4 [FORWARD 10]
Would cause the turtle to move forward 10 spaces, 4 time. So, in total the turtle would move
forward 40 spaces. Now Try These. Type:
Page | 332
REPEAT 4 [FORWARD 50 RIGHT 90]
You should get a square. For a bigger square try replacing 50 with 100 or 200.Type:
REPEAT 360 [FORWARD 2 LEFT 1]
You should get a circle. For a smaller circle try replacing 2 with 1. Can you make a bigger one?
Your First Program
That’s a lot to type every time you want to make a square or circle though. Can it be easier? YES.
You can teach Logo what a square (or a circle, or a flower) is by making it program. Try typing:
TO SQUARE
REPEAT 360 [FORWARD 80 LEFT 90]
END
Now type SQUARE and see what happens.
How would you write the program CIRCLE?
How would you write the program CIRCLE_AND_SQUARE to make a drawing that looks like this
(where the black triangle is the turtle at the end)?
TO CIRCLE_AND_SQUARE
HOME
CLEAN
CIRCLE
FORWARD 52
REPEAT 3 [ LEFT 90 FORWARD 104]
LEFT 90
FORWARD 52
END
Complete the set of instructions to draw the shape shown above (in bold lines).
Pen Down ......................................................................................................................................
Repeat 2.........................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
Page | 334
...........................................................................................................................................................................
...........................................................................................................................................................................
.....................................................................
1 mark for each block of code between dotted lines. (e.g. use of REPEAT and BACKWARD functions). If an error
occurs in the code, try to find a correct code sequence later on in the answer (in cases such as this, it is often easier to
work backwards from last statement looking for correct blocks).