9.2 As Algorithm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

About the developer of this workbook

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.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


9.2 Algorithm

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”

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


In general, an 'algorithm' is the name given to a defined set of steps used to complete a task.
For instance, you could define an algorithm to make a cup of tea. You start by filling the kettle, and
then place a tea bag in the cup and so on.
In computer terms, an algorithm describes the set of steps needed to carry out a software task. This
mini-web takes you through the topic of algorithm

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
10.1 Data Types & Records
Candidates should be able to: Notes and guidance
Select and use appropriate data types for a including integer, real, char, string, Boolean,
problem solution date (pseudocode will use the following data
types:
INTEGER, REAL, CHAR, STRING, BOOLEAN,
DATE, ARRAY, FILE)

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Variable:
Variable is memory container, where a value can be stored. The values stored in a variable are
changed during execution.
Like in computer game you have observed Lives and Score.
At the start if game Score has a value 0, while Lives has
some values like 3. During game values stored in Score are
increased with each success and values stored in Lives
decreases with failure. Here Lives and Score are variables, storing values, which are changed
during program execution.
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mix case. They
can only contain letters (A Z, a z) and digits (0 9). They must start with a letter and not a digit.
Accented letters and other characters, including the underscore, should not be used.
As in programming, it is good practice to use identifier names that describe the variable, procedure or
function they refer to. Single letters may be used where these are conventional (such as i and j when
dealing with array indices, or X and Y when dealing with coordinates) as these are made clear by the
convention.
Keywords identified elsewhere in this guide should never be used as variables. Keywords are written
in ALL CAPS.
Identifiers should be considered case insensitive, for example, Countdown and Countdown should
not be used as separate variables.

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:

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
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”
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).

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


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.

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
the same data type as the variable.
Example – assignments
Counter ← 0
Counter ← Counter + 1
TotalToPay ← NumberOfHours * HourlyRate

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.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Operator Precedence:
In mathematics and computer programming, the order of operations (or operator precedence) is a
collection of rules that reflect conventions about which procedures to perform first in order to evaluate
a given mathematical expression.

Should we follow BODMAS or PEMDAS?


They are basically the same

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.

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Relational operations
The following symbols are used for relational operators (also known as comparison operators):
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
The result of these operations is always of data type BOOLEAN.
In complex expressions it is advisable to use parentheses to make the order of operations explicit.
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.
String operations
Syllabus requirements The AS & A Level (9618) syllabus specifically requires candidates to know
string manipulation functions in their chosen programming language. Pseudocode string manipulation
functions will always be provided in examinations.
User-defined data types
Syllabus requirements The AS & A Level (9618) syllabus requires candidates to understand that
data structures that are not available in a particular programming language need to be constructed
from the data structures that are built-in within the language. User-defined data types need to be
defined, the syllabus requires candidates to use and define non-composite data types such as
enumerated and pointer; composite data types record, set, class/object. Abstract Data Types (ADTs)
stack, queue, linked list, dictionary and binary tree are also defined as composite data types.
Defining user-defined data types
A composite data type is a collection of data that can consist of different data types, grouped under
one identifier. The composite type should be declared as follows:
TYPE <identifier1>
DECLARE <identifier2> : <data type>
DECLARE <identifier3> : <data type>
...
ENDTYPE
Example – declaration of composite type

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


This user-defined data type holds data about a student.
TYPE Student
DECLARE Surname : STRING
DECLARE FirstName : STRING
DECLARE DateOfBirth : DATE
DECLARE YearGroup : INTEGER
DECLARE FormGroup : CHAR
ENDTYPE
Using user-defined data types
When a user-defined data type has been defined it can be used in the same way as any other data
type in declarations.
Variables of a user-defined data type can be assigned to each other. Individual data items are
accessed using dot notation.
Example – using user-defined data types
This pseudocode uses the user-defined type Student, Season and TAddPointer defined in the
previous section.

DECLARE Pupil1 : Student


DECLARE Pupil2 : Student
DECLARE Form : ARRAY[1:30] OF Student

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/
COMPUTER SCIENCE WITH INQILAB PATEL
S17 P22 - 1 (b) (i) Complete the following two sentences.
A suitable operand type for an arithmetic operator is ........................................................
A suitable operand type for a logical operator is ...............................................................[2]
(ii) The following table shows the values of three variables.
Variable Value
FlagA TRUE
FlagB FALSE
FlagC TRUE
Evaluate these expressions. [3]
Expression Evaluates to
(FlagA AND FlagB) OR FlagC
FlagA AND (FlagB OR FlagC)
(NOT FlagA) OR (NOT FlagC)

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


W17 P22 - 1 (a) (i) Procedural high-level languages usually support different data types.
Give an appropriate data type for each data value in the following table: [6]
Data value Data type
FALSE
03/03/2013
35
"INTEGER"
3.5
"35"

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
W18 P21 - contains statements written in pseudocode.
Give the most appropriate data type for the variable used in each statement. [5]
Statement Data type
MyAverage 13.5
ProjectCompleted TRUE
Subject "Home Economics"
MyMark 270
MyGrade 'B'
W18 P22 1 (b) (i) The following table contains statements written in pseudocode. Give the most
appropriate data type for the variable used in each statement. [5]
Statement Data type
Revision 'B'
MaxValue 13.3
ArrayFull TRUE
Activity "Design"
NumberOfEdits 270
S19 P21 - 1 (b) Program variables have values as follows:
Variable Value
Title "101 tricks with spaghetti"
Version 'C'
Author "Eric Peapod"
PackSize 4
WeightEach 6.2
Paperback TRUE
(ii) Give an appropriate data type for the following variables from part (b). [5]
Variable Data type
Title
Version
PackSize
WeightEach
Paperback

S19 P22 - (b) Program variables have values as follows:

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Variable Value
Married 03/04/1982
ID "M1234"
MiddleInitial 'J'
Height 5.6
IsMarried TRUE
Children 2
(ii) Give an appropriate data type for the following variables from part (b). [5]
Variable Data type
Married
ID
MiddleInitial
Height
IsMarried
W19 p21 1 (b) (i) Complete the table by giving a suitable data type for each example value. [4]
Example value Data type
"NOT TRUE"
- 4.5
NOT FALSE
132
W19 P22 1 (b) (i) Complete the table by giving a suitable data type for each example value. [4]
Example value Data type
43
TRUE
- 273.16
"- 273.16"

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
W19 P23 - 1 (a) (i) Programming languages can support different data types.
Complete the table by naming three different data types together with an example data value for
each. [6]
Data type Example data value

(ii) Identify the type of programming statement that assigns a data type to a variable.
..................................................................................................................................... [1]

9618 Specimen Paper 1 (a) Program variables have values as follows:


Variable Value
Today "Tuesday"
WeekNumber 37
Revision 'C'
MaxWeight 60.5
LastBatch TRUE
(i) Give an appropriate data type for each variable. [5]
Variable Data type
Today
WeekNumber
Revision
MaxWeight
LastBatch

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(b) Simple algorithms usually consist of input, process and output.
Complete the table to show if each statement is an example of input, process or output.
Place one or more ticks for each statement. [4]
Item Statement Input Process Output
1 SomeChars ← "Hello World"
2 OUTPUT RIGHT(SomeChars, 5)
3 READFILE MyFile, MyChars
4 WRITEFILE MyFile, "Data is " & MyChars

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)

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(c) The following data items need to be stored for each student in a group:
• student name (a string)
• test score (an integer).
State a suitable data structure and justify your answer.
Structure ...................................................................................................................................
Justification ...............................................................................................................................
...................................................................................................................................................
................................................................................................................................................... [3]

1b 9618 S21 P22 -


1 (a) (i) Complete the following table by giving the appropriate data type in each case. [4]
Variable Example data value Data type
Name "Catherine"
Index 100
Modified FALSE
Holiday 25/12/2020
(ii) Evaluate each expression in the following table by using the initial data values shown in part
(a)(i). [4]
Expression Evaluates to
Modified OR Index > 100
LENGTH("Student: " & Name)
INT(Index + 2.9)
MID(Name, 1, 3)
(b) Each pseudocode statement in the following table contains an example of selection, assignment
or iteration. Put one tick (‘ ’) in the appropriate column for each statement. [3]
Selectio Assignmen
Statement Iteration
n t
Index Index + 1
IF Modified = TRUE THEN
ENDWHILE

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]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


(b) Complete the following table by giving the appropriate data type in each case. [4]
Use of variable Data type
The average mark in a class of 40 students
An email address
The number of students in the class
To indicate whether an email has been read

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
3a 9618 S22 P21
1 (a) A programmer draws a program flowchart to show the sequence of steps required to solve a
problem.
Give the technical term for a sequence of steps that describe how to solve a problem.
...................................................................................................................................................
............................................................................................................................................. [1]
(b) The table lists some of the variables used in a program.
(i) Complete the table by writing the most appropriate data type for each variable. [4]
Variable Use of variable Data type
Temp Stores the average temperature
PetName Stores the name of my pet
MyDOB To calculate the number of days until my next birthday
LightOn Stores state of light; light is only on or off

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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


3b 9618 s22p22
1 (d) The pseudocode statements in the following table may contain errors.
State 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. [4]
Statement Error
Status TRUE AND FALSE
IF LENGTH("Password") < "10" THEN
Code LCASE("Electrical")
Result IS_NUM(-27.3)

3c 9618 S22 P23


1(b) (i) Program variables have values as follows:
Variable Value
AAA TRUE
BBB FALSE
Count 99
Complete the table by evaluating each expression. [2]
Expression Evaluation
AAA AND (Count > 99)
AAA AND (NOT BBB)
(Count <= 99) AND (AAA OR BBB)
(BBB AND Count > 50) OR NOT AAA

(ii) Give an example of when a variable of type Boolean would be used.


...........................................................................................................................................
..................................................................................................................................... [1]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
0 9618 Specimen Paper 2

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


1ac 9618 S21 P21 23

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
1b 9618 S21 P22

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


2ac 9618 W21P21-22

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


3a 9618 S22 P21

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
3b 9618 s22p22

3c 9618 S22 P23

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Test Yourself Summer 2015 P21, 22
Throughout the paper you will be asked to write either pseudo code or program code.
Complete the statement to indicate which high-level programming language you will use.
1 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
(a) The identifier table needs to show the variables required to write a program for this algorithm.
Complete the table. [3]
Identifier Data type Description
RaceHours INTEGER The hours part of the race time.

(b) Before the program is written, the design is amended.


The new design includes input of the runner’s current personal best marathon time (in seconds).
The output will now also show one of the following messages:
• “Personal best time is unchanged”
• “New personal best time”
• “Equals personal best time”
(i) Show the additional variable needed for the new design. [1]
Identifier Data type Description

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Test Yourself Summer 2015 Paper 23
1 Horses are entered for a horse race. A horse may have to carry a penalty weight in addition to the
rider. This weight is added to the saddle. The penalty weight (if any) depends on the number of wins
the horse has achieved in previous races. The penalty weight is calculated as follows:
Number of previous wins Penalty weight (kg)
0 0
1 or 2 4
Over 2 8
A program is to be written from the following structured English design.
1 INPUT name of horse
2 INPUT number of previous wins
3 CALCULATE penalty weight
4 STORE penalty weight
5 OUTPUT name of horse, penalty weight
(a) Complete the identifier table showing the variables needed to code the program. [3]
Identifier Data type Description

(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]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


2 (a) Two operators available in a programming language are DIV and MOD. They perform integer
arithmetic as follows:
Expression Explanation
X DIV Y Computes the number of times Y divides into X
X MOD Y Computes the remainder when X is divided by Y
Calculate the value of the variables shown for the following code fragments. [3]
Code Variable
NumberLeftOver 37 MOD 10 NumberLeftOver ..................
Quantity 208
BoxSize 100 NumberOfBoxes ....................
NumberOfBoxes Quantity DIV BoxSize Temp .......................................
Temp (Quantity MOD BoxSize) + 1

(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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
(i) The following four tests have been designed.
Complete the test data table showing the expected results with comments. [3]
Input value Output
FiftyDolla TwentyDolla TenDolla Comment
Amount
r r r
70 1 1 0 Least possible number of notes
85
130
600

(ii) Complete the pseudocode.


INPUT ...................................................
IF Amount > 500
THEN
OUTPUT "Refused – amount too large"
ELSE
................................................
THEN
OUTPUT "Refused - not a multiple of $10"
ELSE
FiftyDollar Amount DIV 50
Temp .....................................
TwentyDollar ...............................
Temp ......................................
.............................................
ENDIF
ENDIF [5]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Test Yourself Winter 2015 P22
1 Computer programs have to evaluate expressions.
Study the sequence of pseudocode 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 ← 3 * (N1 – 2) + N2 / 2
IsValid← (N1 > N2) AND (N2 = 9)
Found ← FALSE
IsValid← (N1 > N2 / 2) OR (Found =
FALSE)
Answer ← "1034" & " + " & "65"
(i) Answer .............................................................................................................................. [1]
(ii) Answer .............................................................................................................................. [1]
(iii) IsValid........................................................................................................................... [1]
(iv) IsValid........................................................................................................................... [1]
(v) Answer .............................................................................................................................. [1]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
2 A program is to simulate the operation of a particular type of logic gate.
• The gate has two inputs (0 or 1) which are entered by the user.
• The program will display the output (0 or 1) from the gate.
The program uses the following identifiers in the pseudocode below:
Identifier Data type Description
P INTEGER Input signal
Q INTEGER Input signal
X INTEGER Output signal
01 INPUT P
02 INPUT Q
03 IF (P = 1 AND Q = 0) OR (P = 0 AND Q = 1) OR (P = 0 AND Q = 0)
04 THEN
05 X ← 0
06 ELSE
07 X ← 1
08 ENDIF
09 OUTPUT X
(a) The programmer chooses the following four test cases.
Show the output (X) for each test case. [4]
Input Output
Test case
P Q X
1 1 1
2 1 0
3 0 1
4 0 0

(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

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


5 Study the following pseudocode statements.
CONST Pi = 3.1 : REAL
DECLARE Triangle, Base, Height, Radius, Cone : REAL
DECLARE a, b, c, Answer2 : INTEGER
DECLARE Answer1 : BOOLEAN
Base 2.6
Height 10
Triangle (Base * Height) / 2
Radius 1
Height 2
Cone 2 * Pi * Radius * (Radius + Height)
a 13
b 7
c 3
Answer1 NOT((a + b + c) > 28)
Total 34
Total Total - 2
Answer2 a + c * c
Give the final value assigned to each variable.
(i) Triangle ................................... [1]
(ii) Cone ................................... [1]
(iii) Answer1 ................................... [1]
(iv) Total ................................... [1]
(v) Answer2 ................................... [1]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


COMPUTER SCIENCE WITH INQILAB PATEL
Test Yourself Summer 2016 P21-22
2 A team is designing a software system to monitor temperature in a process. To do this, the system
needs to sample the temperature repeatedly. If the temperature exceeds a given threshold value, an
alarm will sound.
The system is to be software-based. It will include a subroutine, SampleTemp, which samples the
temperature and sets the alarm state to either ON or OFF.
The initial design stage will produce a prototype of SampleTemp with a user interface.
The structured English for this is:
1. IF the temperature does not exceed threshold value, SET alarm state to OFF
2. INPUT threshold value (to two decimal places)
3. INPUT sensor value (a whole number in the range 0 to 100)
4. MULTIPLY sensor value by conversion factor 1.135 to give temperature
5. IF temperature exceeds threshold value SET alarm state to ON
6. IF temperature exceeds threshold value OUTPUT message “Temperature Alarm”
7. IF temperature does not exceed threshold value OUTPUT message “Temperature OK”
(a) The procedure needs four variables. Complete the identifier table below for these variables. [4]
Identifier Data type Description
AlarmState
SensorValue
ThresholdValue
Temperature

(b) Write the pseudocode equivalent of the structured English. Use the identifiers from the table
in part (a). [6]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/


Test Yourself Winter 2016 P22
6 Study the sequence of pseudocode statements.
CONST a = 3.2 : REAL
DECLARE x, y, z, Answer1, Answer2, Answer3 : REAL
DECLARE p, q : BOOLEAN
x 3
x x + 7
y 6
Answer1 2 * (a + y)
z 6
Answer2 y ^ 2 + 5
p TRUE
q NOT(NOT(p))
Answer3 y + a * 2
Give the final value assigned to each variable.
(i) x .............................................
(ii) Answer1 .............................................
(iii) Answer2 .............................................
(iv) q .............................................
(v) Answer3 ............................................. [5]

+92 300 2724734 /inqilabpatel https://www.inqilabpatel.com/

You might also like