454,504 Members | 1,724 Online
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 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
8 Replies

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

 P: n/a Juan Antonio Zaratiegui Vallecillo wrote: yu*********@yahoo.cn wrote: >#includeint 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 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 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 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.