0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
If cust is defined as customer cust[3][4] then the following array entries exist and can be used:
cust[0][0] cust[0][1] cust[0][2] cust[0][3]
cust[1][0] cust[1][1] cust[1][2] cust[1][3]
cust[2][0] cust[2][1] cust[2][2] cust[2][3]

Which you will see is 3 rows of 4 columns, and neither cust[3][x] nor cust[x][4] are valid. So writing to cust[sizeR=3][sizeC=4] should not be attempted; that location is beyong the end of the array and you would be overwriting memory that belongs to another part of your program.
so early that is right:
Code:
#define sizeR 4
#define sizeC 5
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
Quote:
Originally Posted by dky8810 View Post
so early that is right:
Code:
#define sizeR 4
#define sizeC 5
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!");
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
dky8810's Avatar, Join Date: Nov 2008
Go4Expert Member
thk for all done ald!

Last edited by dky8810; 18Nov2008 at 20:12..