CSc Practical Worksheet 1
A’S
Topic: Software Development
1 The following is pseudocode for a string handling function.
For the built-in functions list, refer to the Appendix.
FUNCTION Search(InString : STRING) RETURNS INTEGER
DECLARE NewString : STRING
DECLARE Index : INTEGER
DECLARE NextChar : CHAR
DECLARE Selected : INTEGER
DECLARE NewValue : INTEGER
NewString '0'
Selected 0
FOR Index 1 TO LENGTH(InString)
NextChar MID(InString, Index, 1)
IF NextChar < '0' OR NextChar > '9'
THEN
NewValue STRING_TO_NUM(NewString)
IF NewValue > Selected
THEN
Selected NewValue
ENDIF
NewString '0'
ELSE
NewString NewString & NextChar
ENDIF
ENDFOR
RETURN Selected
ENDFUNCTION
(a) (i) The following assignment calls the Search() function:
Result Search("12∇34∇5∇∇39")
Complete the following trace table by performing a dry run of this function call.
The symbol '∇' represents a space character. Use this symbol to represent a space
character in the trace table.
Index NextChar Selected NewValue NewString
[5]
(ii) State the value returned by the function when it is called as shown in part (a)(i).
....................................... [1]
(b) There is an error in the algorithm. When called as shown in part (a)(i), the function did not
return the largest value as expected.
(i) Explain why this error occurred when the program called the function.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
(ii) Describe how the algorithm could be amended to correct the error.
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
..................................................................................................................................... [2]
Appendix
Built-in functions (pseudocode)
Each function returns an error if the function call is not properly formed.
MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING
returns a string of length y starting at position x from ThisString
Example: MID("ABCDEFGH", 2, 3) returns "BCD"
LENGTH(ThisString : STRING) RETURNS INTEGER
returns the integer value representing the length of ThisString
Example: LENGTH("Happy Days") returns 10
LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING
returns leftmost x characters from ThisString
Example: LEFT("ABCDEFGH", 3) returns "ABC"
RIGHT(ThisString : STRING, x : INTEGER) RETURNS STRING
returns rightmost x characters from ThisString
Example: RIGHT("ABCDEFGH", 3) returns "FGH"
INT(x : REAL) RETURNS INTEGER
returns the integer part of x
Example: INT(27.5415) returns 27
NUM_TO_STRING(x : REAL) RETURNS STRING
returns a string representation of a numeric value.
Example: NUM_TO_STRING(87.5) returns "87.5"
Note: This function will also work if x is of type INTEGER
STRING_TO_NUM(x : STRING) RETURNS REAL
returns a numeric representation of a string.
Example: STRING_TO_NUM("23.45") returns 23.45
Note: This function will also work if x is of type CHAR
Operators (pseudocode)
Operator Description
Concatenates (joins) two strings
&
Example: "Summer" & " " & "Pudding" produces "Summer Pudding"
Performs a logical AND on two Boolean values
AND
Example: TRUE AND FALSE produces FALSE
Performs a logical OR on two Boolean values
OR
Example: TRUE OR FALSE produces TRUE