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

Assessment: Computer Science 0478 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science - 0478

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]aeiou
Counts[j]

(b) Show the trace for processing just the first three characters of "orange". [3]

iChIs 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]

Answer Details

(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]aeiou
Counts[j]11010

Working through the word character by character:

  • 'o' matches Vowels[4], so Counts[4] increments to 1
  • 'r' does not match any vowel, so no count changes
  • 'a' matches Vowels[1], so Counts[1] increments to 1
  • 'n' does not match any vowel
  • 'g' does not match any vowel
  • 'e' matches Vowels[2], so Counts[2] increments to 1

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]

iChIs vowel?Which vowel?Count updated
1oYeso (j=4)Counts[4] = 1
2rNo-No update
3aYesa (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]

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning