Question

In: Computer Science

Solve the question using 2 dimensional pointers. You must use new and delete operator in the...

Solve the question using 2 dimensional pointers. You must use new and delete operator in the program and try to make an easier logic of the problem.

Q2. Write a C++ program using a dynamic array (or arrays) to assign passengers seats in a Bus and your program will ask the user how many rows the Bus has and will handle that many rows (Assume the Bus does not always have the same rows). (5 points)
Expected output: Assume a small Bus 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
8 A B C D
9 A B C D
10 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: (5 points)
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
8 A B C D
9 A B C D
10 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 the seat is occupied and ask for
another choice. (10 points)

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

main.cpp

#include <iostream>
#include<iomanip>
using namespace std;
//check if empty seat in flight
int checkIsEmpty(int desiredRow,int seatNumber,char ** busAvalSeats){
if(busAvalSeats[desiredRow][seatNumber]=='*'){
busAvalSeats[desiredRow][seatNumber]='X';
return true;
}
return false;
}
//print menu and flight details
void printMenu(char ** flight,int seatRows){
cout<<"------------------------------------------------------------------------"<<endl;
cout<<setw(40)<<"MY BUS MANAGEMENT SYSTEM "<<endl;
cout<<"------------------------------------------------------------------------"<<endl;
cout<<setw(11)<<" ";
for(int i=0;i<4;i++){
cout<<setw(5)<<char('A'+i);
}
cout<<endl;
for(int i=0;i<seatRows;i++){
cout<<"Row "<<setw(7)<<i+1;
for(int j=0;j<4;j++){
cout<<setw(5)<<flight[i][j];
}
cout<<endl;
}
}
int main () {
int seatRows;
cout<<"Enter number of seat in bus : ";
cin>>seatRows;
char ** busAvalSeats;
busAvalSeats = new char*[seatRows];
for(int i=0;i<seatRows;i++)
   busAvalSeats[i] = new char[4];

for(int i=0;i<seatRows;i++)
for(int j=0;j<4;j++)
busAvalSeats[i][j]='*';

//print flight
printMenu(busAvalSeats,seatRows);
char choice='y';
char seat;
int seatNumber;
int row;
while(choice=='y'||choice=='Y'){

while(true){
cout<<"Enter row : ";
cin>>row;
cout<<"Enter desire seat (A-D) : "<<endl;
cin>>seat;
while(seat=='\n'){
cin>>seat;
}
row--;
if(seat>='A' && seat<='D' && row>=0 && row<seatRows){
break;
}else{
cout<<"Please enter valid option"<<endl;
}
}
seatNumber = seat - 'A';
if(checkIsEmpty(row,seatNumber,busAvalSeats)){
cout<<"Congratulation!!! Your booking is successful"<<endl;
}else{
cout<<"!!!Sorry . The seat is already booked"<<endl;
}
printMenu(busAvalSeats,seatRows);

cout<<"\nContinue y/n : ";
do{
cin>>choice;
}while(choice=='\n');
}
delete []busAvalSeats;
}

output


Related Solutions

4.2 Lab: new and delete new is the operator used to dynamically allocate memory while the...
4.2 Lab: new and delete new is the operator used to dynamically allocate memory while the program is running delete is the operator used to release memory when it is no longer needed, so it could be used next time new is used This lab gives you an example of using new and delete with an array of structures. Your tasks are listed below: review the existing code finish writing the copyList()function // Lab: new and delete #include <iostream> #include...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.    ...
Chapter 9 (Pointers) – Review Questions Short Answer 1.     What does the indirection operator do? 2.     Look at the following code. int x = 7; int *ptr = &x; What will be displayed if you send the expression *ptr to cout? What happens if you send the expression ptr to cout? 3.     So far you have learned three different uses for the * operator. What are they? 4.     What math operations are allowed on pointers? 5.     Assuming ptr is a...
Write your own R-function for the linear regression using qr() function. You must not use solve()...
Write your own R-function for the linear regression using qr() function. You must not use solve() function. The input of your function would be a design matrix X and a response vector y and the output must contain - least sqaure estimates and the corresponding stnadard errors. - residuals and MSE - fitted values - ANOVA table
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU...
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU MUST USE POINTERS. 3. YOU MUST USE THE SWITCH STATEMENT TO EXECUTE THE PROGRAM. 4. ALL MODIFICATIONS MUST BE DONE IN THE SAME ORIGINAL CHAR ARRAY WITHOUT CREATING A NEW ONE. Write a C++ program that modifies a null teminated char array as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example...
1. In terms of security, why would you not use rm command to delete files? 2....
1. In terms of security, why would you not use rm command to delete files? 2. Shred and dd are tools used to securely delete files. Which is the best tool for securely deleting a file from a system and why? Consider the limitations of each tool when choosing your answer. 3. What encryption is used to encrypt HTTP traffic (HTTPS)? 4. The CIA triad , confidentiality, integrity, and availability represent the three goals for cybersecurity. Which goal(s) does/do encryption...
Answer either Question 1 OR Question 2 (Answer ONE question Only.) you must use hire purchase...
Answer either Question 1 OR Question 2 (Answer ONE question Only.) you must use hire purchase act. Question 1 On 3.3.2019 Salleh visited a used car showroom as he is interested to buy a second-hand car. The salesman, Ismail, recommended to him the Proton Perdana motor car, and informed him that the car was in tip top condition. Salleh then agree to enter into hire purchase agreement with XYZ Finance Bhd. for the hire purchase of a second-hand Proton Perdana....
Solve the following problem using the simplex method. If the problem is two dimensional, graph the...
Solve the following problem using the simplex method. If the problem is two dimensional, graph the feasible region, and outline the progress of the algorithm. Max               Z = 5X1 + 3X2 + 2X3 Subject to    4X1 + 5X2 + 2X3 + X4≤ 20                      3X1 + 4X2 - X3 + X4≤ 30                       X1, X2, X3, X4 ≥ 0   
Must solve in R code Question 2 The random variable X is a payout table of...
Must solve in R code Question 2 The random variable X is a payout table of a casino slot machine. The probability mass function is given: X   -5   0   2   10   20   40   60   1000 Probability   0.65   0.159   0.10   0.05   0.02   0.01   0.01   0.001 Please simulate the results for playing the slot machine 10,000,000 times. Find the mean of these 10,000,000 simulated payout outcomes .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT