Question 1 Report
Study the following pseudocode that reverses the order of words in a sentence (not the characters).
FUNCTION ReverseWords(Sentence : STRING) RETURNS STRING
DECLARE Words : ARRAY[1:100] OF STRING
DECLARE WordCount : INTEGER
DECLARE CurrentWord : STRING
DECLARE Result : STRING
DECLARE i : INTEGER
DECLARE Ch : CHAR
WordCount ← 0
CurrentWord ← ""
FOR i ← 1 TO LENGTH(Sentence)
Ch ← Sentence[i]
IF Ch = ' ' THEN
IF CurrentWord <> "" THEN
WordCount ← WordCount + 1
Words[WordCount] ← CurrentWord
CurrentWord ← ""
ENDIF
ELSE
CurrentWord ← CurrentWord & Ch
ENDIF
NEXT i
IF CurrentWord <> "" THEN
WordCount ← WordCount + 1
Words[WordCount] ← CurrentWord
ENDIF
Result ← Words[WordCount]
FOR i ← WordCount - 1 TO 1 STEP -1
Result ← Result & " " & Words[i]
NEXT i
RETURN Result
ENDFUNCTION(a) Trace the word extraction phase for the input "THE CAT SAT". Show the Words array and WordCount after processing. [3]
| Words[1] | Words[2] | Words[3] | WordCount |
|---|---|---|---|
(b) State the return value for the input "THE CAT SAT". [1]
(c) Explain why the code after the FOR loop checks IF CurrentWord <> "". [2]
(d) State the return value for the input "HELLO". [1]
(e) Write pseudocode for a simpler function that reverses the characters (not words) in a string. [7]
(f) The function does not handle multiple consecutive spaces correctly. Explain what would happen with the input "THE CAT" (two spaces) and describe how the algorithm could be modified to handle this. [3]
(g) State two advantages of using a function rather than writing this code directly in the main program. [2]
(h) State the data type returned by the LENGTH function. [1]
(a) The word-extraction loop builds each word character by character until it hits a space, at which point the completed word is stored in the Words array: [3]
| Words[1] | Words[2] | Words[3] | WordCount |
|---|---|---|---|
| THE | CAT | SAT | 3 |
Processing "THE CAT SAT" character by character: T, H, E build CurrentWord = "THE". The space triggers storage into Words[1]. Then C, A, T build "CAT", the next space stores it in Words[2]. Finally S, A, T build "SAT", and the post-loop IF stores it in Words[3]. [1 mark for Words[1] and Words[2], 1 mark for Words[3], 1 mark for WordCount]
(b) The reversal loop starts from Words[3] and prepends each earlier word with a space separator. Result = Words[3] = "SAT", then "SAT" & " " & "CAT" = "SAT CAT", then "SAT CAT" & " " & "THE" = "SAT CAT THE". Return value: "SAT CAT THE". [1]
(c) The FOR loop only stores a word into the array when it encounters a space character. [1] The last word in the sentence is not followed by a space, so it would remain in CurrentWord without being added to the array. The IF check after the loop ensures this final word is also stored correctly. [1]
(d) For input "HELLO", there are no spaces, so the loop never triggers storage. The post-loop IF stores "HELLO" as Words[1] (WordCount = 1). The reversal starts with Result = Words[1] = "HELLO" and the FOR loop from 0 TO 1 STEP -1 does not execute. Return value: "HELLO". [1]
(e) A character-reversal function iterates from the end of the string to the beginning, appending each character to build the reversed result: [7]
FUNCTION ReverseChars(Text : STRING) RETURNS STRING
DECLARE Result : STRING
DECLARE i : INTEGER
Result ← ""
FOR i ← LENGTH(Text) TO 1 STEP -1
Result ← Result & Text[i]
NEXT i
RETURN Result
ENDFUNCTION[1 mark for loop from end to start, 1 mark for building result character by character, 1 mark for returning result]
(f) With input "THE CAT" (two spaces), the algorithm encounters the first space after "THE" and stores it in Words[1]. [1] When it encounters the second space, CurrentWord is empty (""). The IF check IF CurrentWord <> "" prevents this empty string from being added to the array, so the algorithm handles this correctly and produces the same result as single-spaced input. [1] Without this guard, an empty word would be stored, causing the reversed output to contain extra spacing. [1]
(g) Advantage 1: The function can be called multiple times from different parts of the program without rewriting the code (reusability). [1] Advantage 2: The function can be tested independently of the rest of the program, making debugging and maintenance easier (modularity). [1]
(h) The LENGTH function returns an INTEGER, because the number of characters in a string is always a whole number. [1]
Everything you need to excel in your exams