0% found this document useful (0 votes)
24 views15 pages

Session 2

The document outlines several pseudocode procedures and functions for various programming tasks, including counting odd and even numbers, selecting integers based on specific criteria, generating strings, and processing sound samples. Each task specifies input parameters, expected outputs, and conditions for execution. The document serves as a guide for writing pseudocode for algorithms related to data processing and manipulation.

Uploaded by

ruhaanh2
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)
24 views15 pages

Session 2

The document outlines several pseudocode procedures and functions for various programming tasks, including counting odd and even numbers, selecting integers based on specific criteria, generating strings, and processing sound samples. Each task specifies input parameters, expected outputs, and conditions for execution. The document serves as a guide for writing pseudocode for algorithms related to data processing and manipulation.

Uploaded by

ruhaanh2
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

PAGE 1336

4 A procedure Count() will:

1. input a value (all values will be positive integers)


2. count the number of odd values and count the number of even values
3. repeat from step 1 until the value input is 99
4. output the two count values, with a suitable message.

The value 99 must not be counted.

(a) Write pseudocode for the procedure Count().

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [6]

© UCLES 2023 9618/22/O/N/23


PAGE 1288

10

6 A procedure Select() will:

1. take two integer values as parameters representing start and end values where both values
are greater than 9 and the end value is greater than the start value
2. output each integer value between the start and the end value (not including the start and
end values), where the sum of the last two digits is 6, for example, 142.

(a) Write pseudocode for procedure Select().

Parameter validation is not required.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [7]
© UCLES 2023 9618/23/M/J/23
PAGE 1286

4 A function MakeString() will:

1. take two parameters:


• a count as an integer
• a character
2. generate a string of length equal to the count, made up of the character
3. return the string generated, or return "ERROR" if the count is less than 1.

For example, the function call:

MakeString(3, 'Z') will return the string "ZZZ"

Write pseudocode for function MakeString().

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

.................................................................................................................................................... [6]

© UCLES 2023 9618/23/M/J/23


PAGE 1252

4 A function GetNum() will:


1. take two parameters: a string and a character
2. count the number of times that the character occurs in the string
3. return the count.

Any comparison between characters needs to be case sensitive. For example, character 'a' and
character 'A' are not identical.

Write pseudocode for function GetNum().

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

.................................................................................................................................................... [6]

© UCLES 2023 9618/22/M/J/23


PAGE 1230

10

6 A video-conferencing program supports up to six users. Speech from each user is sampled and
digitised (converted from analogue to digital). Digitised values are stored in array Sample.

The array Sample consists of 6 rows by 128 columns and is of type integer. Each row contains
128 digitised sound samples from one user.

The digitised sound samples from each user are to be processed to produce a single value which
will be stored in a 1D array Result of type integer. This process will be implemented by procedure
Mix().

A procedure Mix() will:


• calculate the average of each of the 6 sound samples in a column
• ignore sound sample values of 10 or less
• store the average value in the corresponding position in Result
• repeat for each column in array Sample

The diagram uses example values to illustrate the process:

1 2 3 ... 126 127 128


1 20 20 20 30 30 2
2 20 20 30 50 30 3
3 20 20 40 40 40 4
Sample:
4 20 20 50 40 50 20
5 20 3 5 6 60 4
6 20 4 2 4 70 30

Result: 20 20 35 40 46 25

© UCLES 2023 9618/21/M/J/23


MUHAMMAD ALI KHAN
PAGE 1231

11

Write pseudocode for procedure Mix().

Assume Sample and Result are global.

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

.................................................................................................................................................... [6]

© UCLES 2023 9618/21/M/J/23 [Turn over


PAGE 1228

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

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

.................................................................................................................................................... [6]

© UCLES 2023 9618/21/M/J/23


MUHAMMAD ALI KHAN
PAGE 1177

12

7 A simple arithmetic expression is stored as a string in the format:

<Value1><Operator><Value2>

$QRSHUDWRUFKDUDFWHULVRQHRIWKHIROORZLQJ    í       

Example arithmetic expression strings:


"803+1904"
"34/7"

(a) A procedure Calculate() will:

• take an arithmetic expression string as a parameter


• evaluate the expression
• output the result.

Assume:

• the string contains only numeric digits and a single operator character
• Value1 and Value2 represent integer values
• 9DOXHDQG9DOXHDUHXQVLJQHG WKH\ZLOOQRWEHSUHFHGHGE\  RU í 

(i) Write pseudocode for the procedure Calculate().

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

© UCLES 2022 9618/21/O/N/22


PAGE 1178

13

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

..................................................................................................................................... [7]

(ii) Calculate() is changed to a function that returns the value of the evaluated
expression.

Write the header for the function in pseudocode.

...........................................................................................................................................

..................................................................................................................................... [1]

(b) A string representing an arithmetic expression could be in the correct format but be impossible
to evaluate.

Give an example of a correctly formatted string and explain why evaluation would be
impossible.

Example string ..........................................................................................................................

Explanation ...............................................................................................................................

...................................................................................................................................................

...................................................................................................................................................
[2]

© UCLES 2022 9618/21/O/N/22 [Turn over


MUHAMMAD ALI KHAN
PAGE 1145

10

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

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

© UCLES 2022 9618/22/M/J/22


PAGE 1146

11

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

ENDPROCEDURE [7]

© UCLES 2022 9618/22/M/J/22 [Turn over


MUHAMMAD ALI KHAN
PAGE 1047

10

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

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

© UCLES 2021 9618/22/M/J/21


PAGE 636

3 (a) A student is developing an algorithm to search through a 1D array of 100 elements. Each
element of the array, Result, contains a REAL value.

The algorithm will output:

• the average value of all the elements


• the number of elements with a value of zero.

The structured English description of the algorithm is:

1. SET Total value to 0


2. SET Zero count to 0
3. SELECT the first element
4. ADD value of element to Total value
5. IF element value is 0 then INCREMENT Zero count
6. REPEAT from step 4 for next element, until element is last element
7. SET Average to Total / 100
8. OUTPUT a suitable message and Average
9. OUTPUT a suitable message and Zero count

Write pseudocode for this algorithm.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [7]

© UCLES 2019 9608/21/M/J/19 [Turn over


PAGE 748

4 A program is being written to control the operation of a portable music player. One part of the
program controls the output volume.

The player has two buttons, one to increase the volume and one to decrease it. Whenever a
button is pressed, a procedure Button() is called with a parameter value representing the button
as follows:

Button Parameter value

Volume increase 10

Volume decrease 20

For example, pressing the volume increase button three times followed by pressing the volume
decrease button once would result in the calls:

  &$//%XWWRQ  9RO/HYHOLQFUHDVHGE\
  &$//%XWWRQ  9RO/HYHOLQFUHDVHGE\
  &$//%XWWRQ  9RO/HYHOLQFUHDVHGE\
  &$//%XWWRQ  9RO/HYHOGHFUHDVHGE\

The program makes use of two global variables of type INTEGER as follows:

Variable Description
9RO/HYHO The current volume setting. This must be in the range 0 to 49.
A value that can be set to limit the maximum value of 9RO/HYHO,
LQRUGHUWRSURWHFWWKHXVHU·VKHDULQJ
0D[9RO
A value in the range 1 to 49 indicates the volume limit.
A value of zero indicates that no volume limit has been set.

The procedure Button() will modify the value of 9RO/HYHO depending on which button has
been pressed and whether a maximum value has been set.

© UCLES 2019 9608/22/O/N/19


MUHAMMAD ALI KHAN
PAGE 749

(a) Write pseudocode for the procedure Button(). Declare any additional variables you use.

The value of 0D[9RO should not be changed within the procedure.

Parameter validation is not necessary.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [6]
© UCLES 2019 9608/22/O/N/19 [Turn over

You might also like