• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Need help (use a single-subscripted array to represent the seating chart)

Ok, here is my problem.  How do I program this to assign seat to ten people who enter 1 or 2
It suppose to assign one seat per person. And of course, never assign a seat that has already been assigned.


#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
     const int seat = 10;
     //int a[ seat ];
     int num;
         
     while (seat <= 10) {

     cout <<"Please type 1 for \"First Class\"" << endl;
     cout <<"Please type 2 for \"Economy\"" << endl;
     cin >> num;
     
     if (num==1)
          for (int seat = 1; seat <= 5; seat++) {
               cout <<"Your seat assignment is " << seat << endl;
               for (int person = 1; person <= 10; person++) {
                    cout <<"Person number: " << person << endl;
               }
          }
     else
          for (int seat = 6; seat <= 10; seat++) {
               cout <<"Your seat assignment is " << seat << endl;
               for (int person = 1; person <= 10; person++) {
                    cout <<"Person number: " << person << endl;
               }
          }
     }

     return 0;
}
0
Hvt05
Asked:
Hvt05
  • 4
  • 4
1 Solution
 
n_fortynineCommented:
You need to tell us what your problem is (what the errors are) but there are several I could point out right away. <seat> is a constant so you cannot reassign it for your for-loops. Secondly, C/C++ array indexes start at 0, not 1, so the last index goes to one less than the size, in this case go to 9, not 10. so <= 10 or <= 5 should be < 10 or < 5 etc. Plus it doesn't look like your code is assigning seats at all.

You can simply create an array full of 0's. Then if a person chooses a seat, you put the number of the person in the slot. An empty seat would have a 0, a non-empty one would have a non-0 number. It would involve some basic searching...
0
 
n_fortynineCommented:
or full of -1's...
0
 
SalteCommented:
If it is important to assign the seats so that 1st class people come together in seats 0..K-1 while 2nd class have seats from K..9  then you probably just have to rethink how you assign the seats. It is a very simple problem. I'll give you a hint: An array has two ends. The problem would be a lot more difficult if you had three classes but two classes fit perfectly for an array and has an obvious and simple solution!

Enough hint.

Go and do your homework.

Alf
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Hvt05Author Commented:
I have added some more to my program, but I gets lots of error can anyone debug it for me???????????

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int assignFist (int[], int, int);
int assignEcon (int[], int, int);
void printPass (int, int);

int main()
{
     int classNum;
     const int chartSize = 10;
     const int firstEconomy = 6;
     int seatNum;
     int seatChart [chartSize] = {0,0,0,0,0,0,0,0,0,0};

     while (true) {
         
          cout <<"Please type 1 for \"First Class"\" << endl;
          cout <<"Please type 2 for \"Economy"\" << endl;
          cin >> classNum;
          if (classNum == 1) {
               seatNum = assignFirst (seatChart, chartSize, firstEconomy);
          }
          if (classNum == 2) {
               seatNum = assignEconomy (seatChart, chartSize, first Economy);
          }
          if (seatNum == -1)
               cout <<"Next flight leaves in 3 hours." << endl;
          else
               printPass (seatNum, firstEconomy);
     }
     return 0;
}

int assignFirst (int seatChart[], int chartSize, int firstEconomy) {
     char answer;
     for (int seat = 1; seat < firstEconomy; seat++) {
          if (seatChart[seat-1] == 0) {
               seatChart [seat-1] = 1;
               return seat;
          }
     }
     cout <<"The FirstClass section is full" << endl;
     cout <<"Would you like to sit in the Economy section (y or n)";
     cin >> answer;
     if (answer == 'y' || answer == 'Y') {
          return assignEconomy (seatChart, chartSize, firstEconomy);
     }
     else {
          return -1;
     }
}

int assignEconomy (int seatChart [], int chartSize, int firstEconomy) {
     char answer;
     for (int seat = firstEconomy; seat < chartSize; seat++) {
          seatChart [seat-1] = 0) {
          seatChart [seat-1] = 1;
          return seat;
          }
     }
     cout <<"Would you like to sit in the First Class section (y or n)";
     cin >> answer;
     if (answer == 'y' || answer == 'Y') {
          return assignEconomy (seatChart, chartSize, firstEconomy);
     }
     else {
          return -1;
     }
}

void printPass (int seat, int firstEconomy) {
     if (seat < firstEconomy)
          cout <<"Your seat assignment is << seat <<"You are in First Class" << endl;
     else
          cout <<"Your seat assignment is << seat <<"You are in Economy" << endl;
}
0
 
Hvt05Author Commented:
I have debug the program and end up with the last 4 errors.  I cannot find these errors, can someone debug it for me.

Here it is

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int FirstClass (int[], int, int);
int Economy (int[], int, int);
void Indication (int, int);

int main()
{
    int num, seat;
    const int Seat = 10;
    int assignSeat [Seat] = {0,0,0,0,0,0,0,0,0,0};

    while (true) {
         
         cout <<"Please type 1 for First Class" << endl;
         cout <<"Please type 2 for Economy" << endl;
         cin >> num;
     
         if (num == 1) {
              seat = FirstClass (assignSeat, Seat, FirstClass);
         }
         if (num == 2) {
              seat = Economy (assignSeat, Seat, Economy);
         }
         if (seat == -1)
              cout <<"Next flight leaves in 3 hours." << endl;
         else
              Indication (seat, Economy);
    }
    return 0;
}

int FirstClass (int assignSeat[], int Seat, int FirstClass) {
    char answer;
    for (int seat = 1; seat <= 5; seat++) {
         if (assignSeat [seat-1] == 0) {
             assignSeat [seat-1] = 1;
              return seat;
         }
    }

    cout <<"The First Class section is full" << endl;
    cout <<"Would you like to sit in the Economy section (y or n)";
    cin >> answer;
   
     if (answer == 'y' || answer == 'Y') {
         return Economy (assignSeat, Seat, Economy);
    }
    else {
         return -1;
    }
}

int Economy (int assignSeat [], int Seat, int Economy) {
    char answer;
    for (int seat = 6; seat <= 10; seat++) {
         assignSeat [seat-1] = 0; {
         assignSeat [seat-1] = 1;
         return seat;
         }
    }
    cout <<"Next flight leaves in 3 hours" << endl;
}


void Indication (int seat, int Economy) {
    if (seat < Economy)
         cout <<"Your seat assignment is " << seat <<
           "\nYou are in First Class section" << endl;
    else
         cout <<"Your seat assignment is " << seat <<
           "\nYou are in Economy section" << endl;
}  
0
 
Hvt05Author Commented:
Can someone find these errors for me, there are only two but I cannot fixed it.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int assignFirst (int, int);
int assignEconomy (int, int);

int main()
{
    int num, seat = 0, person = 0;
    const int SizeA = 5;
    const int SizeB = 5;

 int A[SizeA] = {0};
 int B[SizeB] = {0};

 while (seat <= 10) {
     cout <<"Please type 1 for First Class" << endl;
    cout <<"Please type 2 for Economy" << endl;
    cin >> num;
 
  if (num == 1) {
   assignFirst (seat, SizeA);
   person++;
   cout <<"You are in First Class section " << endl;
   cout <<"Your seat assignment is " << seat << endl;
   cout <<"Person number " << person << endl;
  }
  else {
   assignEconomy (seat, SizeB);
   person++;
   cout <<"You are in Economy section " << endl;
   cout <<"Your seat assignment is " << seat << endl;
   cout <<"Person number " << person << endl;
  }
 }

 while (seat == 5) {
  char answer;
  cout <<"Would you like to sit in the Economy section (y or n) ";
  cin >> answer;

  if (answer == 'y' || answer == 'Y') {
   assignEconomy (seat, SizeB);
  }
  else {
   cout <<"Next flight leaves in 3 hours. " << endl;
  }
 }
 
 if (seat == 10 ) {
  cout <<"All seats of this flight are sold out " << endl;
 }

 return 0;
}


int assignFirst (int seat, int Size) {
     const int SizeA = 5;
     int A[SizeA] = {0};
    for (int seat = 0; seat < Size; seat++) {
         A[SizeA] = seat + 1;
    }
}
     
   
   

int assignEconomy (int seat, int Size) {
     const int SizeB = 5;
     int B[SizeB] = {0};
    for (int seat = 0; seat < Size; seat++) {
         B[SizeB] = seat + 6;
    }
}
0
 
n_fortynineCommented:
This one should do the minimum job for you.
#include <iostream.h>

void assignSeat(int seats[], int person, int& next_seat_avail) {
   seats[next_seat_avail] = person;
   ++next_seat_avail;
}

int main() {
   int const max_seats = 10;
   int person = 0,
       eclass = 0,
       fclass = 5,
       choice,
       seats[max_seats];

   while(person < max_seats) {
      cout << "1: First Class\n2: Economy Class\nYour Choice: ";
      cin >> choice;

      if(choice == 1)
         if(fclass == max_seats) {
            cout << "FC not available. Trying to book you for an EC...\n";
            choice = 2;
         }
         else {
            cout << "Your seat # " << fclass << ". And you are customer # " << ++person << "\n";
            assignSeat(seats, person, fclass);
         }

      if(choice == 2) {
         cout << "Your seat # " << eclass << ". And you are customer # " << ++person << "\n";
         assignSeat(seats, person, eclass);
      }
      else if(choice != 1)
         cout << "Invalid choice!\n";
   }

   cout << "This flight is full...\nThe seating is as followed: ";
   for(int i(0); i < max_seats; cout << seats[i++] << " ");
}

What you did wrong in your code is in the 2 functions. You want to pass in the array representing the seats to modify it. What you did was re-created the arrays inside the functions, it's not good because they are local to the functions and will be destroyed after the function call ends.
0
 
Hvt05Author Commented:
Ok, I have modify the program you gave me.  But when executed it is not what I expected.
the program suppose to terminated when person #10 is enter. (FirstClass have seat 1 - 5, Econ have 6-10). If you test it by enter 1's for the first five time when it ask to choose, then u know what i mean.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void assignSeat(int seats[], int person, int& next_seat_avail) {
  seats[next_seat_avail] = person;
  ++next_seat_avail;
}

int main() {
 
  int const max_seats = 10;
  char answer;
  int person = 1,
      eclass = 5,
      fclass = 0,
      choice,
      seats[max_seats];

  while (person <= max_seats) {
     
      cout <<"Please enter 1 for First Class" << endl;
      cout <<"Please enter 2 for Economy" << endl;
     cin >> choice;
      cout << endl;
     
      if (choice == 1) {
          assignSeat(seats, person, fclass);
             cout <<"You are in the First Class section " << endl;
           cout <<"Your seat assignment is " << fclass << endl;
             cout <<"Person number: " << person++ << endl;
             cout << endl;
             }
     
      else {
          assignSeat(seats, person, eclass);
               cout <<"You are in the Economy section " << endl;
               cout <<"Your seat assignment is " << eclass << endl;
               cout <<"Person number: " << person++ << endl;
               cout << endl;
      }

     if (fclass == 5) {
           cout <<"The First Class section is full." << endl;
             cout <<"Would you like to sit in the Economy section (y or n): ";
           cin >> answer;
             
             if (answer == 'y' || answer == 'Y') {
                  assignSeat(seats, person, eclass);
             }
             else {
                  cout <<"Next flight leaves in 3 hours. " << endl;
             }
     }

     if (person == max_seats) {
               cout << "All seats for this flight are sold out" << endl;
     }
  }

  return 0;
}
0
 
n_fortynineCommented:
if you're going to arrange it like this why don't you put the if(fclass == 5) {} part before the choice checking, and make this choice checking an else that follows it, if you know what i mean. try that and tell me what it gives you that is so unexpected.

another easier path i can think of is to modify this part in my original example:
>> if(fclass == max_seats) {
>>           cout << "FC not available. Trying to book you for an EC...\n";
>>           choice = 2;
>>        }
into
if(fclass == max_seats) {
   cout << "FC not available. Would you like an EC seat? ";
   //prompt if answer is yes, set choice = 2 (else do nothing)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now