|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Hello everyone;
I am a beginner at programming C++. It seems pretty simple, until arrays start kicking in. I'm having problems with some syntax, and breaking a problem down into logical units in order to tackle it. My first assignment in class is this: making a computer airline reservation systems for a plane of a capacity of 10 seats. Display: Please type 1 for First Class Please type 2 for Economy Class (if the person types 1, assign a seat in first class section -seats 1-5. if the person types 2, your program should assign a seat in economy class - seats 6-10). I got this much so far: #include<iostream.h>#include<iostream.h> #include<conio.h> void main() { int response; cout<<"Please type 1 for First Class\n"; cout<<"Please type 2 for Economy Class. "; cin>>response; while (response != 1 && response !=2) { cout<<"Please try again. Type a 1 for First Class "; cout<<"or 2 for Economy Class. "; cin>>response; } } Im having problems with the array(s) of this problem because I can't figure out how to align the First Class and Economy Seating with the input I generated from the user. Im also supposed to change the value of the seat from a 0 to a 1 when a seat is vacant and given, indicating that the seat is occupied (which just complicates it further for me). I'm too embarressed to post what I got next, because it doesn't make any sense whatsoever. I doubt anyone will respond to this problem, but thanks to anyone who took the time and read it. |
|
#2
|
||||
|
||||
|
try this:
int array[10] = {0}; int a = 0; if (response == 1) { do { if(array[a] == 1) a++; else { array[a] = 1; } } while(array[a] == 0) a++; } I'll leave the rest to u! but that's a start. |
|
#3
|
|||
|
|||
|
Arrays are very easy once you understand them. Try to think of an array as a short hand way of writing a lot of variables. Without arrays you would have to write:
int seat1 = 0; int seat2 = 0; . . . int seat10 = 0; (imagine if you had 300 seats!) With arrays, you can write: int seat[10] = {0}; or int seat[300]={0}; Now suppose some vacation company called your airline and needed the whole plane, so there were no more seats available. You could indicate that like this: for(int i=0; i<10;i++) { seat[i]=1; } or in the case if you had 300 seats: for(int i=0; i<300;i++) { seat[i]=1; } Without arrays, you would have to type in 10 variables in your case(or 300 for a real plane), and then have to type all of them in again every time you needed to examine their values. Now, let's suppose your customers were able to choose any seat assignment they wanted, and now you want to check how many seats are available on your plane. Assuming, a 1 in the array means the seat is taken, and a 0 means it's free, you can do this: int open_seats=0; for(int i=0; i<300;i++) { if(seat[i]==0) open_seats++; } cout<<open_seats; Without arrays you would have to do this: if(seat1==0) open_seats++; if(seat2==0) open_seats++; . . . if(seat300==0) open_seats++; The second method is waaaaaaaaaay too much typing. I hope that helps. Last edited by 7stud : February 12th, 2003 at 08:45 PM. |
|
#4
|
|||
|
|||
|
Did they teach you this?
>>void main() as it is SO WRONG! ( not ISO C compliant) >>if the person types 1, assign a seat in first class section -seats 1-5. if the person types 2, your program should assign a seat in economy class - seats 6-10. This is the array index. Use 0 - 4 and 5 - 9 as arrays start at zero (not 1) Code:
//define the main constant
#define MAX_PASSENGERS 10
//some to make the code more readable
#define EMPTY 0
#define FULL 1
#define FIRST_CLASS 1
#define ECONOMY 2
//array of seats
int iSeats[MAX_PASSENGERS];
int main()
{
int response;
int iPassengers=0,i;
//init array
for(i=0;i<MAX_PASSENGERS;i++)
iSeats[i]=0;//all set to empty
//for each passengers until max
while(iPassengers<MAX_PASSENGERS)
{
//a "do while" loop is used where the code MUST run at least once
do
{
cout<<"Please type 1 for First Class\n";
cout<<"Please type 2 for Economy Class. ";
cin>>response;//no need to prime the loop as we get the input here BEFORE we test the loop exit condition
}
while (response != FIRST_CLASS && response !=ECONOMY)
//now will have the TYPE of seat
iPassenger++;//increase the passengers
//now need to find the first empty seat in the section the user has choosen
if(response==FIRST_CLASS)//first class
{
/ /look for an empty seat
i=0;//init loop counter
while(iSeats[i]==FULL && i<MAX_PASSENGERS/2)//watch for arrays going out of the space you have allocated
i++;//more thru array looking fo an empty seat
//now have either run out of seats or have an empty one
if(i>=MAX_PASSENGERS/2)//no empty seats, WHAT WILL YOU DO?
else //got a seat.
{
iSeat[i]=FULL;
//INFORM USER OF SEAT NUMBER
}
}
//ect
return 1;
}
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions. Frank Zappa |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Problems with IF Statement and Arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|