Thread: Review my finished assignment

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    385

    Review my finished assignment

    I was wondering if any of you C gurus would mind checking out my assignment that's due on Tuesday. It seems to run fine and I think I have it completed but wouldn't mind if some of you guys kind of proof read it and make sure I've done things correctly. Take into consideration we have just been using printf and scanf and basic control structures.

    Code:
    /* Airline Reservation System */
    
    #include <stdio.h>
    #define SIZE 10
    
    void reserve_first_class(int [], int);
    void reserve_economy(int [], int);
    
    int main(void)
    {
    	int seats[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int choice;
    
    	printf("Please type 1 for \"first class\"\n");
    	printf("Please type 2 for \"economy\"\n");
    	printf("Enter -99 to end>>>");
    	scanf("%d", &choice);
    
    	while(choice != -99)
    	{
    		if(choice == 1)
    		{
    			reserve_first_class(seats, SIZE);
    		}
    		else if(choice == 2)
    		{
    			printf("You chose choice 2\n\n");
    			reserve_economy(seats, SIZE);
    		}
    		else
    			printf("\n\nPlease Select A Valid Choice!!\n\n");
    		
    		printf("Please type 1 for \"first class\"\n");
    		printf("Please type 2 for \"economy\"\n");
    		printf("Enter -99 to end>>>");
    		scanf("%d", &choice);
    	}
    	return 0;
    }
    
    void reserve_first_class(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    	
    	for(i = 0; i < size/2; i++)
    	{
    		if(s[i] == 0)
    		{
    			s[i] = 1;
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break;
    		}
    		else 
    			continue;
    	}
    	if(seat == 0)
    	{
    		
    		printf("\nThere are no more available First Class seats\n");
    		printf("Would you like a seat in Economy? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision);
    	
    		if(decision == 'y' || decision == 'Y')
    			reserve_economy(s, size);
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    	}
    	
    }
    
    void reserve_economy(int s[], int size)
    {
    	int i, seat = 0;
    
    	for(i = size/2; i < size  ; i++)
    	{
    		if(s[i] == 0)
    		{
    			s[i] = 1;
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break;
    		}
    		else 
    			continue;
    	}
    
    	if(seat == 0)
    		printf("\n\nSorry the flight is currently full\n\n");
    }
    I would greatly appreciate it if anyone has any insight that could help me.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Review my finished assignment

    Originally posted by damonbrinkley
    I was wondering if any of you C gurus would mind checking out my assignment that's due on Tuesday. It seems to run fine and I think I have it completed but wouldn't mind if some of you guys kind of proof read it and make sure I've done things correctly. Take into consideration we have just been using printf and scanf and basic control structures.

    Code:
    /* Airline Reservation System */
    
    #include <stdio.h>
    #define SIZE 10
    
    void reserve_first_class(int [], int);
    void reserve_economy(int [], int);
    
    int main(void)
    {
    	int seats[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    	int choice;
    
    	printf("Please type 1 for \"first class\"\n");
    	printf("Please type 2 for \"economy\"\n");
    	printf("Enter -99 to end>>>");
    	scanf("%d", &choice);
    
    	while(choice != -99)
    	{
    		if(choice == 1)
    		{
    			reserve_first_class(seats, SIZE);
    		}
    		else if(choice == 2)
    		{
    			printf("You chose choice 2\n\n");
    			reserve_economy(seats, SIZE);
    		}
    		else
    			printf("\n\nPlease Select A Valid Choice!!\n\n");
    		
    		printf("Please type 1 for \"first class\"\n");
    		printf("Please type 2 for \"economy\"\n");
    		printf("Enter -99 to end>>>");
    		scanf("%d", &choice);
    	}
    
    
    	return 0;
    }
    
    void reserve_first_class(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    	
    	for(i = 0; i < size/2; i++)
    	{
    		if(s[ i] == 0)
    		{
    			s[ i] = 1;
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break;
    		}
    		else 
    			continue;
    	}
    	if(seat == 0)
    	{
    		
    		printf("\nThere are no more available First Class seats\n");
    		printf("Would you like a seat in Economy? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision);
    	
    		if(decision == 'y' || decision == 'Y')
    			reserve_economy(s, size);
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    	}
    	
    }
    
    void reserve_economy(int s[], int size)
    {
    	int i, seat = 0;
    
    	for(i = size/2; i < size  ; i++)
    	{
    		if(s[ i] == 0)
    		{
    			s[ i] = 1;
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break;
    		}
    		else 
    			continue;
    	}
    
    	if(seat == 0)
    		printf("\n\nSorry the flight is currently full\n\n");
    }
    I would greatly appreciate it if anyone has any insight that could help me.
    Looks good and straight forward. A couple comments you can take or leave:

    1) I would use a do...while to replace the while in red. That way your menu display won't need to be used twice, before the loop and at the end of it.

    2) Why use -99 to exit? Why not 0 or 9? They are easier to type.

    3) Insead of size/2 to define the number of seats in 1st class, specify a value as a #define. This way you can specify exactly how many seats there are. It took me a few minutes to understand what size/2 meant.

    4) Comments, comments, comments. You need to comment your code so the reader doesn't have to figure out what you're trying to accomplish (see 3)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hey I compiled it and it works fine. The only thing I would change is the parameters for your functions. In your prototypes you leave them empty, I usually specify a variable name. If that's the way they did it in class then just keep it that way, but if it isn't I'd change it. I think in like bigger programms it will be harder to follow what is what without those variable names. Especially if say those were in a header file. Also even though it doesn't really need comments, if your teacher is in the habit of using lots of comments you should do it too
    Goodluck, I'm sure you will get an A.

    Insead of size/2 to define the number of seats in 1st class, specify a value as a #define. This way you can specify exactly how many seats there are. It took me a few minutes to understand what size/2 meant.
    Just read the above post heh, yea that caught me too when I saw size was being passed instead of SIZE. But then I saw the assignment in the for statement. At first I thought it was an error cause half of SIZE was being passed instead of SIZE heh. Anyways I think your idea about using a #define is a good one. It will make it easier to read. Again it looks great, I see no reason why you should not get an easy A.
    Last edited by SourceCode; 06-20-2003 at 12:33 PM.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Originally posted by Salem
    > printf("Enter -99 to end>>>");
    If there is no newline at the end of a printf(), you should really follow it with
    fflush( stdout );
    to make sure you see the text.
    We haven't messed with fflush or anything like that so that's why I didn't use it.



    > printf("You chose choice 2\n\n");
    For consistency, the other one should have
    printf("You chose choice 1\n\n");



    Ah, that was in there for debugging, it's gone now.


    > int seats[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int seats[SIZE] = {0};
    achieves the same thing


    Didn't even think of that, THANKS


    > else
    > continue;
    The for loop will do exactly this without you having to state it.


    Cool, guess I can axe those


    > if(seat == 0)
    > printf("\n\nSorry the flight is currently full\n\n");
    Why not give the economy passengers chance to fly first (however unlikely it may seem)
    I thought about that but the problem didn't state that needed to be done so I didn't worry about it.

    Thanks for the input....
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Alright, here's my new and improved code thanks to you guys.

    Code:
    /* Airline Reservation System */
    
    #include <stdio.h>
    #define SIZE 10
    
    void reserve_first_class(int [], int); /* books first class ticket */
    void reserve_economy(int [], int); /* books economy ticket */
    
    int main(void)
    {
    	/* Declare arrays of seats and initialize all to 0 */
    	int seats[SIZE] = { 0 };
    	int choice;
    	
    	
    	printf("Please type 1 for \"first class\"\n");
    	printf("Please type 2 for \"economy\"\n");
    	printf("Enter -99 to end>>>");
    	scanf("%d", &choice); 	/* Get choice from user */
    	
    	while(choice != -99)
    	{
    		if(choice == 1)		/* User chose first class */
    		{
    			/* first class function call */
    			reserve_first_class(seats, SIZE);
    		}
    		else if(choice == 2) /* User chose economy */
    		{
    			/* economy function call */
    			reserve_economy(seats, SIZE);
    		}
    		else
    		{
    			/* Invalid Choice */
    			printf("\n\nPlease Select A Valid Choice!!\n\n");
    		}
    		printf("Please type 1 for \"first class\"\n");
    		printf("Please type 2 for \"economy\"\n");
    		printf("Enter -99 to end>>>");
    		scanf("%d", &choice);
    	} 	
    	return 0;
    }
    
    void reserve_first_class(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    	
    	/* first class is seats 1-5 (s[0] to s[4]) */
    	for(i = 0; i < size/2; i++)
    	{
    		if(s[i] == 0) /* If seat is empty */
    		{
    			s[i] = 1; /* assign the seat to this person */
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break; /* they got a seat, get out of the loop */
    		}
    		
    	}
    	if(seat == 0) /* If they didn't get a seat, ask if they want economy */
    	{
    		
    		printf("\nThere are no more available First Class seats\n");
    		printf("Would you like a seat in Economy? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision); /* Get their choice */
    	
    		if(decision == 'y' || decision == 'Y') /* they chose yes */
    			reserve_economy(s, size); /* send them to the economy function */
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    	}
    	
    }
    
    void reserve_economy(int s[], int size)
    {
    	int i, seat = 0;
    
    	/* economy is seats 6-10 (s[5] to s[9]) */
    	for(i = size/2; i < size; i++)
    	{
    		if(s[i] == 0) /* Seat is empty */
    		{
    			s[i] = 1; /* assign them the seat */
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break; /* they got a seat, get out of the loop */
    		}
    		
    	}
    
    	if(seat == 0) /* They didn't get a seat and economy is full */
    		printf("\n\nSorry the flight is currently full\n\n");
    }
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf("%d", &choice);
    Always check the return code from scanf() to ensure it did what you wanted. Read this for better ways of obtaining a number from the user.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Code:
    /* Airline Reservation System */
    
    #include <stdio.h>
    #define SIZE 10
    
    void reserve_first_class(int [], int); /* books first class ticket */
    void reserve_economy(int [], int); /* books economy ticket */
    
    int main(void)
    {
    	/* Declare arrays of seats and initialize all to 0 */
    	int seats[SIZE] = { 0 };
    	int choice;
    	
    	
    	printf("Please type 1 for \"first class\"\n");
    	printf("Please type 2 for \"economy\"\n");
    	printf("Enter -99 to end>>>");
    	scanf("%d", &choice); 	/* Get choice from user */
    	
    	while(choice != -99)
    	{
    		if(choice == 1)		/* User chose first class */
    		{
    			/* first class function call */
    			reserve_first_class(seats, SIZE);
    		}
    		else if(choice == 2) /* User chose economy */
    		{
    			/* economy function call */
    			reserve_economy(seats, SIZE);
    		}
    		else
    		{
    			/* Invalid Choice */
    			printf("\n\nPlease Select A Valid Choice!!\n\n");
    		}
    		printf("Please type 1 for \"first class\"\n");
    		printf("Please type 2 for \"economy\"\n");
    		printf("Enter -99 to end>>>");
    		scanf("%d", &choice);
    	} 	
    	return 0;
    }
    
    void reserve_first_class(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    	
    	/* first class is seats 1-5 (s[0] to s[4]) */
    	for(i = 0; i < size/2; i++)
    	{
    		if(s[i] == 0) /* If seat is empty */
    		{
    			s[i] = 1; /* assign the seat to this person */
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break; /* they got a seat, get out of the loop */
    		}
    		
    	}
    			//Check if at least a seat is available
    	if(seat == 0 && s[size-1] == 0) /* If they didn't get a seat, ask if they want economy */
    	{
    		
    		printf("\nThere are no more available First Class seats\n");
    		printf("Would you like a seat in Economy? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision); /* Get their choice */
    	
    		if(decision == 'y' || decision == 'Y') /* they chose yes */
    			reserve_economy(s, size); /* send them to the economy function */
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    	}
    	
    	else if(seat == 0) /* They didn't get a seat and economy is full */
    		printf("\n\nSorry the flight is currently full\n\n");
    }
    
    
    void reserve_economy(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    
    	/* economy is seats 6-10 (s[5] to s[9]) */
    	for(i = size/2; i < size; i++)
    	{
    		if(s[i] == 0) /* Seat is empty */
    		{
    			s[i] = 1; /* assign them the seat */
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break; /* they got a seat, get out of the loop */
    		}
    		
    	}
    
    
    				//Check if at least a seat is available
    	if(seat == 0 && s[size/2-1] == 0) /* If they didn't get a seat, ask if they want economy */
    	{
    		
    		printf("\nThere are no more available Economy Class seats\n");
    		printf("Would you like a seat in First Class? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision); /* Get their choice */
    	
    		if(decision == 'y' || decision == 'Y') /* they chose yes */
    			reserve_first_class(s, size); /* send them to the economy function */
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    
    	}
    
    	else if(seat == 0) /* They didn't get a seat and economy is full */
    		printf("\n\nSorry the flight is currently full\n\n");
    
    }
    What about if there is no more seat available Economy Class and available seat(s) in First Class?
    You can try this way.
    Last edited by zahid; 06-24-2003 at 02:08 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  8. #8
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Really? how was that? Okay let me check. I marked RED the part of code I added. Salem, why don't you give us little time and show the bugs.
    Last edited by zahid; 06-24-2003 at 02:03 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    Like I said earlier, the problem given by the professor said to offer economy if first class is full but nothing about offering first class if economy is full. Zahid, your code won't work because if seat == 0 then s[size-1] is going to be equal to 1 and not zero. If testing for 0 then that for loop will never be true which defeats the purpose of it.
    Last edited by damonbrinkley; 06-24-2003 at 04:41 AM.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  10. #10
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Originally posted by damonbrinkley
    Like I said earlier, the problem given by the professor said to offer economy if first class is full but nothing about offering first class if economy is full. Zahid, your code won't work because if seat == 0 then s[size-1] is going to be equal to 1 and not zero. If testing for 0 then that for loop will never be true which defeats the purpose of it.
    Okay.. I haven't looked at the condition (to offer economy if first class is full but nothing about offering first class if economy is full) you are saying now.

    Anyway, accourding to your condition, "Sorry the flight is currently full" is not true, because there could be available seat(s). That's why I thought there is something missing.

    I guess that isn't the bug salem was talking about?
    Last edited by zahid; 06-24-2003 at 06:13 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Maybe not a bug in the current program, but personally I don't like the looks of this:
    >>if(seat == 0 && s[size/2-1] == 0)

    If size/2 equates to 0, then you'll end up indexing s[-1], which is not allowed.

    Like I said, the current code may not invoke this bug, but changes later down the line may.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    uuuu yeah, Salem is absolutely correct. And Salem knew that cut and pest is the reason.


    Code:
    /* Airline Reservation System */
    
    #include <stdio.h>
    #define SIZE 10
    
    void reserve_first_class(int [], int); /* books first class ticket */
    void reserve_economy(int [], int); /* books economy ticket */
    
    void display_all(int [], int);
    
    
    int main(void)
    {
    	/* Declare arrays of seats and initialize all to 0 */
    	int seats[SIZE] = { 0 };
    	int choice;
    	
    	
    do{
    	printf("Please type 1 for \"first class\"\n");
    	printf("Please type 2 for \"economy\"\n");
    	printf("Please type 3 to see all seats\n");
    	printf("Enter -99 to end>>>");
    	scanf("%d", &choice); 	/* Get choice from user */
    	
    switch(choice)
    {
    case 1:
    			/* User chose first class */
    			/* first class function call */
     if(seats[SIZE/2-1] != 0 && seats[SIZE-1] != 0) //Check if at least a seat is available
    	{
    	printf("\n\nSorry the flight is currently full\n\n");
    	break;
    	}
    	reserve_first_class(seats, SIZE);
    	break;
    
    case 2:			/* User chose economy */
    			/* economy function call */
     if(seats[SIZE/2-1] != 0 && seats[SIZE-1] != 0) //Check if at least a seat is available
    	{
    	printf("\n\nSorry the flight is currently full\n\n");
    	break;
    	}
    	reserve_economy(seats, SIZE);
    	break;		
    
    case 3:			
    			/* User chose Display All */
    			/* economy function call */
    	display_all(seats, SIZE);
    	break;
    
    case -99:
    	return 0;
    
    default:
    			/* Invalid Choice */
    	printf("\n\nPlease Select A Valid Choice!!\n\n");
    }
    
    }while(1);
    
    }
    
    
    void reserve_first_class(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    	
    	/* first class is seats 1-5 (s[0] to s[4]) */
    	for(i = 0; i < size/2; i++)
    	{
    		if(s[i] == 0) /* If seat is empty */
    		{
    			s[i] = 1; /* assign the seat to this person */
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break; /* they got a seat, get out of the loop */
    		}
    		
    	}
    
    	if(seat == 0) /* If they didn't get a seat, ask if they want economy */
    	{
    		
    		printf("\nThere are no more available First Class seats\n");
    		printf("Would you like a seat in Economy? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision); /* Get their choice */
    	
    		if(decision == 'y' || decision == 'Y') /* they chose yes */
    			reserve_economy(s, size); /* send them to the economy function */
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    	}
    	
    }
    
    
    void reserve_economy(int s[], int size)
    {
    	int i, seat = 0;
    	char decision;
    
    	/* economy is seats 6-10 (s[5] to s[9]) */
    	for(i = size/2; i < size; i++)
    	{
    		if(s[i] == 0) /* Seat is empty */
    		{
    			s[i] = 1; /* assign them the seat */
    			printf("\n\nYou have a seat, Your flight will leave shortly\n\n");
    			seat = 1;
    			break; /* they got a seat, get out of the loop */
    		}
    		
    	}
    
    
    	if(seat == 0) /* If they didn't get a seat, ask if they want economy */
    	{
    		
    		printf("\nThere are no more available Economy Class seats\n");
    		printf("Would you like a seat in First Class? (Y or N) \n");
    		printf(">>>");
    		scanf(" %c", &decision); /* Get their choice */
    	
    		if(decision == 'y' || decision == 'Y') /* they chose yes */
    			reserve_first_class(s, size); /* send them to the economy function */
    		else
    			printf("\nNext flight leaves in 3 hours!\n\n");
    
    	}
    }
    
    
    void display_all(int seats[], int size)
    {
    int i;
    
    for(i = 0; i < size/2; i++)
    printf("1st_C_seat:%d Stat:%d\n",i, seats[i]);
    
    for(; i < size; i++)
    printf("Eco_C_seat:%d Stat:%d\n",i, seats[i]);
    
    }
    
    Little change will be necessary to offer economy if first class is full but nothing about offering first class if economy is full.
    At switch() and reserve_economy(int [], int);
    Last edited by zahid; 06-24-2003 at 08:24 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. Just finished assembly assignment
    By xddxogm3 in forum Tech Board
    Replies: 6
    Last Post: 10-08-2005, 04:01 PM