# Thread: Need help with Arrays

1. ## Need help with Arrays

ArrayQuestion).You are to write a program to assingn seats on each flights only plane ( capacity: 10 seats)..

Please type 1 for First Class
Please type 2 for Economy Class

If the person types 1, then your program should assign a seat in the first class section (seats 1-5).If the person types two , then your program should assign a seat in the economy class (seats 6- 10).Your program should print a bording pass indicating the person's seat number and whether it is in the first class or economy section of the plane.
Use a single-subscript array to represent the seating chart of the plane.Initislize sll the elements of the array to 0 to indicate that all seats are empty.As each seat is assigned, set the corresponding element 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 used.When the first class section is full, your programm should ask the person if it is acceptable to be placed in the economy section( and vise-versa).If yes then make the appropriate seat assingment.If n, then print the message "Nest flight leaves in 3 hours".

this is what i have done so far ..
just some hint and tips would help
Code:
```#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

int main()
{
int seats[SIZE] = {0};
int select, i=0, k = 5;
char yn;

while ( seats[SIZE] > 9 ){
printf( "Please type 1 for First Class."
"\nPlease type 2 for Economy Class.");
scanf ("%d", &select );
if ( seats[SIZE] > 9 )
printf( "Next Flight leaves in 3 hours\n");
if ( select == 1 ){
++seats[i++];
if ( seats[i] > 4 ){ {
printf ( "Sorry first class is taken do you need economy class");
scanf ("%c", &yn );}
if ( yn == 'y' )
++seats[++k];
else
printf ("\nNext Flight leaves in 3 hours");
} }
else
if ( select == 2 ){
if ( seats[k] > 4 )
printf ("Sorry no more seats");
else
++seats[++k];}}

system("PAUSE");
return 0;
}```
Thanks alot ....

2. i havent read throughly through your code, but does it compile and run? if so is there a problem with it? or do you just want general advice "how could it be better"?

3. >while (seats[SIZE] > 9)
You can going outside of the array bounds. Remember you allocated it like this:
>int seats[10]
which means you access the elements via 0 to 9, not 1 to 10.

There are a few other problems too, but if you try compiling and running it, you'll soon spot what they are

4. ## help yoo.

Code:
```#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

int main()
{
int  seats[SIZE]= {0};
int select, i=0, k = 4, cnt;
char yn;

while ( seats[SIZE-1] != 9 ){

printf( "Please type 1 for First Class."
"\nPlease type 2 for Economy Class.");
scanf ("%d", &select );

if ( select == 1 ){
++seats[i];
if ( seats[i] < 4 )
printf( "\nSorry first class is taken do you need economy class");
scanf ("%s", &yn );

if ( yn == 'y' )
++seats[++k];
else if ( yn == 'n' )
printf ("\nNext Flight leaves in 3 hours\n");
break;
}

else if ( select == 2 ){

if ( seats[k] >= 4 )
printf ("Sorry no more seats for economy class\n");
else
++seats[++k];}}

for ( cnt =0; cnt <= SIZE-1; cnt++ )
printf ("%d", seats[cnt] );

system("PAUSE");
return 0;
}```
The problem with this code is that when u choose 1 it keeps on executing the wrong statment why???

5. Code:
```if ( seats[i] < 4 )
printf( "\nSorry first class is taken do you need economy class");
scanf ("%s", &yn );```
I'm assuming you meant to do this, and you didn't accidently omit the braces, right?

Quzah.

6. ## a lil more explanation needed

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <conio.c>
#define SIZE 10

int main()
{
int  seats[SIZE]= {0};
int select, i=0, k = 5, cnt;
char yn;

while ((( i < 4 ) || ( k < 10))){
textcolor (LIGHTGREEN);
printf( "\nPlease type 1 for First Class."
"\nPlease type 2 for Economy Class.");
scanf ("%d", &select );

if (  select == 1 ) {
if ( i > 4  ){
textcolor(LIGHTBLUE);
printf( "Sorry first class is taken do you need economy class[y/n]: ");
scanf ("%s", &yn );   }

if ( yn == 'y' )
++seats[++k];
else if ( yn == 'n' ) {
textcolor(LIGHTGRAY);
textcolor(RED);
printf ("\nNext Flight leaves in 3 hours\n");
break;}
++seats[i++];
}

else if ( select == 2 ){

if ( k > 10 ){
textcolor(RED);
printf ("\nNow there arnt any seats for economy class\n");  }

else
++seats[++k];}
}
for ( cnt =0;cnt <= SIZE-1; cnt++ )
printf ("%d", seats[cnt] );

printf ("\n" );

textcolor (RED);
printf( "----------VIRGIN ATLANTIC----------\n"
"     No more seats are avaiable\n"
"----------From 1980 to 2002--------\n");
textcolor(LIGHTGRAY);

system("PAUSE");
return 0;
}```
Now the only problem is i have to type .2 for economy class 7 times and it does not increment 5 just 6,7,8,9 is incremented ...why?????

7. ## Re: a lil more explanation needed

>it does not increment 5 just 6,7,8,9 is incremented ...why?????
probably because of this:
>++seats[++k];
perhaps you meant
>++seats[k++];
Personally, I'd split it into 2 statements to aid clarity.

>i have to type .2 for economy class 7 times
yep, the logic is a bit screwy. Try following the flow of your code, and seeing if it is doing what you think. Debugging is fun

8. OT rant:
> Try flowing the flow of your code
No offense Hammer, but, how do I do that?

OT rant:
> Try flowing the flow of your code
No offense Hammer, but, how do I do that?
goddam keyboard of mine can't spell properly. I've corrected it now

10. ## thanks alot guys

Yeah i found out the dam problem ur right hammer ..Its the way i used my if/else ...

Thanks again