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

where the mistake is

P: n/a
#include<stdio.h>
int main()
{
int n=1;
int average;
int num;
int sum;
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);

sum = sum + num;
}

average = sum / n;
printf("average is %d",average);

return 0;
}
why the program end in a wrong result
Nov 3 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
<yu*********@yahoo.cnwrote:
#include<stdio.h>
int main()
{
int n=1;
int average;
int num;
int sum;
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);

sum = sum + num;
}

average = sum / n;
average has been declared as an integer. In general, there are decimal
results in averages so change average to a variable of type double. There
may be other problems as well, I didn't look for them.
printf("average is %d",average);

return 0;
}
why the program end in a wrong result

Nov 3 '08 #2

P: n/a
On Nov 3, 9:05*am, yuanshua...@yahoo.cn wrote:
#include<stdio.h>
int main()
{
* * * * int n=1;
* * * * int average;
* * * * int num;
* * * * int sum;
* * * * for(n=1;num != 9999;n++){
* * * * printf("Enter a integer:\n");
* * * * * * * * scanf("%d",&num);

* * * * * * * * sum = sum + num;
* * * * }

* * average = sum / n;
* * * * printf("average is %d",average);

* * * * return 0;}

why the program end in a wrong result
For one, the variables num and sum are not initialized.

HTH
Nov 3 '08 #3

P: n/a
yu*********@yahoo.cn wrote:
#include<stdio.h>
int main()
{
int n=1;
'num' initialised to 1, and reinitialized later in the loop, you may
just intialise it once
int average;
int num;
int sum;
'sum' is not initialised
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);
'scanf' return value is not checked for errors
>
sum = sum + num;
}

average = sum / n;
'average' is integer, so the result will be such that:
result<= real average< result+1
printf("average is %d",average);

return 0;
}
why the program end in a wrong result
When you add anything to an undefined value (not initialised) the result
is undefined behaviour. That is, you may even get the result you
expected. Now. But not later, specially if you are showing your program
to another party.

Best regards,

Zara
Nov 3 '08 #4

P: n/a
<yu*********@yahoo.cnwrote in message
news:0f**********************************@v13g2000 pro.googlegroups.com...
for(n=1;num != 9999;n++){
That's almost certainly not the loop you want. Apart from the confusion
of having num in the test, even if that is the test you want, note that you
add numbers until you enter a 9999 - but do add in that 9999.
Nov 3 '08 #5

P: n/a
Juan Antonio Zaratiegui Vallecillo wrote:
yu*********@yahoo.cn wrote:
>#include<stdio.h>
int main()
{
int n=1;

'num' initialised to 1, and reinitialized later in the loop, you may
just intialise it once
No, this comment is wrong.
>
> int average;
int num;
int sum;

'sum' is not initialised
> for(n=1;num != 9999;n++){
Shouldn't it be:
while ( num != 9999 ) {
?
> printf("Enter a integer:\n");
scanf("%d",&num);

'scanf' return value is not checked for errors
>>
sum = sum + num;
}

average = sum / n;

'average' is integer, so the result will be such that:
result<= real average< result+1
> printf("average is %d",average);

return 0;
}
why the program end in a wrong result

When you add anything to an undefined value (not initialised) the result
is undefined behaviour. That is, you may even get the result you
expected. Now. But not later, specially if you are showing your program
to another party.

Best regards,

Zara
Nov 3 '08 #6

P: n/a
yu*********@yahoo.cn wrote:
#include<stdio.h>
int main()
{
int n=1;
int average;
int num;
int sum;
for(n=1;num != 9999;n++){
printf("Enter a integer:\n");
scanf("%d",&num);

sum = sum + num;
}

average = sum / n;
printf("average is %d",average);

return 0;
}
why the program end in a wrong result
Because to terminate the program you are entering 9999, which is added
to the sum?
Nov 3 '08 #7

P: n/a
On Nov 3, 11:55*am, Juha Nieminen <nos...@thanks.invalidwrote:
yuanshua...@yahoo.cn wrote:
#include<stdio.h>
int main()
{
* *int n=1;
* *int average;
* *int num;
* *int sum;
* *for(n=1;num != 9999;n++){
* *printf("Enter a integer:\n");
* * * * * *scanf("%d",&num);
* * * * * *sum = sum + num;
* *}
* * average = sum / n;
* *printf("average is %d",average);
* *return 0;
}
why the program end in a wrong result

* Because to terminate the program you are entering 9999, which is added
to the sum?- Hide quoted text -

- Show quoted text -
Also, n is incorrect when the loop terminates.
--
Fred
Nov 3 '08 #8

P: n/a
On Nov 3, 4:05*pm, yuanshua...@yahoo.cn wrote:
#include<stdio.h>
int main()
{
* * * * int n=1;
* * * * int average;
* * * * int num;
* * * * int sum;
* * * * for(n=1;num != 9999;n++){
Reading 'num' without being initialized yield undefined behavior.
Nov 4 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.