473,721 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",&num ber);
while(number % 2 == 1 && number != 99)
{
printf("%d is not even.\n try again\n",number );
scanf("%d",&num ber);
}
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 1625
<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",&num ber);
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",&num ber);
}
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.pow ernet.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.btint ernet.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",&num ber);
while(number % 2 == 1 && number != 99)
{
printf("%d is not even.\n try again\n",number );
scanf("%d",&num ber);
}
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.btint ernet.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",&num ber);
while(number % 2 == 1 && number != 99)
{
printf("%d is not even.\n try again\n",number );
scanf("%d",&num ber);
}
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
3344
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 the user. I want that cookie should get deleted when user closes the browser without signing out. I think it is done in global.asa file . But i don;t know how to do it?
6
2157
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 geeky while vb is more naturally legible. there dont seem to be many capabilities in one over the other, at least with whidbey, so basically, is there anything c# can do that vb can't?
1
3477
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 ext3. Now i heard, xfs is better for the data files.
4
2938
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 simplified example. The goal is to parse and build an AST for a print statement, returning the base-node of the AST built. print ::= "print" expr | string ("," expr | string )*
9
288
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
2232
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
2895
by: monkeydragon | last post by:
Which is better, using ReadFile/WriteFile or use fstream?
33
2578
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
4942
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 David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I cannot seem to find many retailers around here that still carry Visual Studio 2003, and some were a...
20
3083
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 like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.