Assigning and declaring arrays and elements.

Assigning and declaring arrays and eleme

Page 1 of 1

4 Replies - 1869 Views - Last Post: 23 November 2005 - 02:10 PM Rate Topic: -----

#1 Flipmodemvp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-November 05

Assigning and declaring arrays and elements.

Posted 23 November 2005 - 02:17 AM

I was asked to write a program to assign seats on a plane. Their are ten seats on a plane and I must use an array to fill up each seat. The input is "1 for smoking" and "2 for non-smoking." How am i suppose to assign the seats on the plane? Here is what i got:

#include <iostream.h>
#include <cmath>

int main()
{
int i, plane, seat[10];

cout<<"This program will assign you seats in the airplane."<<endl;


do
{
cout<<"Please type 1 for ''smoking''"<<endl;
cout<<"Please type 2 for ''non-smoking''"<<endl;

cin>>seat[i];
for (i=0;i<10;i++)
if (seat[i]==1)
cin>>seat[i];
else if (seat[i]==2)
{
cin>>seat[i];
cout<<"Your seat assignment is "<<seat[5]<<" (non-smoking section)"<<endl;
}
else
cout<<"Next flight leaves in 3 hours"<<endl;
}while(i<10);



return 0;
}

//now i am getting an error after this so i'm really not sure what to do. Please HELP!

Is This A Good Question/Topic? 0
  • +

Replies To: Assigning and declaring arrays and elements.

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Assigning and declaring arrays and elements.

Posted 23 November 2005 - 04:38 AM

 cout<<"Please type 1 for ''smoking''"<<endl;


this line will generate an error as you cannot have "'s in a string like that, you have to use the '\"' character to denote a double quote insinde of a string

 cout<<"Please type 1 for \''smoking\''"<<endl;


as for filling the seats...do you have to fill each seat, one at a time, or keep requesting "smoking" or "non-smoking" from the user until all the seats are filled up. is part of the array supposed to be designated for smoking and the other part to be non-smoking only?
Was This Post Helpful? 0
  • +
  • -

#3 Flipmodemvp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-November 05

Re: Assigning and declaring arrays and elements.

Posted 23 November 2005 - 11:40 AM

Yes, I suppose to keep asking the user to input smoking or nonsmoking until all seats are full. I was thinking of using a do-while loop to keep asking the same question. But what I don't understand is how can I keep looping that question and fill up each array element with a passenger.
Was This Post Helpful? 0
  • +
  • -

#4 Flipmodemvp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-November 05

Re: Assigning and declaring arrays and elements.

Posted 23 November 2005 - 11:44 AM

and also, yes, only arrays [0]-[4] are non-smoking and arrays [5]-[9] should be for smoking. Once all of smoking or non-smoking is filled up, it should output something like, "The smoking section is full, would you like to sit in the smoking section(y or n). And for this I would use an "if" statement I believe. And if they choose "n" it will output "next flight leaves in 3 hours."
Was This Post Helpful? 0
  • +
  • -

#5 microchip  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 37
  • Joined: 14-August 05

Re: Assigning and declaring arrays and elements.

Posted 23 November 2005 - 02:10 PM

I'd do something like this then:

#define TOTAL_SEATS 10 //total number of seats
#define START_SMOKING_SEATS 5 //what seat number and up are smoking

unsigned char smoking = 0;
unsigned char nonsmoking = 0;

unsigned char seats[TOTAL_SEATS];

do {
    // cout code
    if (input_smoking) {
        if (smoking > (TOTAL_SEATS-START_SMOKING_SEATS)) {
            // no more smoking seats
        } else {
            seats[smoking+START_SMOKING_SEATS] = 2; // 2 = smoking
            smoking++;
        }
    } else if (input_nonsmoking) {
        if (nonsmoking == START_SMOKING_SEATS) {
            // no more non-smoking seats
        } else {
            seats[nonsmoking] = 1; // 1 = smoking
            nonsmoking++;
        }
    }
} while ((smoking+nonsmoking) != TOTAL_SEATS);



Something like that.

There is some psuedocode in, so you'll still have to do some thinking...

This post has been edited by microchip: 23 November 2005 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1