Question 1 Report
Study the following pseudocode that counts how many times each vowel appears in a word.
DECLARE Word : STRING
DECLARE Counts : ARRAY[1:5] OF INTEGER
DECLARE Vowels : STRING
DECLARE i : INTEGER
DECLARE j : INTEGER
DECLARE Ch : CHAR
Vowels ← "aeiou"
Word ← "orange"
FOR i ← 1 TO 5
Counts[i] ← 0
NEXT i
FOR i ← 1 TO LENGTH(Word)
Ch ← LCASE(Word[i])
FOR j ← 1 TO 5
IF Ch = Vowels[j] THEN
Counts[j] ← Counts[j] + 1
ENDIF
NEXT j
NEXT i(a) Complete the final state of the Counts array after processing the word "orange". [3]
| Vowels[j] | a | e | i | o | u |
|---|---|---|---|---|---|
| Counts[j] |
(b) Show the trace for processing just the first three characters of "orange". [3]
| i | Ch | Is vowel? | Which vowel? | Count updated |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 |
(c) State why LCASE is used on each character. [1]
(d) State one disadvantage of this approach compared to using a single counter variable. [1]
(a) The pseudocode iterates through each character of the word "orange" (o, r, a, n, g, e), converts it to lowercase with LCASE, and checks whether it matches any of the five vowels stored in the Vowels string "aeiou". Each match increments the corresponding element of the Counts array. [3]
| Vowels[j] | a | e | i | o | u |
|---|---|---|---|---|---|
| Counts[j] | 1 | 1 | 0 | 1 | 0 |
Working through the word character by character:
The vowels 'i' and 'u' do not appear in "orange", so Counts[3] and Counts[5] remain at 0.
(b) The trace table below shows the state for each of the first three characters of "orange" (o, r, a). For each character, the inner loop checks all five vowels. [3]
| i | Ch | Is vowel? | Which vowel? | Count updated |
|---|---|---|---|---|
| 1 | o | Yes | o (j=4) | Counts[4] = 1 |
| 2 | r | No | - | No update |
| 3 | a | Yes | a (j=1) | Counts[1] = 1 |
At i=1, the character 'o' is extracted and converted to lowercase (already lowercase). The inner loop compares it against each vowel in turn: a (no), e (no), i (no), o (yes, at j=4). Counts[4] is incremented from 0 to 1. At i=2, 'r' is not a vowel, so no counter is updated. At i=3, 'a' matches at j=1, so Counts[1] becomes 1.
(c) LCASE converts each character to lowercase before comparison so that the algorithm works correctly regardless of whether the input word contains uppercase or lowercase letters. Without LCASE, an uppercase vowel such as 'O' would not match the lowercase 'o' stored in the Vowels string, and the count would be incorrect. This makes the comparison case-insensitive. [1]
(d) Using five separate counters (one per vowel) requires more memory (an array of five integers rather than a single integer) and a nested loop that checks every vowel for every character, making the algorithm slower. A single counter variable that increments whenever any vowel is found would be more memory-efficient and would need only a single-level check per character. However, the trade-off is that the array approach tracks which specific vowels appeared and how often, whereas a single counter only gives the total vowel count. [1]
Everything you need to excel in your exams