Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This is the kind of long scenario question that ends Paper 2. Answer in pseudocode.
A cinema ticket machine sells tickets to a group, one person at a time. For each person the machine asks for an age, until is input. The rules are:
Write the algorithm, with a prompt before each input and comments. Then give the two final outputs for the inputs , , , , , .
Cambridge pseudocode: keywords in capitals, names in mixed case, ← for assignment, INPUT and OUTPUT, IF ... THEN ... ELSE ... ENDIF, CASE OF ... OTHERWISE ... ENDCASE, FOR ... TO ... STEP ... NEXT, WHILE ... DO ... ENDWHILE, REPEAT ... UNTIL. DIV(a, b) is the whole-number quotient and MOD(a, b) the remainder.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Two running values (Tickets and Total), a pre-condition loop that stops on , a validity check, then the price.
A model answer.
// Ticket machine for a group
Total ← 0
Tickets ← 0
OUTPUT "Enter an age, or -1 to finish"
INPUT Age
WHILE Age <> -1 DO
IF Age < 0 OR Age > 120
THEN
OUTPUT "Age must be 0 to 120"
ELSE
IF Age < 12
THEN
Price ← 5 // child
ELSE
IF Age < 65
THEN
Price ← 9 // adult
ELSE
Price ← 6 // senior
ENDIF
ENDIF
Total ← Total + Price
Tickets ← Tickets + 1
ENDIF
OUTPUT "Enter an age, or -1 to finish"
INPUT Age
ENDWHILE
OUTPUT "Tickets: ", Tickets
OUTPUT "Total: ", TotalThe run. is an adult: . is a child: . is a senior: . is out of range, so it gets the error and no ticket. is an adult: . Then ends the loop. Tickets: . Total: dollars.
A marking list. One mark for each point.
Total starts at .Tickets starts at .Total.Tickets goes up by for each valid age.Answer: Tickets: 4 and Total: 29.
Check. Good test data here includes the boundaries and , and , and and with their rejected neighbours and . Note that cannot be used as test data for the range check, because it is the stop value.
Log in to join the discussion.
Loading…