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. In the exam you may answer in pseudocode, Python, Visual Basic or Java. Here, write pseudocode.
A car park has spaces. A sensor at the gate sends the input IN when a car enters and OUT when a car leaves. At the end of the day the attendant inputs END. Write an algorithm that:
IN and takes one car away for OUT;FULL when there are no free spaces left;END, outputs how many cars came in during the day.Use meaningful names and add comments. Then work out what your algorithm outputs at the end for the inputs IN, IN, OUT, IN, IN, END.
Flowchart symbols: terminator (START, STOP), process (rectangle), input/output (parallelogram), decision (diamond, two exits Yes and No), subroutine (rectangle with a line inside each end), flow line (arrow). A flow line back to an earlier shape makes a loop. Conditions are written precisely, such as Count > 10.
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 counters, a constant for the size of the car park, and a loop that runs until END.
A model answer.
// Car park counter
CONSTANT Spaces ← 40
Cars ← 0 // cars in the car park now
CarsIn ← 0 // cars that came in today
INPUT Signal
WHILE Signal <> "END" DO
IF Signal = "IN"
THEN
Cars ← Cars + 1
CarsIn ← CarsIn + 1
ELSE
IF Signal = "OUT"
THEN
Cars ← Cars - 1
ELSE
OUTPUT "Error: input IN, OUT or END"
ENDIF
ENDIF
OUTPUT "Free spaces: ", Spaces - Cars
IF Cars = Spaces
THEN
OUTPUT "FULL"
ENDIF
INPUT Signal
ENDWHILE
OUTPUT "Cars in today: ", CarsInThe run. After IN, IN, OUT, IN, IN there are cars inside, so the free spaces are . Four inputs were IN, so the output at the end is Cars in today: 4.
A marking list. A teacher would give one mark for each point like these.
END is input.IN adds to the cars inside.IN also adds to the cars in today.OUT takes from the cars inside.Spaces - Cars, are worked out.FULL is output when the cars inside equal the spaces.Answer: free spaces after the last car, and cars came in.
What if? A careful answer also refuses IN when the car park is full, and refuses OUT when it is empty. Add AND Cars < Spaces to the first test and AND Cars > 0 to the second.
Log in to join the discussion.
Loading…