I have a project to get it done, but I got stuck on some part of it. Here is the
ID: 3759810 • Letter: I
Question
I have a project to get it done, but I got stuck on some part of it. Here is the program to be done:
Write a program that reserves seats for a theater, airline, etc. using a two-dimensional array. The program should start by asking for the number of rows and seats on each row and display the available seats. The rows are to be identified by a number starting at '1'. The seats on each row are to be identified by a letter, 'A', 'B', 'C', etc. For example, 3C, 4A and 2D are sample seat identifiers.
Because the total number of rows and seats is unknown when the program first starts, it will be necessary for the program to dynamically allocate memory for the seating array and then release that memory when the program is ready to end.
The program should ask the user to select row and seat. The program needs to verify and process only legal row and seat requests. If the seat is already reserved, the program should notify the user that the seat is taken. If the seat is available, the program should replace the seat identifier with a dash character '-'. The program should contain a loop to ask if additional seats are to be reserved. The program should also give a message when all the seats are taken.
When running the program, you must use a minimumof 3 rows and 4 seats. The program output part of thelab report must show these minimums.
My question is how to implement the code inside the "Do Loop" as you can see the on the comments to get them done?
So far I have written the code as it follows:
#include <iostream>
#include <cctype>
using namespace std;
// Function declarations (prototypes)
char **CreateArrayOfSeats(int NumberOfRows, int SeatsOnRow);
void InitializeSeats(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
void DisplayArrayOfSeats(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
void MemoryCleanup(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
int main(int argc, char* argv[])
{
char **ArrayOfSeats;
int NumberOfRows;
int NumberOfSeats;
char answer; // user input for "Do you want another seat?"
// get the number of NumberOfRows and SeatsOnRow from the user
cout << "Enter the number of rows: ";
cin >> NumberOfRows;
cout << "Enter the number of seats on each row: ";
cin >> NumberOfSeats;
ArrayOfSeats = CreateArrayOfSeats(NumberOfRows, NumberOfSeats);
InitializeSeats(ArrayOfSeats, NumberOfRows, NumberOfSeats);
DisplayArrayOfSeats(ArrayOfSeats, NumberOfRows, NumberOfSeats);
do
{
int rowSelection; // 1 to max NumberOfRows, input from the user
char seatSelection; // 'A' to max seats, input from the user, convert to a number
int row; // index into ArrayOfSeats, 0 to NumberOfRows-1
int seat; // index into ArrayOfSeats, 0 to seats-1
//char taken = '-'; // seats taken
cout << "Enter a seat selection (example 5F): ";
cin >> rowSelection; // get row from the user
cin >> seatSelection; // get the seat from the user
seatSelection = toupper(seatSelection); // convert to upper case
row = rowSelection - 1; // count from zero to work with the array
seat = seatSelection - 'A'; // convert 'A' to 0, 'B' to 1, 'C' to 2, etc.
// Verify that a valid row and seat were entered
// See if all the seats are taken
// hint, keep a count each time a seat is taken
// and compare to the maximum number of seats (NumberOfRows*seats)
// See if the seat selection is already taken (see if it has an '-')
// if it has an '-', display a message that the seat is taken
// else, put an '-' in that location
/*
if (**ArrayOfSeats)
{
for (int r = 0; r < rowSelection; r++)
{
for (int s = 0; s < seatSelection; s++)
ArrayOfSeats[r][s] = '-';
cout << endl;
}
cout << "The seat is taken ";
}
else
{
cout << "Invalid seat choice";
}
*/
DisplayArrayOfSeats(ArrayOfSeats, NumberOfRows, NumberOfSeats);
cout << "Do you want to select another seat (y/n)? ";
cin >> answer;
answer = toupper(answer);
} while (answer == 'Y');
MemoryCleanup (ArrayOfSeats, NumberOfRows, NumberOfSeats); // return the memory that was allocated
cout << "Press the ENTER key to continue...";
char buff[100];
cin.getline (buff, 100);
return 0;
}
char **CreateArrayOfSeats(int NumberOfRows, int SeatsOnRow) // ** means pointer to pointers
{
char **ArrayOfSeats;
ArrayOfSeats = new char*[NumberOfRows]; // create an array of pointers for the NumberOfRows
for(int r = 0; r < NumberOfRows; r++)
ArrayOfSeats[r] = new char[SeatsOnRow]; // create an array of SeatsOnRow for each row
return ArrayOfSeats; // return pointer to the array back to the main program
}
void InitializeSeats(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow)
{
for (int r = 0; r < NumberOfRows; r++) // initialize the data for each row
{
for (int s = 0; s < SeatsOnRow; s++)
ArrayOfSeats[r][s] = 'A' + s; // put 'A' 'B' 'C' etc in each row
}
}
void DisplayArrayOfSeats(char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
{
for (int r = 0; r < NumberOfRows; r++) // for each row
{
cout.width(2);
cout << r+1 << ' '; // Display row numbers starting from 1
for (int s = 0; s < NumberOfSeats; s++)
cout << ArrayOfSeats[r][s] << ' '; // Display info for each seat
cout << endl; // new line after each row
}
}
void MemoryCleanup(char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
{
for (int r = 0; r < NumberOfRows; r++)
delete [] ArrayOfSeats[r]; // delete each row of seats individually
delete [] ArrayOfSeats; // delete the row array
}
Explanation / Answer
#include <iostream>
#include <cctype>
using namespace std;
// Function declarations (prototypes)
char **CreateArrayOfSeats(int NumberOfRows, int SeatsOnRow);
void InitializeSeats(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
void DisplayArrayOfSeats(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
void MemoryCleanup(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow);
int main(int argc, char* argv[])
{
char **ArrayOfSeats;
int NumberOfRows;
int NumberOfSeats;
char answer; // user input for "Do you want another seat?"
// get the number of NumberOfRows and SeatsOnRow from the user
cout << "Enter the number of rows: ";
cin >> NumberOfRows;
cout << "Enter the number of seats on each row: ";
cin >> NumberOfSeats;
ArrayOfSeats = CreateArrayOfSeats(NumberOfRows, NumberOfSeats);
InitializeSeats(ArrayOfSeats, NumberOfRows, NumberOfSeats);
DisplayArrayOfSeats(ArrayOfSeats, NumberOfRows, NumberOfSeats);
do
{
int rowSelection; // 1 to max NumberOfRows, input from the user
char seatSelection; // 'A' to max seats, input from the user, convert to a number
int row; // index into ArrayOfSeats, 0 to NumberOfRows-1
int seat; // index into ArrayOfSeats, 0 to seats-1
//char taken = '-'; // seats taken
cout << "Enter a seat selection (example 5F): ";
cin >> rowSelection; // get row from the user
cin >> seatSelection; // get the seat from the user
seatSelection = toupper(seatSelection); // convert to upper case
row = rowSelection - 1; // count from zero to work with the array
seat = seatSelection - 'A'; // convert 'A' to 0, 'B' to 1, 'C' to 2, etc.
// Verify that a valid row and seat were entered
// See if all the seats are taken
// hint, keep a count each time a seat is taken
// and compare to the maximum number of seats (NumberOfRows*seats)
// See if the seat selection is already taken (see if it has an '-')
// if it has an '-', display a message that the seat is taken
// else, put an '-' in that location
/*
if (**ArrayOfSeats)
{
for (int r = 0; r < rowSelection; r++)
{
for (int s = 0; s < seatSelection; s++)
ArrayOfSeats[r][s] = '-';
cout << endl;
}
cout << "The seat is taken ";
}
else
{
cout << "Invalid seat choice";
}
*/
DisplayArrayOfSeats(ArrayOfSeats, NumberOfRows, NumberOfSeats);
cout << "Do you want to select another seat (y/n)? ";
cin >> answer;
answer = toupper(answer);
} while (answer == 'Y');
MemoryCleanup (ArrayOfSeats, NumberOfRows, NumberOfSeats); // return the memory that was allocated
cout << "Press the ENTER key to continue...";
char buff[100];
cin.getline (buff, 100);
return 0;
}
char **CreateArrayOfSeats(int NumberOfRows, int SeatsOnRow) // ** means pointer to pointers
{
char **ArrayOfSeats;
ArrayOfSeats = new char*[NumberOfRows]; // create an array of pointers for the NumberOfRows
for(int r = 0; r < NumberOfRows; r++)
ArrayOfSeats[r] = new char[SeatsOnRow]; // create an array of SeatsOnRow for each row
return ArrayOfSeats; // return pointer to the array back to the main program
}
void InitializeSeats(char **ArrayOfSeats, int NumberOfRows, int SeatsOnRow)
{
for (int r = 0; r < NumberOfRows; r++) // initialize the data for each row
{
for (int s = 0; s < SeatsOnRow; s++)
ArrayOfSeats[r][s] = 'A' + s; // put 'A' 'B' 'C' etc in each row
}
}
void DisplayArrayOfSeats(char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
{
for (int r = 0; r < NumberOfRows; r++) // for each row
{
cout.width(2);
cout << r+1 << ' '; // Display row numbers starting from 1
for (int s = 0; s < NumberOfSeats; s++)
cout << ArrayOfSeats[r][s] << ' '; // Display info for each seat
cout << endl; // new line after each row
}
}
void MemoryCleanup(char **ArrayOfSeats, int NumberOfRows, int NumberOfSeats)
{
for (int r = 0; r < NumberOfRows; r++)
delete [] ArrayOfSeats[r]; // delete each row of seats individually
delete [] ArrayOfSeats; // delete the row array
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.