473,320 Members | 1,982 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,320 software developers and data experts.

re Which is better


Q: write a program so that excepts six even numbers
or until the number 99 is entered.
I should of add display only the even numbers entered
sorry!!
the two programs that were wrote done this:

First posted on 21/12/03

one of the replys Wrote:

#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;
}
__________________________________________________ _______________
When i run program i get
the output

input 43,24,46,78,99

out put
1 value 1073934432
2 value 24
3 value 48
4 value 78
____________________
input 24,43,46,78,99

output
1 value 24
2 value 0
3 value 46
4 value 78
____________________
input 24,46,43,78,99

output
1 value 2
2 value 46
3 value -1073744904
4 value 78

And when i enter four even numbers and two odd numbers it displays
the four even numbers and garbage for the two odd numbers.
What makes me laugh is i am sure when i ran it for the first time
it worked but now i get the output as above.

Could one tell me what is going on?
Nov 14 '05 #1
4 1454
nrk
<posted & mailed>

darklight wrote:

Q: write a program so that excepts six even numbers
or until the number 99 is entered.
I should of add display only the even numbers entered
sorry!!
the two programs that were wrote done this:

First posted on 21/12/03

one of the replys Wrote:

#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++) {
Note that ctr is always incremented, even if the number entered was odd, or
worse, if the input was not even a number.
scanf("%d",&nbr);

Ideally, you should check the return code of scanf to see if the input was
valid or not. Otherwise, you'll have to eat up the line from the input.
For instance:
int rc;

nbr = 0; /* always initialize nbr */
rc = scanf("%d", &nbr);
if ( rc == 0 ) {
/* invalid input, eat away the line */
scanf("%*[^\n]") != EOF && getchar();
}
else if ( !feof(stdin) ) getchar();

if(nbr % 2 == 0) {
variable[ctr] = nbr;
}
If number was even, variable[ctr] will contain that number.
else if(nbr == 99) {
variable[ctr]=99;
puts("Exiting program");
break;
}
If number was 99, variable[ctr] will contail that number.

However, if number was odd, ctr will be incremented without initializing
variable[ctr] to anything valid!! So, this program results in undefined
behavior as you end up using an uninitialized int. Here's the fix:

else
--ctr; /* dec. ctr so that the for increment is nullified */
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;
}
__________________________________________________ _______________
When i run program i get
the output

input 43,24,46,78,99

out put
1 value 1073934432
2 value 24
3 value 48
4 value 78
____________________
input 24,43,46,78,99

output
1 value 24
2 value 0
3 value 46
4 value 78
____________________
input 24,46,43,78,99

output
1 value 2
2 value 46
3 value -1073744904
4 value 78

And when i enter four even numbers and two odd numbers it displays
the four even numbers and garbage for the two odd numbers.
What makes me laugh is i am sure when i ran it for the first time
it worked but now i get the output as above.

Same problem as above.

HTH,
-nrk.
Could one tell me what is going on?


Nov 14 '05 #2

"darklight" <am**@netscepe.net> schrieb im Newsbeitrag
news:bs**********@hercules.btinternet.com...

Q: write a program so that excepts six even numbers
or until the number 99 is entered.
I should of add display only the even numbers entered
sorry!!
the two programs that were wrote done this:

First posted on 21/12/03

one of the replys Wrote:

#include<stdio.h>

int main(void) {
int variable[6], ctr = 0, nbr=0;
Here you have an array of 6 _uninitialized_ int

printf("Enter 6 values no decimal point or 99 to Quit\n");

for(ctr=0;ctr<6;ctr++) {
Your counter is incremented at each iteration.
scanf("%d",&nbr);
Ask Dan Pop about the safe use of scanf() or use fgets() and do the
conversion yourself :)
What if the user enters garbage?
OK, let's assume she/he entered a valid number...
if(nbr % 2 == 0) {
variable[ctr] = nbr;
Fine, here you assign a valid int into the int at the current position of
your array
}
else if(nbr == 99) {
variable[ctr]=99;
puts("Exiting program");
Same here..
break;
}
else {
puts("number not an even number try again");
But not here. However you increment ctr, so you leave an uninitialized
member in your array, which in your case happens to be a strange number, but
could as well crash your program later in the call to printf()
}
}
puts("");

for(ctr = 0; ctr < 6 && variable[ctr] != 99; ctr++)
{
printf("%d value %d\n",ctr +1,variable[ctr]);
}
return 0;
}
__________________________________________________ _______________
When i run program i get
the output

input 43,24,46,78,99

out put
1 value 1073934432
This is the garbage in the uninitialized int variable[0]
2 value 24
3 value 48
I'd expect 46 here...
4 value 78
____________________
input 24,43,46,78,99

output
1 value 24
2 value 0

This is the garbage in the uninitialized int variable[1]
3 value 46
4 value 78
____________________
input 24,46,43,78,99

output
1 value 2
24 maybe?
2 value 46
3 value -1073744904

This is the garbage in the uninitialized int variable[2]
4 value 78

And when i enter four even numbers and two odd numbers it displays
the four even numbers and garbage for the two odd numbers.
See above.
What makes me laugh is i am sure when i ran it for the first time
it worked but now i get the output as above.


Maybe. One of the outcomes of UB is "works as expected, but only on monday
1st when the moon is full"... :)

Merry christmas to all of you
Robert
Nov 14 '05 #3
Groovy hepcat darklight was jivin' on Thu, 25 Dec 2003 10:58:35 +0000
(UTC) in comp.lang.c.
re Which is better's a cool scene! Dig it!
Q: write a program so that excepts six even numbers
Goddamnit! Pay attention to what you were told. You were told before
that there is no such word as "excepts", and that what you probably
mean is "accepts". People who don't listen when they're asking for
help are very rude and annoying!
Why did you begin a new thread to discuss something from an existing
thread? Don't do that! It is very irritating.
or until the number 99 is entered.
I should of add display only the even numbers entered
sorry!!
the two programs that were wrote done this:


The above makes no sense.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #4
Peter Shaggy Haywood wrote:

Groovy hepcat darklight was jivin' on Thu, 25 Dec 2003 10:58:35 +0000
(UTC) in comp.lang.c.
re Which is better's a cool scene! Dig it!
Q: write a program so that excepts six even numbers


Goddamnit! Pay attention to what you were told. You were told before
that there is no such word as "excepts", and that what you probably
mean is "accepts".


What's the "anithing" spelling, all about ?

http://groups.google.com/groups?hl=e...Dcomp.lang.c.*

--
pete
Nov 14 '05 #5

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
3
by: Muhd | last post by:
<usualDisclaimer>Please forgive me if this is in the wrong group, and if so, what is the right group.</usualDisclaimer> Let me start off by first saying im a newb. Ok, with that out of the way I...
24
by: Faith Dorell | last post by:
I really don´t like C.You can write better programs in BASIC than in C, if you don´t like this language. I don´t understand how C became so popular, although much better programming languages...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
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!!!!!
22
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at...
19
by: Alexandre Badez | last post by:
I'm just wondering, if I could write a in a "better" way this code lMandatory = lOptional = for arg in cls.dArguments: if arg is True: lMandatory.append(arg) else: lOptional.append(arg)...
23
by: mike3 | last post by:
Hi. (posted to both newsgroups since I was not sure of which would be appropriate for this question or how specific to the given language it is. If one of them is inappropriate, just don't send...
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...
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.