Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 5
Reputation: neilyan is an unknown quantity at this point 
Solved Threads: 0
neilyan neilyan is offline Offline
Newbie Poster

Arrays

 
0
  #1
Nov 11th, 2007
ok my teacher wants me to write a program using these instructions :

A small airline has just purshased a computer for its new automated reserations system. You have been asked to program the new system. You are to write a program to assign seats on each flight of the airline's only plane (capacity: 10 seats). Your program should display the following menu alternatives--Please type 1 for "First Class" and PLease type 2 for "Economy". If the persons types 1, YOur program should assign a seat in the first class section (seat 1-5). If the person types 2, Your program should assign a seat in the economy section (seats 6-10). Your program should print a boarding pass indicating the person's seat number and whether it is in the first class or economy section of the plane.
Use one-dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to 0 to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to 1 to indicate that the seat is no longer available. Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, Your program should ask the person if it is acceptable to be placed in the economy section(and viceversa). If yes, then make the appropiate seat assignment. If no, the print the message "Next flight leaes in 3 hours".

this is what i have so far and i need to know what can i do assign each section a seat, but individually, i don't want it to print all together, i just want to ask do yo want first class or economy depending on the answer i assign the corresponding seat can someone help me pleaseee what is im doing wrong


//Airplane Reservation System

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;


int main()
{
int firstClass = 0;//first class section
int economy = 0;//economy section
int sections= 0;//to choose the section of the seats
int total = 0;
int sectionsCounter = 0;
//use an array for the capacity of 10 seats
int seats[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//Menu Alternatives



cout<<"Please type 1 for ""First Class"":"<<"\n"<<endl;
cout<<"Please type 2 for ""Economy"":"<<endl;
cin>>sections;



//use an if statement to assign a seat in first class
if ( sections == 1)
{
sections = 1;
cout<<"First Class"<<setw (13)<<"Seats"<<endl;

for ( int i = 0; i <= 4; i++)

cout<<setw(7)<<sections<<setw (13)<<seats[i]<<endl;
}//end of if

//use a if statement to assign a seat in economy section
else
if ( sections == 2)
{
sections = 1;
cout<<"Economy"<<setw (13)<<"Seats"<<endl;
for ( int i = 5; i < 10; i++)

cout<<setw(7)<<sections<<setw (13)<<seats[i]<<endl;
}//end of if
system("pause");
return 0;
}//end of main
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,707
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 275
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Arrays

 
0
  #2
Nov 12th, 2007
You mean something like this:
  1. if ( sections == 1) //first class reqeusted
  2. {
  3. if(firstClass < 4)//implies there's only 4 first class seats available on this flight
  4. ++firstClass; //assign a seat in first class
  5. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 5
Reputation: neilyan is an unknown quantity at this point 
Solved Threads: 0
neilyan neilyan is offline Offline
Newbie Poster

Re: Arrays

 
0
  #3
Nov 12th, 2007
no, what i mean is that the program should print First Class or Economy 0 until the user inputs the number of section, and when the user choose wich section they want, the program should assign a seat for each section, if it is first class it has to be in the seats from 1-5 and if it is economy it has to be in the seats from 6-10
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: helixkod is an unknown quantity at this point 
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

Re: Arrays

 
0
  #4
Nov 12th, 2007
If you want it to print out 0's then set the array to 0's

  1. int seats[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
with this you're setting each array with it's respective number.

also as a heads up if you're looking for this to loop until all the seats are full you need to set a lop before the menu starts to go back to the menu once the user is done making his selections.

  1. //use an if statement to assign a seat in first class
  2. if ( sections == 1)
  3. {
  4. sections = 1;
  5. cout<<"First Class"<<setw (13)<<"Seats"<<endl;
  6.  
  7. for ( int i = 0; i <= 4; i++)
  8.  
  9. cout<<setw(7)<<sections<<setw (13)<<seats[i]<<endl;
  10. }//end of if

with this you're only printing out the numbers between seats[0] to seats[4]. first thing first take out the sections = 1; its doing nothing. Then find a way to change seats[] to the value 1 once they selected either 1 or 2 for their seating. I would do something like
int i =0;
while (i < 4 && seats[i] ==1)
i++;
seats[i] = 1;

Hope this helps out.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,707
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 275
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Arrays

 
0
  #5
Nov 12th, 2007
Use firstClass and and economy to calculate indexes into the array of seats which represent the first available seat in each section. Here's something that might guide you.
  1. if(sections == 1) //customer wants first class
  2. {
  3. if(firstClass < 5)/*there's only 5 seats in first class, they are seats[0], seats[1], seats[2], seats[3], and seats[4]*/
  4. cout << "you will be assigned seat " << seats[firstClass];
  5. /*now there's one less seat class in first class
  6. so increment firstClass so you don't give the seat out twice*/
  7. ++firstClass;
  8. }
  9. if(sections == 2) //they want economy.
  10. /*the first open seat in economy will be at index i == 5 + economy. You can write the code notifying the customer what seat they were assigned and keeping track of the next available seat in economy */
Last edited by Lerner; Nov 12th, 2007 at 2:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 5
Reputation: neilyan is an unknown quantity at this point 
Solved Threads: 0
neilyan neilyan is offline Offline
Newbie Poster

Re: Arrays

 
0
  #6
Nov 14th, 2007
Thanks for all your help it was very helpful
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC