SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:10 | ISBN:9780132846813 | Edition: 5

Question

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. For example, after seats 1A, 2B, and 4C are taken, the display should look like this:

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

After displaying the seats available, the program prompts for 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 that seat is occupied and ask for another choice.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program code:

//Header section
#include <iostream>
#include <vector>
using namespace std;
// Function to display the seat pattern
void displaySeats(const vector<vector<char>>& seats) 
{
    cout << "Seat Pattern:" << endl;

    // Display the seat pattern
    for (const auto& row : seats) {
        for (const auto& seat : row) {
            cout << seat << " ";
        }
        cout << endl;
    }

    cout << endl;
}
//main method
int main()
{
    //Initialize variables
    const int ROWS = 7;
    const int SEATS_PER_ROW = 4;

    // Create a 2D vector to represent the seat pattern
    vector<vector<char>> seats(ROWS, vector<char>(SEATS_PER_ROW, ' '));

    bool endProgram = false;

    // Main program loop
    while (!endProgram) {
        // Display the seat pattern
        displaySeats(seats);

        // Prompt for the seat desired
        cout << "Enter the seat desired (e.g., 3B) or type 'end' to exit: ";
        string seat;
        cin >> seat;

        // Check if the user wants to end the program
        if (seat == "end") {
            endProgram = true;
            continue;
        }

        // Extract the row and seat values from the input
        int row = seat[0] - '0';  // Convert the character to integer
        char seatChar = seat[1];

        // Check if the seat is already assigned
        if (seats[row - 1][seatChar - 'A'] == 'X') {
            cout << "That seat is already occupied. Please choose another seat." << endl;
            continue;
        }

        // Assign the seat
        seats[row - 1][seatChar - 'A'] = 'X';
    }

    return 0;
}

Output of the program code:

Seat Pattern:
  A B C D
1      
2      
3      
4      
5      
6      
7      

Enter the seat desired (e.g., 3B) or type 'end' to exit: 1A

Seat Pattern:
  A B C D
1 X    
2      
3      
4      
5      
6      
7      

Enter the seat desired (e.g., 3B) or type 'end' to exit: 2B

Seat Pattern:
  A B C D
1 X    
2 X    
3      
4      
5      
6      
7      

Enter the seat desired (e.g., 3B) or type 'end' to exit: 4C

Seat Pattern:
  A B C D
1 X    
2 X    
3      
4    X 
5      
6      
7      

Enter the seat desired (e.g., 3B) or type 'end' to exit: 6D

Seat Pattern:
  A B C D
1 X    
2 X    
3      
4    X 
5      
6       X
7      

Enter the seat desired (e.g., 3B) or type 'end' to exit: end

 

0 0

Discussions

Post the discussion to improve the above solution.