9.2 As Algorithm
9.2 As Algorithm
9.2 As Algorithm
Inqilab Patel is an O & A Level Computer Teacher. Currently he is teaching A & O Level Computer Science
at The City School PAF Chapters, Hira Foundation School and Intellect. He has taught in many other
schools including Yaqeen Model School, Karachi Cadet School, KN Academy, Hexis A Level, Verge and
Nakhlah Boys Campus Society. Cambridge has selected him as a Member of Cambridge Editorial
Review Board. He is also associated with Aga Khan University Examination Board in the capacity of
Chief Examiner, Item Writer, E-Marker, Karachi Board of Secondary Education the capacity of
Deputy Head Examiner and Sindh Board of Technical Education.
His entire career path revolves around computer science; either he was a student or a teacher.
He got a chance to polish his skills of teaching and studying more about computers at various
levels which has given him great confidence in presenting himself for any senior level position of
transferring his knowledge to the youth.
He has not stopped; he is continuing with his education at the higher levels. It is his second
semester of MPhil computer studies from a well-known university of Pakistan; The Institute of
Business & Technology.
Inqilab Patel knows a lot of methods of teaching computers and has developed tutorial notes,
worksheets and assignments for my students. He also maintains a website
(www.inqilabpatel.com) which is specifically designed for the support of those who want to excel
in GCSE computer science. He also regularly contributes material to CIE teacher support website,
for which he receives appreciation from different people across the world.
He has also received various training in innovative and special methods of teaching this subject.
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”
Data types:
A data type is a classification that specifies which type of data a variable has and what type of
operations can be applied to it.
Most programming languages support various types of data, including integer, real, character or
string, and Boolean.
1. INTEGER:
A whole number (without fractional part) like COUNT which never requires fractional part
For example, 56, 89, 1
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:
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”
5. BOOLEAN: A data type with two possible values
For example TRUE and FALSE or YES or NO
6. DATE: To store a calendar date
For example 16/04/2010
Literals
Literals of the above data types are written as follows:
Data Type Literals
Integers: Written as normal in the denary system, e.g. 5, -3
Real: Always written with at least one digit on either side of the decimal point, zeros
being added if necessary, e.g. 4.7, 0.3, -4.0, 0.0
Char: A single character delimited by single quotes, e.g. x , C , @
String: Delimited by double quotes. A string may contain no characters (i.e. the empty
string) e.g. "This is a string", "Abdullah Patel"
Boolean: TRUE, FALSE
Date: This will normally be written in the format dd/mm/yyyy. However, it is good
practice to state explicitly that this value is of data type DATE and to explain the
format (as the convention for representing dates varies across the world).
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.
Assignments
The assignment operator is .
Assignments should be made in the following format:
<identifier> ← <value>
The identifier must refer to a variable (this can be an individual element in a data structure such as an
array or an abstract data type). The value may be any expression that evaluates to a value of
Common operations
Input and output
Values are input using the INPUT command as follows:
INPUT <identifier>
The identifier should be a variable (that may be an individual element of a data structure such as an
array, or a custom data type).
Values are output using the OUTPUT command as follows:
OUTPUT <value(s)>
Several values, separated by commas, can be output using the same command.
5.2 Arithmetic operations
Standard arithmetic operator symbols are used:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Raised to the power of
Care should be taken with the division operation: the resulting value should be of data type REAL,
even if the operands are integers.
B - Brackets = P - Parentheses
O - Order = E - Exponents i.e. Powers
D - Division != M - Multiplication
M - Multiplication != D - Division
A - Addition = A - Addition
S - Subtraction = S - Subtraction
So we see the only difference is between the multiplication and division operators. Both are acronyms
designed to be easy to remember but don’t quite tell the full story. The central idea the two acronyms
try to get across is that you should do multiplication and division before addition and subtraction.
Now there are some awkward cases like 6/2*3, 6*3/2 and 6/3/2. Neither rule works for all these case.
The real rule is that when the operations are of the same precedence, (like * and /) you do the
operations from left to right. Hence 6/2*3 = (6/2) * 3 = 3 * 3 = 9 and 6 * 3 / 2 = (6 * 3) / 2 = 18 / 2 =
9 and 6/3/2 = (6/3)/2 = 2/2 = 1.
The same happens with + and –. So 6 – 2 – 3 = (6 – 2) – 3.
Pupil1.Surname ← "Johnson"
Pupil1.Firstname ← "Leroy"
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← ꞌAꞌ
Pupil2 ← Pupil1
FOR Index ← 1 TO 30
Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT INDEX
S18 P23 1 (a) A program controls the heating system of an energy-efficient house.
Give a suitable identifier name for each of the data items. [4]
Description of data item Suitable identifier name
The temperature inside the house
The temperature outside the house
The wind speed
Whether it was raining or not
(b) (i) Program variables have values as follows:
Variable Value
Quality 'D'
DayNumber 20
MyName "Stephen"
QualityConfirmed TRUE
Factor 6.5
(ii) Give an appropriate data type for each of these variables from part (b)(i). [5]
Variable Data type
QualityConfirmed
DayNumber
Factor
Quality
MyName
(ii) Identify the type of programming statement that assigns a data type to a variable.
..................................................................................................................................... [1]
(ii) Evaluate each expression in the following table. If an expression is invalid then write ERROR. [5]
Expression Evaluates to
MID(Today, 3, 2) & Revision & "ape"
INT(MaxWeight + 4.2)
LENGTH(MaxWeight)
MOD(WeekNumber, 12)
(Revision <= 'D') AND (NOT LastBatch)
1 (a) A program is being developed to help manage the membership of a football club.
Complete the following identifier table. [4]
Example Variable
Explanation Data type
value name
"Wong" The preferred name of the member joining the football club
A value to indicate whether an existing member of the club
FALSE
lives at the same address
19/02/1983 When the member joined the football club
The number of points a member has earned. Members of
1345
the club earn points for different activities.
(b) Each pseudocode statement in the following table may contain an error due to the incorrect use of
the function or operator.
Describe the error in each case, or write ‘NO ERROR’ if the statement contains no error.
You can assume that none of the variables referenced are of an incorrect type. [5]
Statement Error
Result 2 & 4
SubString MID("pseudocode", 4, 1)
IF x = 3 OR 4 THEN
Result Status AND INT(x/2)
Message "Done" + LENGTH(MyString)
1 Sylvia is testing a program that has been written by her colleague. Her colleague tells her that the
program does not contain any syntax errors.
(a) (i) State what her colleague means by “does not contain any syntax errors”.
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [1]
(ii) Identify and describe one other type of error that the program may contain.
Type of error ......................................................................................................................
Description ........................................................................................................................
........................................................................................................................................... [2]
1 (c) Complete the pseudocode expressions so that they evaluate to the values shown. [4]
Any functions and operators used must be defined in the insert.
Expression Evaluates to
67
..................... ('C')
54
2 * ....................... ("27")
13
................... (27 / ..............)
"Subtract"
"Sub" & ........ ("Abstraction" , .......... ,.........)
(d) Evaluate the expressions given in the following table. The variables have been assigned values as
follows: [2]
PumpOn ← TRUE
PressureOK ← TRUE
HiFlow ← FALSE
Expression Evaluates to
PressureOK AND HiFlow
PumpOn OR PressureOK
NOT PumpOn OR (PressureOK AND NOT HiFlow)
NOT (PumpOn OR PressureOK) AND NOT HiFlow
(ii) One of the names used for a variable in the table in part 1(b)(i) is not an example of good
practice.
Identify the variable and give a reason why it is not good practice to use that name.
Variable .............................................................................................................................
Reason ..............................................................................................................................
...........................................................................................................................................
........................................................................................................................................... [2]
(c) Complete the table by evaluating each expression. [4]
Expression Evaluation
INT((31 / 3) + 1)
MID(TO_UPPER("Version"), 4, 2)
TRUE AND (NOT FALSE)
NUM_TO_STR(27 MOD 3)
(b) Line 3 in the algorithm above does not give the detail about how the race penalty weight is
calculated; this step in the algorithm must be expressed in more detail.
(i) The algorithm above currently has five stages. One technique for program design is to further
break down, where required, any stage to a level of detail from which the program code can be
written. Name this technique.
....................................................................................................................................... [1]
(ii) Write pseudocode for the given structured English design. [5]
(b) Bank customers withdraw money from their account at a cash dispenser machine using their
bank card. The machine operates as follows:
• it can dispense the following notes:
o $50
o $20
o $10
• the maximum amount for a single withdrawal is $500
When a customer withdraws money, they enter the amount to withdraw. (This must be a multiple of
$10).The machine will always dispense the least possible number of notes.
A program is designed for the machine to process a withdrawal.
The following variables are used:
Identifier Data type Description
Amount INTEGER Amount to withdraw entered by the user
FiftyDollar INTEGER Number of $50 notes to dispense
TwentyDollar INTEGER Number of $20 notes to dispense
TenDollar INTEGER Number of $10 notes to dispense
Temp INTEGER Used in the calculation of the number of each note required
(b) The selection statement (lines 03 – 08) could have been written with more simplified logic.
Rewrite this section of the algorithm in pseudocode.
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.................................................................................................................................................................
.......................................................................................................................................................[3]
Test Yourself Winter 2016 Paper 21
(b) Write the pseudocode equivalent of the structured English. Use the identifiers from the table
in part (a). [6]