445,849 Members | 2,164 Online Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

# Which is better

 P: n/a Q: write a program so that it excepts six even numbers or until the number 99 is entered please explain why one is better than the other, if that is the case. A1: /*EX6-1.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int array, x, number; for(x = 0; x < 6 && number != 99; x++) { puts("Enter an even value or 99 to quit: "); scanf("%d",&number); while(number % 2 == 1 && number != 99) { printf("%d is not even.\n try again\n",number); scanf("%d",&number); } array[x] = number; } for(x = 0; x < 6 && number != 99; x++) { printf("%d,\t",array[x]); } puts(""); return 0; } A2: /* EX5.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int variable, ctr = 0, nbr; while(ctr <= 5 && nbr != 99) { printf("Enter 6 values no decimal point or 99 to Quit "); scanf("%d",&nbr); if(nbr % 2 == 0) { variable[ctr] = nbr; ctr++; } else if(nbr == 99) { puts("Exiting program"); } else puts("number not an even number try again"); } puts(""); for(ctr = 0; ctr <= 5 && nbr != 99; ctr++) { printf("%d value %d\n",ctr +1,variable[ctr]); } return 0; } Nov 14 '05 #1
3 Replies

 P: n/a darklight wrote: Q: write a program so that it excepts six even numbers or until the number 99 is entered please explain why one is better than the other, if that is the case. (Assuming you mean "accepts" rather than "excepts".) Even with this correction, the spec is unclear. I will assume that the program is supposed to terminate when either condition is met (i.e. it will never accept more than six even numbers, but may accept fewer). A1: /*EX6-1.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int array, x, number; for(x = 0; x < 6 && number != 99; x++) Since number does not have a determinate value on this first iteration of the loop, the comparison with 99 invokes undefined behaviour. { puts("Enter an even value or 99 to quit: "); scanf("%d",&number); What if the user enters "TEN"? scanf will tell you that the input data could not be converted to the required form, if you'll only listen - by checking the return value. while(number % 2 == 1 && number != 99) { printf("%d is not even.\n try again\n",number); scanf("%d",&number); } array[x] = number; } for(x = 0; x < 6 && number != 99; x++) This time, there is no need for the number != 99 condition. In fact, the condition suppresses the output altogether if the user entered 99 to indicate that he'd finished entering numbers. If this is what you wanted, great, but it may not have been. { printf("%d,\t",array[x]); } puts(""); return 0; } A2: /* EX5.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int variable, ctr = 0, nbr; You meant , not  while(ctr <= 5 && nbr != 99) { printf("Enter 6 values no decimal point or 99 to Quit "); scanf("%d",&nbr); if(nbr % 2 == 0) { variable[ctr] = nbr; ctr++; } else if(nbr == 99) { puts("Exiting program"); } else puts("number not an even number try again"); } puts(""); for(ctr = 0; ctr <= 5 && nbr != 99; ctr++) { printf("%d value %d\n",ctr +1,variable[ctr]); } return 0; } There is little to choose between the two methods. Neither is particularly robust. I marginally prefer the second, since it doesn't involve unnecessarily nested loops. -- Richard Heathfield : bi****@eton.powernet.co.uk "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton Nov 14 '05 #2

 P: n/a "darklight" wrote in message news:bs**********@sparta.btinternet.com... Q: write a program so that it excepts six even numbers or until the number 99 is entered please explain why one is better than the other, if that is the case. A1: /*EX6-1.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int array, x, number; for(x = 0; x < 6 && number != 99; x++) { puts("Enter an even value or 99 to quit: "); scanf("%d",&number); while(number % 2 == 1 && number != 99) { printf("%d is not even.\n try again\n",number); scanf("%d",&number); } array[x] = number; } for(x = 0; x < 6 && number != 99; x++) { printf("%d,\t",array[x]); } puts(""); return 0; } A2: /* EX5.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int variable, ctr = 0, nbr; while(ctr <= 5 && nbr != 99) { printf("Enter 6 values no decimal point or 99 to Quit "); scanf("%d",&nbr); if(nbr % 2 == 0) { variable[ctr] = nbr; ctr++; } else if(nbr == 99) { puts("Exiting program"); } else puts("number not an even number try again"); } puts(""); for(ctr = 0; ctr <= 5 && nbr != 99; ctr++) { printf("%d value %d\n",ctr +1,variable[ctr]); } return 0; } The first program is better since the second one will not work (and the first one will fail occasionally because you don't initialise number). However the structure of the second one is better simply because it is more readable. If you have to repeat the same code twice (like the scanf() call inside two different nested loops in the first example) then it is likely that you should rethink the structure of your loops. In both cases you should initialise the variable 'number' (or 'nbr') before you compare it to 99 in the loop since an uniitialised local variable will have an undefined (random) value. In the second one you have specified a size of  for your array whereas this should be  other wise your program might crash when you assign variable=nbr; Also in the second one if you enter 99 the for loop at the end will never print any values out (since nbr will alway be 99). Modify example 2 to something like this: #include int main(void) { int variable, ctr = 0, nbr=0; printf("Enter 6 values no decimal point or 99 to Quit\n"); for(ctr=0;ctr<6;ctr++) { scanf("%d",&nbr); if(nbr % 2 == 0) { variable[ctr] = nbr; } else if(nbr == 99) { variable[ctr]=99; puts("Exiting program"); break; } else { puts("number not an even number try again"); } } puts(""); for(ctr = 0; ctr < 6 && variable[ctr] != 99; ctr++) { printf("%d value %d\n",ctr +1,variable[ctr]); } return 0; } Sean Nov 14 '05 #3

 P: n/a Sean Kenwrick wrote: "darklight" wrote in message news:bs**********@sparta.btinternet.com... Q: write a program so that it excepts six even numbers or until the number 99 is entered please explain why one is better than the other, if that is the case. A1: /*EX6-1.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int array, x, number; for(x = 0; x < 6 && number != 99; x++) { puts("Enter an even value or 99 to quit: "); scanf("%d",&number); while(number % 2 == 1 && number != 99) { printf("%d is not even.\n try again\n",number); scanf("%d",&number); } array[x] = number; } for(x = 0; x < 6 && number != 99; x++) { printf("%d,\t",array[x]); } puts(""); return 0; } A2: /* EX5.C TO COUNT AND DISPLAY SIX EVEN NUMBERS*/ #include int main(void) { int variable, ctr = 0, nbr; while(ctr <= 5 && nbr != 99) { printf("Enter 6 values no decimal point or 99 to Quit "); scanf("%d",&nbr); if(nbr % 2 == 0) { variable[ctr] = nbr; ctr++; } else if(nbr == 99) { puts("Exiting program"); } else puts("number not an even number try again"); } puts(""); for(ctr = 0; ctr <= 5 && nbr != 99; ctr++) { printf("%d value %d\n",ctr +1,variable[ctr]); } return 0; } The first program is better since the second one will not work (and the first one will fail occasionally because you don't initialise number). However the structure of the second one is better simply because it is more readable. If you have to repeat the same code twice (like the scanf() call inside two different nested loops in the first example) then it is likely that you should rethink the structure of your loops. In both cases you should initialise the variable 'number' (or 'nbr') before you compare it to 99 in the loop since an uniitialised local variable will have an undefined (random) value. In the second one you have specified a size of  for your array whereas this should be  other wise your program might crash when you assign variable=nbr; Also in the second one if you enter 99 the for loop at the end will never print any values out (since nbr will alway be 99). Modify example 2 to something like this: #include int main(void) { int variable, ctr = 0, nbr=0; printf("Enter 6 values no decimal point or 99 to Quit\n"); for(ctr=0;ctr<6;ctr++) { scanf("%d",&nbr); if(nbr % 2 == 0) { variable[ctr] = nbr; } else if(nbr == 99) { variable[ctr]=99; puts("Exiting program"); break; } else { puts("number not an even number try again"); } } puts(""); for(ctr = 0; ctr < 6 && variable[ctr] != 99; ctr++) { printf("%d value %d\n",ctr +1,variable[ctr]); } return 0; } Sean Thanks for that that's what i wanted to do in the first place the first answer was taken from the book the second was mine both do work and give the same output and both have the same behaviour. IE: when 99 is entered both exit without displaying the even numbers entered Thanks again Nov 14 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 