0% found this document useful (0 votes)
40 views7 pages

Test 2 - Problem Solving and Design - Memo

The document contains pseudocode for several computer science procedures and functions, including displaying odd numbers between two values, counting integers in a list of numbers, counting vowels in a string, calculating factorials, checking if a number is prime, generating Fibonacci sequences, and simulating dice rolls until a double six occurs.

Uploaded by

dkandpal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views7 pages

Test 2 - Problem Solving and Design - Memo

The document contains pseudocode for several computer science procedures and functions, including displaying odd numbers between two values, counting integers in a list of numbers, counting vowels in a string, calculating factorials, checking if a number is prime, generating Fibonacci sequences, and simulating dice rolls until a double six occurs.

Uploaded by

dkandpal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TERM 1 High School 2022

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.

For Examiner Use Only:


Examination Question Marks Awarded

Total:
Percentage:
1. A procedure displayOdd() uses post-condition loop to output every odd number between
100 and 200.

Write pseudocode for the procedure displayOdd().

procedure displayOdd()

DECLARE Num:integer

Num  100

repeat

if (Num % 2) = 0 then

output Num

Num  Num + 1;

until Num > 200

ENDProcedure
(5)

2. A procedure, intCount():
• inputs 1000 numbers
• returns how many of these numbers were whole numbers (integers)

Write pseudocode for the procedure intCount():

procedure intCount(BYREF countInt:integer)

DECLARE Number:real

DECLARE counter:integer

countInt  0

counter 0

REPEAT

INPUT Number

IF INT(Number) = Number THEN

countInt  countInt + 1

counter  counter + 1

UNTIL counter >= 1000

ENDPROCEDURE

(5)

2
3. A procedure, Frequency(), receives a string and return the number of times each vowel
occurs in a string.

The procedure will:

• prompt and input a string


• count the occurrence of each vowel in the string using a CASE structure
• return each vowel with its count value.

You may assume that vowels are the upper-case or lower-case characters 'a', 'e',
'i', 'o', and 'u'.

Write pseudocode for the procedure Frequency().

procedure frequency(BYVAL sWord:string; BYREF cntA, cntE, cntI, cntO,


cntU:integer)
DECLARE index:integer

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.

Thus, factorial four is written 4! = 1 × 2 × 3 × 4 = 24.

Note: Factorial zero is defined as equal to 1 (0! = 1).

A function calcFactorial() receives a number N and returns a factorial of a number N.

Write pseudocode statements for the function calcFactorial().

function calcFactorial(BYVAL Number:integer):integer

DECLARE factorial:integer

if Number < 0 then


factorial  0

else if Number = 0 then


factorial  1

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

function isPrime(Number: integer): Boolean

DECLARE factor, counter:integer

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.

Write pseudocode statements for the procedure fibSequence().

procedure fibSequence(Number:integer;var sOut:string; var iSum:integer);

DECLARE currentTerm, previousTerm, nextTerm, count:integer;

previousTerm  0
currentTerm 1

sOut  str(previousTerm)+', '+ str(currentTerm) + ', '

iSum  previousTerm + currentTerm

count := 2

repeat
nextTerm  previousTerm + currentTerm
sOut  sOut + str(nextTerm) +', '
iSum  iSum + nextTerm

count  count + 1

previousTerm  currentTerm
currentTerm  nextTerm

until count = Number


ENDPROCEDURE

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

Write pseudocode statements for the function isDouble().

function isDouble():integer

DECLARE dice1,dice2, counter:integer

counter  0

repeat
dice1  random(6)+1
dice2  random(6)+1

counter  counter + 1

until ((dice1 = 6) and (dice2=6))

return counter
ENDFUNCTION
(8)

You might also like