Question 1 Report
Fig. 1 shows an expression tree from a cinema booking program. The program calculates the number of free seats left after online bookings and then gives each booking a row number. Table 1 is a small test data set from one screen. The programmer must use operators that preserve whole seat numbers. A row contains 12 seats, numbered from 1 to 12. The code editor will display an error if a word is used instead of a number in an arithmetic calculation.
Table 1
| Screen | rows | online bookings | desk bookings |
|---|---|---|---|
| 2 | 9 | 38 | 17 |
| 5 | 14 | 64 | 23 |
| 7 | 8 | 31 | 14 |
(a) Identify the order in which the two types of operator at the top of Fig. 1 are evaluated. [3]
(b) Use Table 1 to calculate the value of freeSeats for Screen 2. [4]
(c) Complete the pseudocode used to find a seat's row number.rowNumber ← (seatNumber - 1) ___ 12 + 1 [5]
(d) Write an algorithm using arithmetic and relational operators that displays VALID only when seatNumber is from 1 to rows * 12, inclusive. [8]
(a) Multiplication is evaluated first [2]. Then the additions and subtraction are evaluated according to the brackets represented by the expression tree [1]. Thus the calculation is \((rows\times12)-(online+desk)\).
(b) For Screen 2:
\[9\times12=108\] [1]
\[38+17=55\] [1]
\[108-55=53\] [1]
So freeSeats is 53 seats [1].
(c) Use DIV [5]: rowNumber ← (seatNumber - 1) DIV 12 + 1. Subtracting 1 makes seats 1 to 12 become values 0 to 11; integer division by 12 identifies the zero-based row; adding 1 restores normal row numbering.
(d) A correct algorithm is:
IF seatNumber >= 1 AND seatNumber <= rows * 12 THEN DISPLAY "VALID" ENDIF
The first comparison rejects values below seat 1, and the second includes the final seat in the screen [4]. The display statement is required [2], as is a correct completed control structure [2].
Everything you need to excel in your exams