Hello, I have the code for the most part written, however I am not sure on passi
ID: 3623695 • Letter: H
Question
Hello, I have the code for the most part written, however I am not sure on passing the two dimension array through functions. Can someone help me break the code up in functions?#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int const NUMBER_ROWS = 13;
int const NUMBER_SEATS = 6;
int row;
int col;
int i;
int j;
char ticket;
char seat;
char choice;
char plane[NUMBER_ROWS][NUMBER_SEATS];
for ( i = 0; i < NUMBER_ROWS; i++ )
for (j = 0; j < NUMBER_SEATS; j++ )
plane[i][j] = '*';
cout << "Welcome to the Plane Seat Selection Program!" << endl;
cout << endl;
cout << "Would you like to make a selection? (Y/N)" << endl;
cin >> choice;
choice = static_cast<char>(toupper(choice));
do
{
choice = static_cast<char>(toupper(choice));
cout << setw(11)<< "A" << setw(5) << "B"
<< setw(5) << "C" << setw(5) << "D"
<< setw(5) << "E" << setw(5) << "F" << endl;
cout << " " << endl;
for ( i = 0; i < NUMBER_ROWS; i++ )
{
cout <<"Row " << setw(2)<< i + 1;
for ( j = 0; j < NUMBER_SEATS; j++ )
cout << setw(5)<< plane [i][j];
cout << endl;
}
cout << " " << endl;
cout << "Please Enter the type of Seat desired: " << endl;
cout << "F = First Class " << ' ' << "B = Business Class "
<< ' ' << "E = Economy Class" << endl;
cin >> ticket;
cout << "Please enter in the desired Row Number (1-13): " << endl;
cin >> row;
cout << "Please enter in the desired Seat (1-6): " << endl;
cin >> seat;
col = static_cast<int>(tolower(seat) - 97);
if (( ticket == 'F') && ( row > 2 ))
{
cout << "Invalid Selction for First Class" << endl;
continue;
}
else if (( ticket == 'B' ) && (( row <= 2 ) || ( row > 7 )))
{
cout << "Invalid Selection for Business Class" << endl;
continue;
}
else if (( ticket == 'E' ) && (( row < 7 ) || ( row > 13 )))
{
cout << "Invalid Selection for Economy Class" << endl;
continue;
}
if ( plane[row -1][col] != 'X')
{
plane[row -1][col] = 'X';
}
else
{
cout<<"Seat taken!"<<endl;
}
} while ( choice != 'N');
return 0;
}
Explanation / Answer
please rate - thanks
should get you started
#include <iostream>
#include <string>
#include <iomanip>
#define NUMBER_ROWS 13
#define NUMBER_SEATS 6
using namespace std;
void init(char[][NUMBER_SEATS ]);
void printPlane(char[][NUMBER_SEATS ]);
bool getSeatNum(char[][NUMBER_SEATS ],int&,int&);
void checkSeat(char[][NUMBER_SEATS ],int,int);
char getSeatYN();
int main()
{
int row;
int col;
int i;
int j;
bool goodseat;
char choice;
char plane[NUMBER_ROWS][NUMBER_SEATS];
init(plane);
cout << "Welcome to the Plane Seat Selection Program!" << endl;
cout << endl;
choice=getSeatYN();
while ( choice != 'N')
{
printPlane(plane);
goodseat=getSeatNum(plane,row,col);
if(goodseat)
checkSeat(plane,row,col);
choice=getSeatYN();
}
return 0;
}
void checkSeat(char plane[][NUMBER_SEATS ],int row,int col)
{if ( plane[row -1][col] != 'X')
{
plane[row -1][col] = 'X';
}
else
{
cout<<"Seat taken!"<<endl;
}
}
void init(char plane[][NUMBER_SEATS])
{int i,j;
for ( i = 0; i < NUMBER_ROWS; i++ )
for (j = 0; j < NUMBER_SEATS; j++ )
plane[i][j] = '*';
}
bool getSeatNum(char[][NUMBER_SEATS ],int& row,int& col)
{char ticket;
char seat;
int min,max;
bool good=false;
cout << "Please Enter the type of Seat desired: " << endl;
cout << "F = First Class " << ' ' << "B = Business Class "
<< ' ' << "E = Economy Class" << endl;
cin >> ticket;
ticket=toupper(ticket);
switch(ticket)
{
case 'F':min=1;
max=2;
break;
case 'E':
min=8;
max=13;
break;
case 'B': min=3;
max=7;
}
cout << "Please enter in the desired Row Number ("<<min<<"-"<<max<<"): " << endl;
cin >> row;
cout << "Please enter in the desired Seat (A-F): " << endl;
cin >> seat;
col = tolower(seat) - 'a';
if(col<0||col>5)
cout<<"Invalid Seat ";
else if (( ticket == 'F') && ( row > 2 )||row<1)
cout << "Invalid Selction for First Class" << endl;
else if (( ticket == 'B' ) && (( row <= 2 ) || ( row > 7 )))
cout << "Invalid Selection for Business Class" << endl;
else if (( ticket == 'E' ) && (( row < 8 ) || ( row > 13 )))
cout << "Invalid Selection for Economy Class" << endl;
else
good=true;
return good;
}
char getSeatYN()
{char choice;
cout << "Would you like to make a selection? (Y/N)" << endl;
cin >> choice;
choice = static_cast<char>(toupper(choice));
return choice;
}
void printPlane(char plane[][NUMBER_SEATS ])
{int i,j;
cout << setw(11)<< "A" << setw(5) << "B"
<< setw(5) << "C" << setw(5) << "D"
<< setw(5) << "E" << setw(5) << "F" << endl;
cout << " " << endl;
for ( i = 0; i < NUMBER_ROWS; i++ )
{
cout <<"Row " << setw(2)<< i + 1;
for ( j = 0; j < NUMBER_SEATS; j++ )
cout << setw(5)<< plane [i][j];
cout << endl;
}
cout << " " << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.