By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

re Which is better

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.