473,371 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,371 software developers and data experts.

Which is better

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;
}

Nov 14 '05 #1
3 1609
<posted & mailed>

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

--
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

"darklight" <am**@netscepe.net> 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<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
Nov 14 '05 #3
Sean Kenwrick wrote:

"darklight" <am**@netscepe.net> 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<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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amit D.Shinde | last post by:
Hello Experts.. I need some help regarding cookies and session objects and also global.asa file I am creating one cookie when a user logs in on my website. The cookie stores the login name of...
6
by: lastusernameleft | last post by:
i've been researching this issue for a while and can't come to any conclusive answer, mostly it seems to be a preference over syntax, some saying c# is elegant while vb is clunky, or that c# is...
1
by: Markus Rebbert | last post by:
Hi list, i got an postgresql installation on linux (debian) with the htree partitions: 1- system 2- postgresql data files 3- postgresql WAL logs(pg_xferlog) Our standard file system is...
4
by: Ed Davis | last post by:
I'm trying to decide which of the following programming styles is better, as in easier to understand, and thus easier to maintain. Keep in mind that for posting purposes, this is a greatly...
9
by: Robert Lario | last post by:
C# verses VB.Net Which Way to go. I am sure this issues has come up before. Please direct me to any good articles that cover this issue. Ideally some neutral assessment.
22
by: smartwolf agassi via DotNetMonster.com | last post by:
I'm a C# language learner. I want to know which IDE is better for C# programing, Borland C#Builder or VS.net 2003? -- Message posted via http://www.dotnetmonster.com
2
by: monkeydragon | last post by:
Which is better, using ReadFile/WriteFile or use fstream?
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.