Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows:
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
The program should display the seat pattern, with an ‘X’ marking the seats already assigned. After displaying the seats available, the program prompts the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that the seat is occupied and ask for another choice.
Input Example:
For example, after seats 1A, 2B, and 4C are taken, the display should look like:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
The whole problem can be implemented with help of 4 major functions:
The entire problem is discussed dividing into parts focusing on functional modularity.
1. Initialize a 2-D array to represent the seat matrix
A 2-D character array is used to represent the seat matrix where the first column have the row number of each seat & the rest of the columns have four seat A,B,C,D respectively. Thus it’s a 7X5 2-D array to represent the airplane seat matrix which looks like following:
2. Take user input for seat no
User is requested to input the desired seat no by giving corresponding no of seat. Like a valid seat no is 1A, 3C, 7D and so on, whereas, an invalid seat request can be 6F, 0B so on.
The input is taken by our function getData() which takes user input & returns the string.
3. Check the seat no request (check() )
Our major function for this problem is to check the validity of the seat request & update seat matrix status as per request.
Then prompt a message stating “It’s already occupied”
This checking can be done by founding the 2-D array row & column index for the corresponding seat.
Let, row_index=r&column_index=c
If(seat_matrix[row_index][column_index]==’X’)
Seat is occupied.
Update()
4. Update the valid entry
If the request is to update the valid seat we simple change its value to 'X'. It can be done by finding row & column index and updating the value of seat_matrix at that location to 'X'.
5. Special function to check whether all seats are occupied
The program also need to be terminated when all seats are occupied. Thus every time we keep a checking whether all the entry of seat_matrix is 'X' or not.
C++ implementation for Airline Seat Reservation Problem
Output
need an explanation for this answer? contact us directly to get an explanation for this answer