Worked example 1
A cinema charges for tickets based on age. Ages 12 and under pay £6. Ages 13 to 17 pay £8. Ages 18 to 64 pay £12. Seniors (65+) pay £7. Write a pseudocode algorithm that takes an age as input and outputs the correct ticket price.
Show solution outline
This problem requires checking an age against several ranges, making it a perfect candidate for a chained IF...THEN...ELSEIF structure.
DECLARE Age : INTEGER
DECLARE Price : REAL
OUTPUT "Please enter your age: "
INPUT Age
IF Age <= 12 THEN
Price <- 6.00
ELSEIF Age >= 13 AND Age <= 17 THEN
Price <- 8.00
ELSEIF Age >= 18 AND Age <= 64 THEN
Price <- 12.00
ELSE // This covers ages 65 and over
Price <- 7.00
ENDIF
OUTPUT "The ticket price is: £", Price
Marking Points:
- Correctly declaring variables (Age, Price).
- Prompting for and inputting the age.
- Correct use of IF...ELSEIF...ELSE...ENDIF structure.
- Correct conditions for each age bracket.
- Assigning the correct price for each case.
- Outputting the final calculated price.