Question C2 The program below simulates a seat reservation system of a theatre. At first, all the seats in the theatre are empty (marked with .'). Seats can be chosen based on user's input or automatically by the system. Reserved seats are marked with 'X' in the seating plan. Each time only consecutive seats along the same row will be reserved. #include #include using namespace std; const int ROW = 5; const int COL = 8; void init (char seats [ROW] [COL]) { for (int i = 0; i < ROW; i++) } void print (char seats [ROW] [COL], int vacancy) { cout << "Vacant Seats: " << vacancy << endl; cout << "Seating Plan:" << endl; // Your codes for C2 (a) should be inserted here } void getInput (int &r, int &c, int &num) { } for (int j = 0; j < COL; j++) seats[j] = !. '; } bool enough (char seats [ROW] [COL], int r, int c, int num) { // Your codes for C2 (c) should be inserted here char input [5]; // stores input seat number cout << "Reservation\n"; cout << "_ ---------\n"; // Your codes for C2 (b) should be inserted here void mark (char seats [ROW] [COL], int r, int c, int num) { // Your codes for C2 (d) should be inserted here } } bool autoSelect (char seats [ROW] [COL], int &r, int &c, int num) for (r = 0; r < ROW; r++) { for (c=0; c < COL; C++) if (enough (seats, r, c, num)) return true; return false;
int main() { int row, col, num; bool hasSeats = false; int vacancy=ROW COL; char seat Plan [ROW] [COL]; init (seat Plan); do { A B C D print (seat Plan, vacancy); get Input (row, col, num); E if (row == -1 && col == -1) else hasSeats autoSelect (seat Plan, row, col, num); hasSeats w enough (seat Plan, row, col, num); if (hasSeats) mark (seat Plan, row, col, num); vacancy = num; cout << "Seats are reserved.\n\n"; hasSeats = false; else { cout << "Not enough seats.\n\n"; } while (vacancy > 0); The sample output below will be shown when all parts are completed correctly. Approriate use of loop structure and constant variables are expected in your answers. cout << "Thanks for using. Good-bye!" << endl; return 0; Sample Output: (the vertical display is shown in row format due to space concern) Vacant Seats: 40 Seating Plan: Vacant Seats: 35 Seating Plan: 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 X X X X X. Reservation How many seats? 5 Seat number: A2 Seats are reserved. A B C D E. + Reservation How many seats? 4 Seat number: A0 Not enough seats. Vacant Seats: 35 Seating Plan: 0 1 2 3 4 5 6 7 X X X X X. A B C E Reservation How many seats? 4 Seat number: BO Seats are reserved.
Question C2 (continued) Vacant Seats: 31. Seating Plan: 0 1 2 3 4 5 6 7 A . X X X X X B X X X X C D E . . . . ▸ Reservation + How many seats? 3. Seat number: C3 Seats are reserved. Vacant Seats: 28. Seating Plan: 0 1 2 3 4 5 6 7 . X X X X X A. BX X X X . D E XXX . Reservation How many seats? 3. Seat number: AUTO Seats are reserved. Vacant Seats: 25 Seating Plan: (...to be answered in C2(e)...)
(c) Complete the enough() function that checks if there are mum consecutive empty seats along row r, starting from the seat at (r. c) on the seating plan. If so, the function returns true. Otherwise, it returns false. (4 marks) (d) Complete the mark() function that marks 'X' in the seat array, to indicate that num seats are reserved along row r, starting from the seat at (r, c) on the seating plan. (4 marks) (e) Following the last seat reservation from the sample output and the algorithm of the given program, suggest the range of 3 seats that are reserved automatically by the system. (2 marks)
Question C2 The program below simulates a seat reservation system of a theatre. At first, all the seats in the theatre a
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am