help me urgent

Discussion in 'C' started by dky8810, Nov 15, 2008.

  1. dky8810

    dky8810 New Member

    Joined:
    Nov 15, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    so early that is right:
    Code:
    #define sizeR 4
    #define sizeC 5
     
  2. dky8810

    dky8810 New Member

    Joined:
    Nov 15, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void Booking(void)
    {
    	strcpy(cust[sizeR][sizeC].flightNo,"AK65");
    	strcpy(cust[sizeR][sizeC].date,"25-12-2008");
    	strcpy(cust[sizeR][sizeC].time,"23:45");
    	strcpy(cust[sizeR][sizeC].gate,"G9");
    	int ID[4][5]={
    	{0,0,0,0,0},
    	{0,0,0,0,0},
    	{0,0,0,0,0},
    	{0,0,0,0,0}
    	};
    	
    	int i;
    	int row;
    	int col;
    	int flag=0;
    	printf("How many seat are you required:\n");
    	scanf("%d",&i);
    	for(row=0;row<sizeR;row++){
    		for(col=0;col<sizeC;col++){
    			if(0==ID[row][col]){
    				flag=1;
    				break;
    			
    			}
    			else{
    			printf("Not enough seats available!The flight is fully booked!");
    
    	}
    	}
    	}
    }
    
    now is this part make me blur:wat i need to write in <if>the code that can let me check still got how many empty in array??
    Code:
    for(row=0;row<sizeR;row++){
    		for(col=0;col<sizeC;col++){
    			if(0==ID[row][col]){
    				flag=1;
    				break;
    			
    			}
    			else{
    			printf("Not enough seats available!The flight is fully booked!");
     
  3. dky8810

    dky8810 New Member

    Joined:
    Nov 15, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    question:
    now is this part make me blur:what i need to write in <if>the code that can let me check still got how many empty place in my cust array??

    Code:
    for(row=0;row<sizeR;row++){
    for(col=0;col<sizeC;col++){
    if(0==ID[row][col]){
    	for(j=0;j<i;j++){
    		printf("Enter your name");
    		scanf("%[\n]c",cust[sizeR][sizeC].name);
    
    }
    else{
    printf("Not enough seats available!The flight is fully booked!");
    how?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you're counting the number of zeros in ID then all you need to do in the if is to increment a counter.
    Then after the two for loops, look at the counter and compare it with the number of seats the customer wants.
     
  5. dky8810

    dky8810 New Member

    Joined:
    Nov 15, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void Booking(void)
    {
    	strcpy(cust[sizeR][sizeC].flightNo,"AK65");
    	strcpy(cust[sizeR][sizeC].date,"25-12-2008");
    	strcpy(cust[sizeR][sizeC].time,"23:45");
    	strcpy(cust[sizeR][sizeC].gate,"G9");
    	int ID[4][5]={
    	{0,0,0,0,0},
    	{0,0,0,0,0},
    	{0,0,0,0,0},
    	{0,0,0,0,0}
    	};
    	int con;
    	int id;
    	int i;
    	int j;
    	int row;
    	int col;
    	int flag=0;
    	printf("How many seat are you required:\n");
    	scanf("%d",&i);
    	int available_seats = 0;
    		for(row=0; row<sizeR; row++){
    			 for(col=0; col<sizeC; col++){
    				if(cust[sizeR][sizeC].id == 0){
    				     available_seats++;
            }
        }
    }
    				if(available_seats >= i){
    					for(j=0;j<i;j++){
    							printf("\t\t\t--- KL Airline Booking Ticket ---\n");
    							cust[sizeR][sizeC].id=id;
    							id++;
    							printf("\t\t\tBooking ID\t:%d",&cust[sizeR][sizeC].id);
    							printf("\t\t\tFlight No.:");
    							scanf("%s",cust[sizeR][sizeC].flightNo);
    							fflush(stdin);
    							printf("\t\t\tPassenger:");
    							scanf("%[\n]s",cust[sizeR][sizeC].name);
    							fflush(stdin);
    							printf("\t\t\tDate:");
    							scanf("%s",cust[sizeR][sizeC].date);
    							fflush(stdin);
    							printf("\t\t\tTime:");
    							scanf("%s",cust[sizeR][sizeC].time);
    							fflush(stdin);
    							printf("\t\t\tGate:");
    							scanf("%s",cust[sizeR][sizeC].gate);
    							fflush(stdin);
    
    							printf("\t\t\tSeat No:");
    							scanf("%c",&cust[sizeR][sizeC].seatNo);
    							fflush(stdin);
    							i++;
    							system("pause");
    							system("cls");
    													
    
    						/*	printf("Want Countinue? press 1");
    							scanf("%d",&con);
    							if(con==1)
    							{
    								system("cls");
    								Booking();
    							}
    							else
    							{
    								system("cls");
    								menu();
    							}*/
    							}
    							}
    											
    											
    
    						
    				else{
    				printf("Not enough seats available!");
    }
    
    why it keep looping ?which line i wrong?
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I don't understand the point of the first four lines. Remember the comment about putting the ring on the sixth finger of your hand?

    > cust[sizeR][sizeC].id=id;

    Writing to the non-existent cust[sizeR][sizeC] again. Remember the comment about putting the ring on the sixth finger of your hand? Are you hoping by telling the computer to write to cust[sizeR][sizeC] that it'll think "oh, he doesn't really mean that, let me go look through the array for an empty slot, even though he hasn't asked me to do that"? It won't.

    > scanf("%s",cust[sizeR][sizeC].flightNo);

    Writing to the non-existent cust[sizeR][sizeC] again. You really aren't getting this, are you?


    Right.


    Let's say cust is a one dimensional (1D) array, and let's stick with this until we get it, OK? Then we can graduate to 2D arrays, because the principle is the same.

    Let's also say cust is defined as customer cust[sizeX], and sizeX=5.

    Now, read carefully.

    C starts counting at ZERO. So instead of going 1,2,3,4,5 etc, it goes ZERO, 1,2,3,4 etc. DO NOT PROCEED UNTIL YOU GET THAT STATEMENT.

    You proceeded, didn't you. Naughty. Now go back and *really* read it over and over until you get it.

    This is just like starting at any other number; if you start at 10, you go "10,11,12,13"; if you start at 100 you go "100, 101, 102, 103", if you start at -357 you go "-357, -356, -355, -354", and if you start at 0 you go "0, 1, 2, 3, 4".

    If you declare an array to have FIVE elements, then it will have FIVE elements. DO NOT PROCEED UNTIL YOU GET THAT STATEMENT.

    Now, let's put those two together. If you declare an array to have FIVE elements, then those FIVE elements will be numbered:
    0 - the first
    1 - the second
    2 - the third
    3 - the fourth

    AND...(drumroll)

    4 (FOUR. THAT IS ONE LESS THAN FIVE.) - The LAST element, after which there are NO MORE ELEMENTS.

    You will notice, and if you don't I'm going to spell out exactly what you must notice, is that IF YOU DECLARE AN ARRAY WITH ***FIVE*** ELEMENTS, THEN INDEX 5 IS ***NOT***, REPEAT ***NOT*** IN THE ARRAY.

    It is ABSOLUTELY VITAL that you get everything above. If you don't get it, PLEASE get someone to explain it to you face to face. Because you will never get anywhere in programming if you can't handle arrays correctly.



    So this means you CANNOT, ABSOLUTELY MUST NOT TRY, MUST AVOID AT ALL COSTS, ABSOLUTELY CANNOT write to the array whose index equals the size of that array.

    So:
    Code:
    int x[5];
    x[0]=1; // THIS IS OK
    x[1]=2; // THIS IS ALSO OK
    x[2]=3; // THIS TOO IS OK
    x[3]=4; // STILL NO PROBLEM
    
    x[4]=5; // THATS THE END, LETS WATCH OUT FOR BOUNDARY CONDITIONS
    
    x[5]=6; // NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO. NO.
    
     
  7. dky8810

    dky8810 New Member

    Joined:
    Nov 15, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thk for all done ald!
     
    Last edited: Nov 18, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice