21 Algorithm Pseudo Code
21 Algorithm Pseudo Code
Chapter 8
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.
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:
Variable:
Variable is memory location where a value can be stored. The values stored in a variable are
changed during execution.
Identifiers
Identifiers (the names given to variables, constants, procedures and functions) are in mix case. They
can only contain letters (A–Z, a–z) and digits (0–9). They must start with a letter and not a digit.
Accented letters and other characters, including the underscore, should not be used.
As in programming, it is good practice to use identifier names that describe the variable, procedure
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.
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)
2 Describe, giving an example for each, the following data types used in programming.
Integer Description ...........................................................................................................................
...........................................................................................................................................................
Example ............................................................................................................................................
String Description ..............................................................................................................................
............................................................................................................................................................
Example ....................................................................................................................................... [4]
Q10 (i) Programming languages support different data types.
Complete the table by giving a suitable data type for each example value. [4]
Example value Data type
43
TRUE
- 273.16
"- 273.16"
Summer2019 P21
.................................................................................................................................................... [2]
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.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [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
‘’ ‘ ’
‘ ’ ‘ ’
‘ ’
i. INPUT Marks
ii. IF Marks>= 50 THEN
iii. PRINT “Pass”
iv. ELSE
v. PRINT “Fail”
vi. ENDIF
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.
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 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
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
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.
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 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
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
……………………………………………………………………………………………………………….
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]
2 (c) The following lines of code are taken from a program in a high-level language.
ON x {
15: Call ProcA
20: y := 0
25: y := 99
NONE: Call ProcError
}
Identify the type of control structure and describe the function of the code.
Control structure .......................................................................................................................
Description ................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................[3]
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 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.
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
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
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
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: 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
(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]
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
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [6]
Q 9.8b) Explain how you change your pseudo code to reject any item over 25 Kg.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
...................................................................................................................................................... [6]
5 (a) Write an algorithm, using pseudo code and a FOR … TO … NEXT loop structure, to input
1000 numbers into an array.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................................[2]
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]
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]
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[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]
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]
Summer2019 P22
4 For each of the four groups of statements in the table, place a tick in the correct column to show
whether it is an example of Selection or Repetition. [4]
Statements Selection Repetition
FOR A 1 TO 100
B B+1
NEXT A
CASE A OF
100: B A
200: C A
ENDCASE
IF A > 100
THEN
B A
ENDIF
REPEAT
A B * 10
UNTIL A > 100
Summer2019 P21
3 (a) Give an example of a conditional statement using pseudocode.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [2]
(b) Describe the purpose of a conditional statement.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
....................................................................................................................................................... [2]
Chapter 12
Using pseudo code or otherwise, write an algorithm that will input weight (kg) and height (m) of
students, calculate their body mass index (BMI) and output their BMI.
Test data: 80, 2, 100, 1.9, 60, 2, 70, 1.8
First draw trace table write down column headings
Calculate BMI using trace table:
Weight Height BMI Setup in pseudo code using
declaration of variable
SECTION SHOWS YOU HOW
THIS WOULD WORK’
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.
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
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
............................................................................................................................................................
............................................................................................................................................................
........................................................................................................................................ [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]
Summer 2015 P21 23
Examiner Report Question 6
Most candidates could identify at least one loop structure. A common wrong answer was to incorrectly identify IF as part of a
loop structure.
Q .12 Describe the purpose of each statement in this algorithm.
Count 0
WHILE Count<10 DO
PRINT Count
Count Count + 1
ENDWHILE
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
.......................................................................................................................................................[2]
Q 9.14b) Explain how do you change your flowchart to work for 30 numbers that are between 0 an
d 100.
............................................................................................................................................................
............................................................................................................................................................
............................................................................................................................................................
......................................................................................................................................................[3]
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]
(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
Complete the trace table for the mark input data: 50, 70, 65, 30, 95, 50, 55, 85, 65, 35, –1, 45[4]
Total Count Distinction Mark OUTPUT
Linear Search
5 Customer names are stored in the array Customer.
An algorithm is to be designed to perform a serial search of the array for a requested customer
name.
The algorithm will use the variables shown in the table.
(a) Study the table and the algorithm and fill in the gaps.
IsFound ← FALSE
Index ← 1
REPEAT
IF …………………………… =SearchName
THEN
IsFound ← TRUE
ELSE
……………………………………………………..
ENDIF
IF ……………………………………………………….. THEN
ENDIF [7]
(b) How many comparisons on average will be needed to find a requested customer from
the Customer array?
......................................................................................................................................................[1]