Which is better

D

darklight

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<stdio.h>

int main(void)
{
int array[6], 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<stdio.h>

int main(void)
{
int variable[5], 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;
}
 
R

Richard Heathfield

<posted & mailed>
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<stdio.h>

int main(void)
{
int array[6], 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<stdio.h>

int main(void)
{
int variable[5], ctr = 0, nbr;

You meant [6], not [5]
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.
 
S

Sean Kenwrick

darklight said:
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<stdio.h>

int main(void)
{
int array[6], 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<stdio.h>

int main(void)
{
int variable[5], 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 [5] for your array whereas
this should be [6] other wise your program might crash when you assign
variable[5]=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<stdio.h>

int main(void) {
int variable[6], 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
 
D

darklight

Sean said:
darklight said:
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<stdio.h>

int main(void)
{
int array[6], 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<stdio.h>

int main(void)
{
int variable[5], 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 [5] for your array whereas
this should be [6] other wise your program might crash when you assign
variable[5]=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<stdio.h>

int main(void) {
int variable[6], 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top