where the mistake is

Y

yuanshuaisd

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

osmium

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

AnonMail2005

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

Juan Antonio Zaratiegui Vallecillo

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

Christopher Dearlove

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

Juan Antonio Zaratiegui Vallecillo

Juan said:
'num' initialised to 1, and reinitialized later in the loop, you may
just intialise it once

No, this comment is wrong.
'sum' is not initialised

Shouldn't it be:
while ( num != 9999 ) {
?
 
J

Juha Nieminen

#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?
 
F

Fred

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

asterisc

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top