Need help with Arrays

This is a discussion on Need help with Arrays within the C Programming forums, part of the General Programming Boards category; ArrayQuestion).You are to write a program to assingn seats on each flights only plane ( capacity: 10 seats).. Your program ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Unhappy Need help with Arrays

    ArrayQuestion).You are to write a program to assingn seats on each flights only plane ( capacity: 10 seats)..
    Your program should display the following menu of alternatives..

    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. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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"?
    hello, internet!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    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. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    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
    Last edited by Hammer; 08-06-2002 at 05:17 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    OT rant:
    > Try flowing the flow of your code
    No offense Hammer, but, how do I do that?
    The world is waiting. I must leave you now.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shadow
    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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    thanks alot guys

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

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21