Algorithmpseudo Code
Algorithmpseudo Code
Introduction
2.1.1 Problem-solving and design
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)
Comment on the effectiveness of a given solution
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.
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.
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.
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.)
2. REAL:
A number capable of containing a fractional part like Weight may contain fractional Part
For example 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 )
For example ‘A’, ‘$’, ‘5’
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:
As in programming, it is good practice to use identifier names that describe the variable, procedure
or function they refer to. Single letters may be used where these are conventional (such as i and j
when dealing with array indices, or X and Y when dealing with coordinates) as these are made clear
by the convention.
Keywords should never be used as variables.
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.
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
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 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.
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)
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]
Summer2019 P21
2 Describe each of the following data types used in programming. In each case, give an example
of a piece of data to illustrate your answer. Each example must be different.
Char...................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
String.................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Integer ...............................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Real .................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Date .................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
Boolean..............................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................[12]
Q 12 Winter 2019 P22
6 Explain why constants, variables and arrays are used in programming.
Constants .........................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
Variables ..........................................................................................................................................
..........................................................................................................................................................
.........................................................................................................................................................
.................................................................................................................................................... [2]
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 1 st 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.
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.
Selection:
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
Largest Number
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
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
Yes
IS
Marks>=50 Result “Pass”
?
No
Result “Fail”
PRINT Result
End
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
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
IF…THEN…ELSE…ENDIF CASE….OF….OTHERWISE…ENDCASE
Problem: input marks and Problem: input marks and output grade
output result
Problem 3: Input marks and output Result, the passing marks is 40 or above.
HW 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.
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
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.
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
Problem 9) March 2018 P22 (India)
5 Explain the difference between the programming concepts of sequence and selection. Include
an example of a programming statement for each concept in your explanation. [4]
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
Comments on Question 5
Candidates found the explanation of the difference between the programming concepts sequence
and selection challenging, with few candidates identifying that programming statements in a sequ
ence were executed one after another whilst selection meant that the path through the program de
pends on the result of a question. Candidates were more successful in providing suitable examples
of programming statements.
Common errors included confusing sequence or selection with iteration.
Problem 10) Winter 2018 P22
4 A programmer wants to test that the readings from 2000 electricity meters are greater than 400
units 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.
[2]
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
Problem 11) Winter 2018 P23
3 Give an example of a pseudo code statement or statements to perform each of the following
functions.
A conditional statement [3]
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
Problem 12) Winter 2015 P21 & 22
5 Identify two different conditional statements that you can use when writing pseudo code.
1 ……………………………………………………………………………………………………………….
2 ……………………………………………………………………………………………………… [2]
Examiners Comments Question 5
Many candidates could identify IF as a conditional statement. Candidates with stronger responses throughout also identified
CASE.
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]
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.
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 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.
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.
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
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
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
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.
Total 0
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
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
Example: to input 10 numbers and output their final total
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
Problem: Input daily wages and number of day worked and output monthly pay for 100 employees.
Problem: Input marks of 30 students in a class output result of each student. Passing marks is 40
Problem: 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
Pre-Condition Loop: (WHILE … DO … ENDWHILE)
When condition to continue the loop is given
Problem: To input and add a series of positive numbers in total. Continue this process for input of
positive numbers
WHILE
Problem: Input a series of numbers, calculate their total, stop input if total is more than 100
REPEAT … UNTIL Loop
Differences between
Pre-Condition Post Condition
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]
There are three different types of looping structures. Write pseudo code for each of following three
problems using different looping structure:
a) Input daily temperature for a month of 30 days, calculate and output their total and average.
……………………………………………………….................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
..................................................................................................................................................... [6]
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]
Extracted from March 2018 P22 (India)
c) Input the weight in kilograms of a passenger 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 9.6) Write an algorithm, using pseudo code and a FOR … TO … NEXT loop structure, to input
1000 numbers into an array.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[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]
(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]
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]
(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]
Summer 2017 P21
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
(b)
............................................................................................................................................................
........................................................................................................................................................[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]
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]
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]
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]
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’
Now convert the real number into whole number using INT()
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
.......................................................................................
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]
........................................................................................................................................... [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
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
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
X X=Y?
0
3.8 3 No
4 4 Yes 1
5 5 Yes 2
9.1 9 No
7 7 Yes 3
12.9
DECLARE Count, CountINT : Integer
DECLARE X, Y: Real
CountINT 0
FOR Count 1 TO 1000
PRINT “ Enter a number “
INPUT X
Y INT(X)
IF X = Y THEN CountINT CountINT + 1
NEXT Count
PRINT “ Number of integers = “ , CountINT
Q12.10)
Q12.18) A programmer uses an Integrated Development Environment (IDE) for all program
development. Describe what is meant by an IDE.
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................ [2]
12.18
IDE is a (Single) software program
Features for:
program editor/writing/editing
translation // interpreter/compiler
testing program code // observe outputs 2 points to score
Q 12.23) An algorithm to reset the contents of the array Coins after each sale is shown below. The
re are 10 different coins. This algorithm contains a logic error.
i=1
REPEAT
Coins(i) = 0
i=i+1
UNTIL i = 10
1........................................................................................................................................................
2 .......................................................................................................................................................
3................................................................................................................................................... [3]
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]
Candidate Example response
Past paper questions on basic concepts of algorithm
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]
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]
(b) Complete the trace table for the input data: 2 [2]
Fib Prev2 Prev1 Number OUTPUT
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.
Identifier Data Type Description
Customer ARRAY[100] OF STRING Array of customer names
Index INTEGER Used to index the array elements
IsFound
SearchName STRING The requested customer name
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]
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]