Cs Question
Cs Question
May/June 2021
1. A procedure CountVowels() will:
• be called with a string containing alphanumeric characters as its parameter
• count and output the number of occurrences of each vowel (a, e, i, o, u) in the string
• count and output the number of occurrences of the other alphabetic characters (as a single
total).
The string may contain both upper and lower case characters.
Each count value will be stored in a unique element of a global 1D array CharCount of type
INTEGER. The array will contain six elements.
Write pseudocode for the procedure CountVowels()
FOR Index ← 1 to 6
CharCount[Index] ← 0 //initialise elements
NEXT Index
Index ← 1
CASE OF ThisChar
'a' : CharCount[1] ← CharCount[1] + 1
'e' : CharCount[2] ← CharCount[2] + 1
'i' : CharCount[3] ← CharCount[3] + 1
'o' : CharCount[4] ← CharCount[4] + 1
'u' : CharCount[5] ← CharCount[5] + 1
'a' TO 'z': CharCount[6] ← CharCount[6] + 1
ENDCASE
NEXT Index
FOR Index ← 1 to 6
OUTPUT CharCount[Index] //output results
NEXT Index
ENDPROCEDURE
9618/23
May/June 2021
2. A procedure Preview() will:
• take the name of a text file as a parameter
• output a warning message if the file is empty
• otherwise output the first five lines from the file (or as many lines as there are in the file if
this number is less than five).
Write pseudocode for the procedure Preview()
9618/21
May/June 2022
IF MySwitch = 1 THEN
ThisChar 'a'
ELSE
IF MySwitch = 2 THEN
ThisChar 'y'
ELSE
IF MySwitch = 3 THEN
ThisChar '7'
ELSE
ThisChar '*'
ENDIF
ENDIF
ENDIF
9618/22
May/June 2022
4. A string represents a series of whole numbers, separated by commas.
For example:
"12,13,451,22"
Assume that:
• the comma character ',' is used as a separator
• the string contains only the characters '0' to '9' and the comma character ','.
A procedure Parse will:
• take the string as a parameter
• extract each number in turn
• calculate the total value and average value of all the numbers
• output the total and average values with a suitable message.
Write pseudocode for the procedure.
Count 0
Total 0
NumString ""
ENDPROCEDURE
9618/23
May/June 2022
5. The factorial of a number is the product of all the integers from 1 to that number.
For example:
factorial of 5 is given by 1 × 2 × 3 × 4 × 5 = 120
factorial of 7 is given by 1 × 2 × 3 × 4 × 5 × 6 × 7 = 5040
factorial of 1 = 1
Note: factorial of 0 = 1
A function Factorial() will:
• be called with an integer number as a parameter
• calculate and return the factorial of the number
• return −1 if the number is negative.
Write pseudocode for the function Factorial().
9618/21
May/June 2023
6. Function Replace() will:
1. take three parameters:
• a string (the original string)
• a char (the original character)
• a char (the new character)
2. form a new string from the original string where all instances of the original character are
replaced by the new character
3. return the new string.
Write pseudocode for function Replace().
Answer
Function Replace(OldString : STRING, Char1, Char2 : CHAR) __ RETURNS : STRING
DECLARE NewString : STRING
DECLARE ThisChar : CHAR
DECLARE Index : INTEGER
NewString ""
FOR Index 1 TO LENGTH(OldString)
ThisChar MID(OldString, Index, 1)
IF ThisChar = Char1 THEN
ThisChar Char2
ENDIF
NewString NewString & ThisChar
NEXT Index
RETURN NewString
ENDFUNCTION
7. A program calculates the postal cost based on the weight of the item and its destination.
Calculations occur at various points in the program and these result in the choice of several
possible postal costs. The programmer has built these postal costs into the program.
For example, the postal cost of $3.75 is used in the following lines of pseudocode:
IF Weight < 250 AND ValidAddress = TRUE THEN
ItemPostalCost 3.75 // set postal cost for item to $3.75
ItemStatus "Valid" // item can be sent
ENDIF
(a) (i) Identify a more appropriate way of representing the postal costs
Use of constants
(ii) Describe the advantages of your answer to part (a)(i) with reference to this program
1 Postal rates are entered once only
2 Avoids input error / changing the cost accidentally // avoids different
values for postal rates at different points in the program
3 When required, the constant representing the postal rate value is
changed once only // easier to maintain the program when the postal
rates change
4 Makes the program easier to understand
CASE OF Dim
1 : OUTPUT "1"
2 : OUTPUT "22"
OUTPUT "22"
3 : OUTPUT "333"
OUTPUT "3*3"
OUTPUT "333"
4 : OUTPUT "4444"
FOR Count 1 TO 2
OUTPUT "4**4"
NEXT Count
OUTPUT "4444"
5 : OUTPUT "55555"
FOR Count 1 TO 3
OUTPUT "5***5"
NEXT Count
OUTPUT "55555"
6 : OUTPUT "666666"
FOR Count 1 TO 4
OUTPUT "6****6"
NEXT Count
OUTPUT "666666"
7 : OUTPUT "7777777"
FOR Count 1 TO 5
OUTPUT "7*****7"
NEXT Count
OUTPUT "7777777"
8 : OUTPUT "88888888"
FOR Count 1 TO 6
OUTPUT "8******8"
NEXT Count
OUTPUT "88888888"
9 : OUTPUT "999999999"
FOR Count 1 TO 7
OUTPUT "9*******9"
NEXT Count
OUTPUT "999999999"
ENDCASE
ENDPROCEDURE
Write pseudocode for the module Generate()
FUNCTION Generate() RETURNS STRING
DECLARE Password, Group : STRING
DECLARE NextChar : CHAR
DECLARE ACount, BCount : INTEGER
CONSTANT HYPHEN = '-'
Password ""
FOR ACount 1 TO 3
Group ""
FOR BCount 1 TO 4
REPEAT
NextChar RandomChar()
UNTIL Exists(Group, NextChar) = FALSE
Group Group & NextChar
NEXT BCount
Password Password & Group & HYPHEN
NEXT ACount
Password LEFT(Password, 14) // remove final hyphen
RETURN Password
ENDFUNCTION