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

Cs Question

The document contains pseudocode for various procedures and functions, including CountVowels(), Preview(), and Factorial(), which perform tasks such as counting vowels in a string, previewing file contents, and calculating factorials. It also includes algorithms for parsing numbers from a string, replacing characters in a string, and generating a password. Additionally, it discusses the use of constants for postal costs and provides pseudocode for a Square() procedure and a Generate() function.

Uploaded by

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

Cs Question

The document contains pseudocode for various procedures and functions, including CountVowels(), Preview(), and Factorial(), which perform tasks such as counting vowels in a string, previewing file contents, and calculating factorials. It also includes algorithms for parsing numbers from a string, replacing characters in a string, and generating a password. Additionally, it discusses the use of constants for postal costs and provides pseudocode for a Square() procedure and a Generate() function.

Uploaded by

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

9618/22

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

PROCEDURE CountVowels(ThisString : STRING)


DECLARE Index : INTEGER
DECLARE ThisChar : CHAR

FOR Index ← 1 to 6
CharCount[Index] ← 0 //initialise elements
NEXT Index
Index ← 1

FOR Index ← 1 TO LENGTH(ThisString)


ThisChar ← LCASE(MID(ThisString, 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()

PROCEDURE Preview (ThisFile : STRING)


DECLARE LineNum : INTEGER
DECLARE ThisLine : STRING

OPENFILE ThisFile FOR READ


IF EOF(ThisFile) THEN
OUTPUT “Warning Message”
ELSE
LineNum ← 1
WHILE LineNum < 6 AND NOT EOF(ThisFile)
READFILE Thisfile, ThisLine
OUTPUT ThisLine
LineNum ← LineNum + 1
ENDWHILE
ENDIF
CLOSEFILE ThisFile
ENDPROCEDURE

9618/21
May/June 2022

3.(a) An algorithm will:


• output each integer value between 100 and 200 that ends with the digit 7, for example,
107
• output a final count of the number of values that are output.
Write pseudocode for this algorithm.
Any variables used must be declared.

DECLARE ThisInt, Count : INTEGER


Count  0
FOR ThisInt  100 TO 200
IF ThisInt MOD 10 = 7 THEN
OUTPUT ThisInt
Count  Count + 1
ENDIF
NEXT ThisInt
OUTPUT Count
(b) Study the following pseudocode.
CASE OF MySwitch
1: ThisChar 'a'
2: ThisChar 'y'
3: ThisChar '7'
OTHERWISE: ThisChar '*'
ENDCASE
Write pseudocode with the same functionality without using a CASE structure.

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.

PROCEDURE Parse(InString : STRING)

PROCEDURE Parse(InString : STRING)


DECLARE Count, Total, Index : INTEGER
DECLARE Average : REAL
DECLARE NumString : STRING
DECLARE ThisChar : CHAR
CONSTANT COMMA = ','

Count  0
Total  0
NumString  ""

FOR Index  1 to LENGTH(InString)


ThisChar  MID(InString, Index, 1)
IF ThisChar = COMMA THEN
Total  Total + STR_TO_NUM(NumString)
Count  Count + 1
NumString  ""
ELSE
NumString  NumString & ThisChar // build the number string
ENDIF
NEXT Index

// now process the final number


Total  Total + STR_TO_NUM(NumString)
Count  Count + 1

Average  Total / Count


OUTPUT "The total was ", Total, " and the average was ",
Average

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

FUNCTION Factorial(ThisNum : INTEGER) RETURNS INTEGER


DECLARE Value : INTEGER

IF ThisNum < 0 THEN


Value  -1
ELSE
Value  1
WHILE ThisNum <> 0
Value  Value * ThisNum
ThisNum  ThisNum – 1
ENDWHILE
ENDIF
RETURN Value
ENDFUNCTION

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

Write pseudocode for procedure Square().


Parameter validation is not required.
PROCEDURE Square(Dim : INTEGER)
DECLARE Count : INTEGER

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

You might also like