Question

I need help fixing this code and adding a loop please.

Here is the original problem:

A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: 10 10 10 10 10 10 10 10 10 10


#include <iostream>

using namespace std;

//function to print seating chart
void printSeatingChart(int **chart)
{

   int i, j;

   cout << "\nROW\t";
   for (i = 1; i <= 10; i++)
   {
       cout << i << "\t";
   }
   cout << "\n-----------------------------------------------------------------------------------\n";
   for (i = 8; i >= 0; i--)
   {

       cout << "\n" << i + 1 << "\t";
       for (j = 0; j < 10; j++)
       {
           cout << chart[i][j] << "\t";
       }
       cout << endl << endl;
   }
}

//function to book the seat based on position
void seat(int **chart, int i, int j)
{
   //if given position is not booked
   if (chart[i][j] != 0)
   {
       chart[i][j] = 0;
       printSeatingChart(chart);
       cout << "Seat is booked..\nThank you." << endl;
   }
   //already booked
   else
   {
       cout << "Seat is already sold.." << endl;
   }
}
//function to book the seat based on price
void price(int **chart, int price)
{

   int i, j, flag = 0;
   for (i = 9; i >= 0; i--)
   {
       for (j = 0; j < 10; j++)
       {
           if (chart[i][j] == price)
           {
               flag = 1;
               chart[i][j] = 0;
               printSeatingChart(chart);
               cout << "Seat is booked..\nrow: " << i + 1 << "\ncol: " << j + 1 << "\nThank you." << endl;
               break;
           }
       }
       if (flag == 1)
           break;
   }

   if (flag == 0)
   {
       cout << "Seat is already sold.." << endl;
   }


}
//initializing the chart
void initializeChart(int **chart, int ticket[10][10])
{

   int i, j;
   for (i = 9; i >= 0; i--)
   {

       for (j = 0; j < 10; j++)
       {
           chart[i][j] = ticket[i][j];
       }
   }

}

int main()
{

   int i;

   //dynamic array to store the seating chart
   int **ticketPrices = new int*[10];
   for (i = 0; i < 10; i++)
       ticketPrices[i] = new int[10];

   //values of the ticket prices
   int ticket[9][10] =
   {
       {30,40,50,50,50,50,50,50,40,30},
       {20,30,30,40,50,50,40,30,30,20},
       {20,20,30,30,40,40,30,30,20,20},
       {10,10,20,20,20,20,20,20,10,10},
       {10,10,20,20,20,20,20,20,10,10},
       {10,10,20,20,20,20,20,20,10,10},
       {10,10,10,10,10,10,10,10,10,10},
       {10,10,10,10,10,10,10,10,10,10},
       {10,10,10,10,10,10,10,10,10,10}
   };


   //initializing the ticketPrices array
   initializeChart(ticketPrices, ticket);

   //printing the chart
   printSeatingChart(ticketPrices);

   //taking the input from the user
   int choice;
   cout << "Pick either seat(1) or price(0): ";
   cin >> choice;

   //user picks seat
   if (choice == 1) {
       int row, col;
       cout << "Enter row: ";
       cin >> row;
       cout << "Enter column: ";
       cin >> col;

       if (row <= 9 && col <= 10)
           seat(ticketPrices, row - 1, col - 1);//calling seat function
       else
           cout << "Enter valid seat positon.." << endl;
   }
   //user picks price
   else if (choice == 0)
   {
       int amount;
       cout << "Enter price: ";
       cin >> amount;
       if (amount > 0)
           price(ticketPrices, amount);//calling price function
       else
           cout << "Enter valid price: " << endl;
   }
   else
   {
       cout << "Enter a valid input..." << endl;
   }


   system("pause");
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Modified C++ program

#include <iostream>
#include<iomanip>
using namespace std;

//function to print seating chart
void printSeatingChart(int **chart)
{

int i, j;

cout << "\nROW ";
for (i = 1; i <= 10; i++)
{
cout << setw(2)<<i << " ";
}
cout << "\n-----------------------------------------------\n";
for (i = 8; i >= 0; i--)
{

cout << "\n" << i + 1 << " ";
for (j = 0; j < 10; j++)
{
cout << setw(2)<<chart[i][j] << " ";
}
cout << endl << endl;
}
}

//function to book the seat based on position
void seat(int **chart, int i, int j)
{
//if given position is not booked
if (chart[i][j] != 0)
{
chart[i][j] = 0;
printSeatingChart(chart);
cout << "Seat is booked..\nThank you." << endl;
}
//already booked
else
{
cout << "Seat is already sold.." << endl;
}
}
//function to book the seat based on price
void price(int **chart, int price)
{

int i, j, flag = 0;
for (i = 8; i >= 0; i--)
{
for (j = 0; j < 10; j++)
{
if (chart[i][j] == price )
{ flag=1;
chart[i][j] = 0;
printSeatingChart(chart);
cout << "Seat is booked..\nrow: " << i + 1 << "\ncol: " << j + 1 << "\nThank you." << endl;
break;
}
}
if (flag == 1)
break;
}

if (flag == 0)
{
cout << "Seat is already sold.." << endl;
}


}
//initializing the chart
void initializeChart(int **chart, int ticket[10][10])
{

int i, j;
for (i = 8; i >= 0; i--)
{

for (j = 0; j < 10; j++)
{
chart[i][j] = ticket[i][j];
}
}

}

int main()
{

int i;
char more;
//dynamic array to store the seating chart
int **ticketPrices = new int*[10];
for (i = 0; i < 10; i++)
ticketPrices[i] = new int[10];

//values of the ticket prices
int ticket[9][10] =
{
{30,40,50,50,50,50,50,50,40,30},
{20,30,30,40,50,50,40,30,30,20},
{20,20,30,30,40,40,30,30,20,20},
{10,10,20,20,20,20,20,20,10,10},
{10,10,20,20,20,20,20,20,10,10},
{10,10,20,20,20,20,20,20,10,10},
{10,10,10,10,10,10,10,10,10,10},
{10,10,10,10,10,10,10,10,10,10},
{10,10,10,10,10,10,10,10,10,10}
};


//initializing the ticketPrices array
initializeChart(ticketPrices, ticket);

//printing the chart
printSeatingChart(ticketPrices);

//taking the input from the user
int choice;
do{
    cout << "Pick either seat(1) or price(0): ";
cin >> choice;

//user picks seat
if (choice == 1) {
int row, col;
cout << "Enter row: ";
cin >> row;
cout << "Enter column: ";
cin >> col;

if (row <= 9 && col <= 10)
seat(ticketPrices, row - 1, col - 1);//calling seat function
else
cout << "Enter valid seat positon.." << endl;
}
//user picks price
else if (choice == 0)
{
int amount;
cout << "Enter price: ";
cin >> amount;
if (amount > 0)
price(ticketPrices, amount);//calling price function
else
cout << "Enter valid price: " << endl;
}
else
{
cout << "Enter a valid input..." << endl;
}

cout<<"Do you want to book more seats? (y/n) : ";
cin>>more;
}while(more=='y'||more=='Y');


system("pause");
return 0;
}

//sample output

CAUsers ishuManish\ Documents seatbook.exe ROW 1 2 3 4 5 6 7 89 10 10 10 10 10 10 10 10 10 10 10 8 10 10 10 10 10 10 10 10 10CUsers IshuManish\ Documents seatbook.exe 20 20 30 30 40 40 30 30 20 20 20 30 30 40 50 5 40 30 30 20 30 40 50 50 50 50 50 50

Add a comment
Know the answer?
Add Answer to:
I need help fixing this code and adding a loop please. Here is the original problem: #include <iostream> using namespace std; //function to print seating chart void printSeatingChart(int **cha...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int...

    #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int lab[4]; myPtr = new int *[row]; for(int i = 0; i < row; i++) myPtr[i] = new int[lab[i]]; for(int i = 0; i<row ; i++) if(myPtr[i] == 0) cout<<"empty"; return myPtr; } void getinput(int ID,int &Station,int &labnumb) { cout<<" Enter your ID number: "<<endl; cin>>ID; cout<<" Enter your station number: "<<endl; cin>>Station; cout<<" Enter your lab number: "<<endl; cin>>labnumb; return; } void logout(int ID,int...

  • Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function...

    Need a FLOW Chart for that code. #include <iostream > using namespace std; int PowerFive(int); //Function prototype declaration int main() for (int i=-10 ; i(z10; 1++) {//for each # cout<<"("<< ǐ<<") ^5. "<<PowerFive(1)くくendl;// calling the defined Function int PowerFive (int a) //Function definition return a*a*a*a*a;

  • #include <iostream> using namespace std; struct node { int base; int power; }; void insert(node ptr[],int...

    #include <iostream> using namespace std; struct node { int base; int power; }; void insert(node ptr[],int basee,int powerr) { ptr[powerr].power=powerr; ptr[powerr].base=basee; } void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[],int size3) { for(int j=0;j<=size1;j++) { ptr3[j].base=ptr3[j].base+ptr1[j].base; } for(int j=0;j<=size2;j++) { ptr3[j].base=ptr3[j].base+ptr2[j].base; } } void display(node ptr[],int size) { if(ptr[0].base!=0) cout<<ptr[0].base<<"+"; for(int i=1; i<=size; i++) { if(ptr[i].base!=0) cout<<ptr[i].base<<"x^"<<ptr[i].power<<"+"; } } int main() { bool choice1=true; bool choice2=true; int size1,size2,base1,base2,power1,power2; cout<<"enter the max power in polynominal 1"; cin>>size1; node *a= new node[size1+1]; for(int...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0,...

    Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...

  • #include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int...

    #include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int basee,int powerr) { ptr[powerr].power=powerr; ptr[powerr].base=basee; } void subtract(node ptr1[],int size1,node ptr2[],int size2,node ptr3[]) { for(int j=0;j<=size1;j++) { if(ptr1[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr1[j].base); ptr3[j].power=ptr2[j].power; } } for(int j=0;j<=size2;j++) { if(ptr2[j].base!=0) { ptr3[j].base=(ptr3[j].base)-(ptr2[j].base); ptr3[j].power=ptr2[j].power; } } } void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[]) { for(int j=0;j<=size1;j++) { if(ptr1[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr1[j].base); ptr3[j].power=ptr2[j].power; } } for(int j=0;j<=size2;j++) { if(ptr2[j].base!=0) { ptr3[j].base=(ptr3[j].base)+(ptr2[j].base); ptr3[j].power=ptr2[j].power; } } } void display(node ptr[],int size)...

  • #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating...

    #include <iostream> using namespace std; int main() {    int sumOdd = 0; // For accumulating odd numbers, init to 0    int sumEven = 0; // For accumulating even numbers, init to 0    int upperbound; // Sum from 1 to this upperbound    // Prompt user for an upperbound    cout << "Enter the upperbound: ";    cin >> upperbound;    // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound    int number =...

  • Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() {...

    Watermelon Problem: The answer should not include any whitespace. #include<iostream> using namespace std; int main() {     double distance, gravity =9.8, height;     int time, t=0;     cout<<"Please input the time of fall in seconds:"<<endl;     [ A ] // Get the user input of the time of fall in seconds (Use cin>> method)     cout<<endl;     cout<<"Please input the height of the bridge in meters:"<<endl;     [ B ] // Get the user input of the height of the bridge in meters (Use cin>>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT