Question 1 Report
A UK postcode follows one of these formats (where A = letter, 9 = digit):
All postcodes have a space separating the outward and inward parts, and the inward part is always 9AA (digit then two letters).
(a) Write pseudocode for a function IsLetter that returns TRUE if a character is an uppercase letter. [2]
(b) Write pseudocode for a function IsDigit that returns TRUE if a character is a digit. [2]
(c) Write pseudocode for a function ValidateInward that checks the inward part (last 3 characters) follows the pattern: digit, letter, letter. [3]
(a) The IsLetter function checks whether a character falls within the range of uppercase letters 'A' to 'Z' by comparing its value against both boundaries. [2]
FUNCTION IsLetter(Ch : CHAR) RETURNS BOOLEAN
IF Ch >= 'A' AND Ch <= 'Z' THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTIONCharacter comparisons work because characters are stored as numeric codes (e.g. ASCII), where 'A' through 'Z' are consecutive values 65 through 90. If Ch is greater than or equal to 'A' and less than or equal to 'Z', it must be an uppercase letter. Any character outside this range (digits, lowercase letters, punctuation, spaces) returns FALSE.
(b) The IsDigit function works on the same principle, checking whether a character falls within the range '0' to '9'. [2]
FUNCTION IsDigit(Ch : CHAR) RETURNS BOOLEAN
IF Ch >= '0' AND Ch <= '9' THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTIONThe digit characters '0' through '9' occupy consecutive codes (ASCII 48-57). A character that is both >= '0' and <= '9' is guaranteed to be a digit.
(c) The ValidateInward function checks that the inward part of a UK postcode follows the required pattern: one digit followed by two letters (9AA). [3]
FUNCTION ValidateInward(Inward : STRING) RETURNS BOOLEAN
IF LENGTH(Inward) <> 3 THEN
RETURN FALSE
ENDIF
IF IsDigit(Inward[1]) AND IsLetter(Inward[2]) AND IsLetter(Inward[3]) THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTIONThe function performs two checks:
Everything you need to excel in your exams