C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 12th, 2003, 03:56 AM
BooRadley BooRadley is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 1 BooRadley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Problems with IF Statement and Arrays

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.

Reply With Quote
  #2  
Old February 12th, 2003, 08:38 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
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.

Reply With Quote
  #3  
Old February 12th, 2003, 01:49 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #4  
Old February 12th, 2003, 11:48 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problems with IF Statement and Arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway