Test 2 - Problem Solving and Design - Memo
Test 2 - Problem Solving and Design - Memo
Grade AS
Subject Computer Science
Date 15 February 2024
Duration 60 Minutes
Total Marks 50
Teacher Mr. Matiza
Full Name:
Instructions to candidates
❖ Answer all questions.
❖ Use only black or blue ink.
❖ Use both sides of the paper.
❖ Do not tear out any parts of this booklet.
❖ All work MUST be handed in.
❖ If you have used any additional sheets or booklets, please ensure that you have attached
those documents, to this booklet.
Total:
Percentage:
1. A procedure displayOdd() uses post-condition loop to output every odd number between
100 and 200.
procedure displayOdd()
DECLARE Num:integer
Num 100
repeat
if (Num % 2) = 0 then
output Num
Num Num + 1;
ENDProcedure
(5)
2. A procedure, intCount():
• inputs 1000 numbers
• returns how many of these numbers were whole numbers (integers)
DECLARE Number:real
DECLARE counter:integer
countInt 0
counter 0
REPEAT
INPUT Number
countInt countInt + 1
counter counter + 1
ENDPROCEDURE
(5)
2
3. A procedure, Frequency(), receives a string and return the number of times each vowel
occurs in a string.
You may assume that vowels are the upper-case or lower-case characters 'a', 'e',
'i', 'o', and 'u'.
cntA 0
cntE 0
cntI 0
cntO 0
cntU 0
index 0
REPEAT
index index + 1
CASE upcase(sWord[index]) OF
'A': cntA cntA + 1
'E': cntE cntE + 1
'I': cntI cntI + 1
'O': cntO cntO + 1
'U': cntU cntU + 1
ENDCASE
UNTIL index > length(sWord)
ENDPROCEDURE
______________________________________________________________________________ (8)
3
4. Factorial, in mathematics, the product of all positive integers less than or equal to a given
positive integer and denoted by that integer and an exclamation point.
DECLARE factorial:integer
else
factorial 1
repeat
factorial factorial * Number
Number Number - 1
until Number = 0
ENDELSE
return factorial
ENDFUNCTION (6)
4
5. A prime number is a natural number greater than 1 and can only be divided by itself and 1 with a
remainder of zero.
A function isPrime() receives a number N and determine whether a number N is prime. It returns a
boolean value.
Write pseudocode statements for the function isPrime().
return false
factor 0
counter 1
repeat
if Number mod counter = 0 then
factor factor + 1
counter counter + 1
until counter > Number
if factor = 2 then
return true
ENDFUNCTION
(8)
5
6. The Fibonacci sequence is a type series where each number is the sum of the two that precede it. It
starts from 0 and 1 usually. The Fibonacci sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, 144 and so on.
A procedure fibSequence() receives a positive integer N and return a string with the first N
elements of the Fibonacci sequence as well as the total sum of N elements of the Fibonacci sequence
as an integer.
previousTerm 0
currentTerm 1
count := 2
repeat
nextTerm previousTerm + currentTerm
sOut sOut + str(nextTerm) +', '
iSum iSum + nextTerm
count count + 1
previousTerm currentTerm
currentTerm nextTerm
(10)
6
7. Uncle Koos needs to work out the odds of throwing a ‘double six’ with the roll of two dice.
A function isDouble() simulate the roll of the dice by generating two random numbers in the
range from 1 to 6. It returns the number of times the dice were thrown before a double six was
thrown.
function isDouble():integer
counter 0
repeat
dice1 random(6)+1
dice2 random(6)+1
counter counter + 1
return counter
ENDFUNCTION
(8)