Questions which require you to answer in pseudo-code or a programming language are used to examine your ability to write algorithms in logical ways. The examiner will check your answer to see that it meets the meets the requirements outlined in the questions, and to see if you have shown that you have a good understanding of how to structure code.
The following question has been taken from an Edexcel past paper. It reflects the type of question that may appear in an exam paper.
The HappyPetBox Company needs a program to validate customer identifiers.
Valid customer identifiers are nine characters long, ending with three uppercase letters.
Here are four examples of valid customer identifiers.
Write an algorithm that will:
A sample output is shown.
Write an algorithm to meet the requirements.
Use pseudo-code or a programming language with which you are familiar.
Pseudo-code questions are worth a large number of marks and are therefore more complex than many other questions. Make sure you read the whole question carefully. This example answer is going to use pseudo-code rather than write the answer in an actual programming language.
After reading the whole question carefully, look at the requirements. This will help you to decompose the problem.
For this question, you need to write an algorithm that will:
The program will need to read in the customer identifier. The identifiers contain both letters and numbers, so the program will need to read in a string. In pseudo-code you could write:
RECEIVE identifier FROM (STRING) KEYBOARD
The program will need to check if the input contains just a “Q”, and if it does, quit the program. This is a selection, so you can do this using an IF statement.
The program will need to check if the entered identifier is nine characters long (the length required is at the start of the question). If the length isn’t nine characters (again, a selection), the program needs to output an error. In pseudo-code you could write:
IF LENGTH (identifier) <> 9 THEN
SEND ‘The customer identifier is not nine characters long’ TO
The start of the question tells you that the last three characters must be uppercase letters. Again, there is selection here, but it also requires you to loop (iterate) through the last three characters and see if any of them are greater than or equal to ‘A’ and less than or equal to ‘Z’. The question tells you how many times the algorithm has to loop so you should use a FOR loop. If the program finds a character that isn’t an uppercase letter, it will need to issue an error.
In pseudo-code you could write:
SET badAlpha TO FALSE #this is a flag that will be changed to TRUE if a non-uppercase letter is found
FOR count FROM 6 TO 8 DO
IF (NOT (identifier[count] >= ‘A’ AND identifier[count] <= ‘Z’)) THEN
SEND ‘Bad character in last 3 characters found’ TO DISPLAY
SET badAlpha TO TRUE
IF badAlpha = FALSE THEN
SEND ‘Final three characters are valid’ TO DISPLAY
This tells you that you’ll need a loop (iteration) of some form so that the user keeps being asked for identifiers to check until Q is entered.