How can I modify my program to use dynamic array in which theprogram asks how ma
ID: 3617595 • Letter: H
Question
How can I modify my program to use dynamic array in which theprogram asks how many rows the plane has, and will handle that manyrows instead of assuming that the plane has 7 rows that I currentlyhave in the program. I tried several ways but kept on gettingerrors. Here's the program:#include <iostream> #include <cstdlib> #include <cstddef>
const int NO_ROWS = 7; typedef char SeatRow[4]; typedef SeatRow Seating[NO_ROWS]; Seating seatAssignment;
int main()
{ using namespace std; Seating plan; int row; char seat; int seatNo; int r; int s;
//Empty seats get initialized for(r = 0; r < NO_ROWS; r++) for(s = 0; s < 4; s++) plan[r][s] = char ('A' + s); cout << "Airlines Seat ReservationProgram. " << "Reserved seats are marked'X'. Others seats are available. ";
//displats seating for (r = 1; r < NO_ROWS; r++) { for (s = 0; s < 4; s++) { cout << plan[r] [s] <<""; if(s ==1) cout << " "; } cout << endl; } char ans = 'a'; while (ans !='q' && ans !='Q') { cout << "Airlines " << "Seat ReservationProgram. " << "Reserved Seats are marked 'X'.Others seats are available. "; cout <<"Please enter your request in theform "3 C" for Row 3, SeatC "; cout<< "There are " << NO_ROWS<< " rows. Seats are A, B, C, D. "; cin >> row >> seat; seatNo = seat - 'A'; if('X' != plan[row] [seatNo]) plan[row][seatNo] = 'X'; else cout << "****That seat is alreadytaken****. ****No assignment made****. ";
//displays current seating plan for(r = 1; r < NO_ROWS; r++) { for (s = 0; s < 4; s++) { cout << plan[r][s] << ""; if(s == 1) cout << " "; } cout << endl; } cout << "Please enter Q or q to quit, pressany other key to continues "; cin >> ans; } return 0; } Here's the program:
#include <iostream> #include <cstdlib> #include <cstddef>
const int NO_ROWS = 7; typedef char SeatRow[4]; typedef SeatRow Seating[NO_ROWS]; Seating seatAssignment;
int main()
{ using namespace std; Seating plan; int row; char seat; int seatNo; int r; int s;
//Empty seats get initialized for(r = 0; r < NO_ROWS; r++) for(s = 0; s < 4; s++) plan[r][s] = char ('A' + s); cout << "Airlines Seat ReservationProgram. " << "Reserved seats are marked'X'. Others seats are available. ";
//displats seating for (r = 1; r < NO_ROWS; r++) { for (s = 0; s < 4; s++) { cout << plan[r] [s] <<""; if(s ==1) cout << " "; } cout << endl; } char ans = 'a'; while (ans !='q' && ans !='Q') { cout << "Airlines " << "Seat ReservationProgram. " << "Reserved Seats are marked 'X'.Others seats are available. "; cout <<"Please enter your request in theform "3 C" for Row 3, SeatC "; cout<< "There are " << NO_ROWS<< " rows. Seats are A, B, C, D. "; cin >> row >> seat; seatNo = seat - 'A'; if('X' != plan[row] [seatNo]) plan[row][seatNo] = 'X'; else cout << "****That seat is alreadytaken****. ****No assignment made****. ";
//displays current seating plan for(r = 1; r < NO_ROWS; r++) { for (s = 0; s < 4; s++) { cout << plan[r][s] << ""; if(s == 1) cout << " "; } cout << endl; } cout << "Please enter Q or q to quit, pressany other key to continues "; cin >> ans; } return 0; } #include <iostream> #include <cstdlib> #include <cstddef>
const int NO_ROWS = 7; typedef char SeatRow[4]; typedef SeatRow Seating[NO_ROWS]; Seating seatAssignment;
int main()
{ using namespace std; Seating plan; int row; char seat; int seatNo; int r; int s;
//Empty seats get initialized for(r = 0; r < NO_ROWS; r++) for(s = 0; s < 4; s++) plan[r][s] = char ('A' + s); cout << "Airlines Seat ReservationProgram. " << "Reserved seats are marked'X'. Others seats are available. ";
//displats seating for (r = 1; r < NO_ROWS; r++) { for (s = 0; s < 4; s++) { cout << plan[r] [s] <<""; if(s ==1) cout << " "; } cout << endl; } char ans = 'a'; while (ans !='q' && ans !='Q') { cout << "Airlines " << "Seat ReservationProgram. " << "Reserved Seats are marked 'X'.Others seats are available. "; cout <<"Please enter your request in theform "3 C" for Row 3, SeatC "; cout<< "There are " << NO_ROWS<< " rows. Seats are A, B, C, D. "; cin >> row >> seat; seatNo = seat - 'A'; if('X' != plan[row] [seatNo]) plan[row][seatNo] = 'X'; else cout << "****That seat is alreadytaken****. ****No assignment made****. ";
//displays current seating plan for(r = 1; r < NO_ROWS; r++) { for (s = 0; s < 4; s++) { cout << plan[r][s] << ""; if(s == 1) cout << " "; } cout << endl; } cout << "Please enter Q or q to quit, pressany other key to continues "; cin >> ans; } return 0; }
Explanation / Answer
I'm not too familiar with C++ but in C it sounds like alinked-list would be your best data structure. Using C++, I thinkyou should look into the Vector class provided in the StandardTemplate Library. The Vector class has built in methods to insert,delete, etc. and basically makes a static structure dynamic. CheckWikipedia for some examples.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.