# array if for loop problem

Page 1 of 1

## 1 Replies - 1743 Views - Last Post: 18 November 2012 - 11:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300733&amp;s=3e69e80122c53d39886afa5f9c23d9d4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gandalf22

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 18-November 12

# array if for loop problem

Posted 18 November 2012 - 06:44 PM

Alright so im supposed to represent an airline. total # of seats is 10. Passangers can choose whether to sit in first or economy. This is what i have so far. If you try the program and try to go from first to economy you will see that it skips seats. For ex. #1 in First, then #7 in Economy then #3 in First. It also wont say that economy is full or that the airplane is full. Any tips?

```#include<stdio.h>
#include<stdlib.h>

int main (void)

{
int seat;
int s[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int f = 0; // first class//
int e = 5; // economy class//
int i;

printf("Welcome to the Airline Reservation system \n");

for(i=0;i<=10;i++)
{

printf("Enter 1 for First class, 2 for Economy Class or 0 to Exit \n");
scanf("%d", &seat);
{
if(seat==1 && s[f]<=5)

printf("You are in seat #%d in the First Class \n", s[f]);  //seat 1-5 works works fine if used consecutively//
f++;

if(s[f]>6)
{
printf("First class is full \n");
printf("Please enter 2 to switch to Economy Class or 0 to exit \n");
}
}

{
if(seat==2 && s[e]>=6)

printf("You are in seat #%d in the Economy Class \n", s[e]); //seat 6-10 works fine if used consecutively//
e++;

if(s[e]>10)
{
printf("Economy class is full \n");
printf("Please enter 1 to switch to First Class or 0 to exit \n");
}
}

{
if(seat==s[f]>5 && seat==s[e]>10)
printf("The airplane is full. Next flight leaves in 3 hours \n");
}

{
if(seat==0)
break;
}

}

system("PAUSE");

}

```

Is This A Good Question/Topic? 0

## Replies To: array if for loop problem

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: array if for loop problem

Posted 18 November 2012 - 11:12 PM

Here are the following problems I noted in your program that must be causing non-execution of your above mentioned statements.

1.First, coming to your condition where you mention s[e]>10.You will note that it is an array you have given and there are only 10 elements in the array.So, there is no way that condition will be true as the last element is 10 and there is no element after that. If you want that to be true, you need to add one more element to your array as 11( although this is not the best way to go about the problem)

So you will have

```int s[11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11};
```

Now,your condition will have the possibility to become true.

2.Your line 48...what exactly were you trying to do there using more than one relational operator?

What you want to do is give condition for the airplane to be full.

You may probably want to use one simple if condition.
```if( s[e]>10 && s[f]>5)
{
//Flight full
}

```

3. You seem to have used a lot of unnecessary braces which are not required at all. The examples would be lines 21, 34, 47 and their corresponding closing braces.

Now, I would like to tell you that the above suggestions are not the best way of going about the problem. I have just given you a solution to make your printf statements to get executed.

regards,
Raghav

This post has been edited by raghav.naganathan: 19 November 2012 - 02:58 AM