Thread: please help me

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Unhappy please help me

    my program is supposed to assign 10 seats on a flight for two classes using one array. apparently there is a problem initializing the array. please help me

    #include <stdio.h>
    #define SIZE 10

    int main ()

    {

    int seats[SIZE]= {0}, answer, first = 1, economy = 6, i, j;

    printf( "Welcome to the Airline Reservation System of JFL AIR\n" );
    printf( "Choose your desired option:\n" );
    printf( "Please type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);

    if ( answer == 1)
    answer = first;

    if (answer == 2)
    answer = economy;


    for ( i = 1; i <= SIZE -6; i++)
    ++first;
    printf( "\nYour seat number is: %d\n", seats[i]);
    seats[i] = i + 1;

    while (first !=5){
    printf( "Please type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);
    if ( answer == 1)
    answer = first;

    if (answer == 2)
    answer = economy;
    }


    for ( j = 6; j <= SIZE -1; j++)
    ++economy;
    printf( "\nYour seat number is: %d\n", seats[j]);
    seats[j] = j + 1;

    while (economy !=10){
    printf( "Please type 1 for \"First Class\".\n" );
    printf( "Please type 2 for \"Economy\". " );
    scanf( "%d", &answer);
    if ( answer == 1)
    answer = first;

    if (answer == 2)
    answer = economy;

    }



    if ( first == 5){
    printf( "First Class is full.\n" );
    printf( "Type 1 for an Economy Seat or Type 2 to wait for next flight\n ");
    scanf( "%d", &answer);
    }

    if (answer == 1){
    ++economy;
    }
    if (answer == 2)
    printf( "Next flight leaves in 3 hours" );

    if ( economy == 10) {
    printf( "Economy Class is full.\n" );
    printf( "Type 1 for a First Class Seat or Type 2 to wait for next flight.\n ");
    scanf( "%d", &answer);
    }
    if (answer == 1){
    ++first;
    }
    if (answer == 2)
    printf( "Next flight leaves in 3 hours." );

    return 0;

    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I may be crazy, but this is almost exactly like a C++ program I helped out with a while back. Try searching the boards for that thread, you may find it interesting.
    http://www.cprogramming.com/cboard/s...&threadid=6610
    http://www.cprogramming.com/cboard/s...ghlight=flight

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    array initialization

    Since seats[SIZE] is local to main, you have to initialize each memeber to 0:

    int seats[SIZE]= {0, 0, 0, 0, 0, 0};

    or using some loop scheme. Local variables have unknown initializations (garbage). You must assign it before use.

    If you make seats[SIZE] global, you need not have to do this. All global variables are initialized to zero if not specified.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    int seats[SIZE]= {0, 0, 0, 0, 0, 0};
    If you have a large array initialising each element would be time consuming, you only need to initialise the first element as any remaining elements are set to zero automatically.
    int seats[SIZE] = {0};
    Last edited by C_Coder; 03-17-2002 at 03:57 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    Yes

    C_coder,
    For some reason I thought it was complier dependent, but this shorthand is indeed guaranteed. I stand corrected.

    Dave

Popular pages Recent additions subscribe to a feed