?
Solved

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

Posted on 2003-03-16
9
Medium Priority
?
395 Views
Last Modified: 2007-12-19
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
Comment
Question by:Hvt05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8149488
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8149489
or full of -1's...
0
 
LVL 12

Expert Comment

by:Salte
ID: 8150836
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Hvt05
ID: 8156849
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
 

Author Comment

by:Hvt05
ID: 8162580
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
 

Author Comment

by:Hvt05
ID: 8171158
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8171276
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
 

Author Comment

by:Hvt05
ID: 8171945
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
 
LVL 4

Accepted Solution

by:
n_fortynine earned 200 total points
ID: 8172420
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question