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 ,julia Lobur
Chapter:
Arrays
Exercise:
Programming Projects
Question:11 | ISBN:9780321531346 | Edition: 7

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 foranother choice

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program code:

//Header file section
#include <iostream>
#include <vector>
using namespace std;

//Define constant variable names
const int ROWS = 7;
const int COLS = 4;

// Function to display the seat pattern
void displaySeats(const vector<vector<char>>& seats) 
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            cout << seats[i][j] << ' ';
        }
        cout << endl;
    }
}

// Function to check if a seat is available
bool isSeatAvailable(const vector<vector<char>>& seats, int row, int col) 
{
    return seats[row][col] != 'X';
}

// Function to assign a seat
void assignSeat(vector<vector<char>>& seats, int row, int col)
{
    seats[row][col] = 'X';
}
//Program starts with a main function
int main()
{
    // Create and initialize the seat pattern
    vector<vector<char>> seats(ROWS, vector<char>(COLS, ' '));

    // Display the initial seat pattern
    displaySeats(seats);

    // Main loop to assign seats
    while (true) 
    {
        int row, col;

        // Prompt for seat choice
        cout << "Enter the row and column of the seat (e.g., 1 2): ";
        cin >> row >> col;

        // Adjust row and col for 0-based indexing
        row--;
        col--;

        // Check if the seat is valid
        if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
            cout << "Invalid seat choice. Please try again." << endl;
            continue;
        }

        // Check if the seat is available
        if (isSeatAvailable(seats, row, col)) 
        {
            // Assign the seat
            assignSeat(seats, row, col);
            cout << "Seat assigned successfully!" << endl;

            // Display the updated seat pattern
            displaySeats(seats);
        } else 
        {
            cout << "Sorry, that seat is already occupied. Please choose another seat." << endl;
        }

        // Check if all seats are filled
        bool allSeatsFilled = true;
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) 
            {
                if (seats[i][j] != 'X') 
                {
                    allSeatsFilled = false;
                    break;
                }
            }
            if (!allSeatsFilled) 
            {
                break;
            }
        }

        // Break the loop if all seats are filled or user signals to end
        if (allSeatsFilled) 
        {
            cout << "All seats are filled. Exiting the program." << endl;
            break;
        }

        char choice;
        cout << "Do you want to continue assigning seats? (Y/N): ";
        cin >> choice;

        // Break the loop if the user chooses to end
        if (choice == 'N' || choice == 'n') 
        {
            cout << "Exiting the program." << endl;
            break;
        }
    }

    return 0;
}

OUTPUT OF THE PROGRAM CODE:

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

Enter the row and column of the seat (e.g., 1 2): 1 1
Seat assigned successfully!
X 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

Do you want to continue assigning seats? (Y/N): Y
Enter the row and column of the seat (e.g., 1 2): 2 2
Seat assigned successfully!
X B C D
2 A X 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

Do you want to continue assigning seats? (Y/N): Y
Enter the row and column of the seat (e.g., 1 2): 4 3
Seat assigned successfully!
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

Do you want to continue assigning seats? (Y/N): N
Exiting the program.

 

0 0

Discussions

Post the discussion to improve the above solution.