[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

where the mistake is

yuanshuaisd

11/3/2008 2:06:00 PM

#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
8 Answers

osmium

11/3/2008 2:16:00 PM

0

<yuanshuaisd@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;

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


AnonMail2005@gmail.com

11/3/2008 2:18:00 PM

0

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

Juan Antonio Zaratiegui Vallecillo

11/3/2008 2:57:00 PM

0

yuanshuaisd@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

Christopher Dearlove

11/3/2008 3:46:00 PM

0

<yuanshuaisd@yahoo.cn> wrote in message
news:0fdbc6bb-1c2f-411b-8eaf-90c322e931bf@v13g2000pro.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.


Juan Antonio Zaratiegui Vallecillo

11/3/2008 3:52:00 PM

0

Juan Antonio Zaratiegui Vallecillo wrote:
> yuanshuaisd@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

Juha Nieminen

11/3/2008 7:55:00 PM

0

yuanshuaisd@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?

Fred

11/3/2008 8:15:00 PM

0

On Nov 3, 11:55 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> 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

asterisc

11/4/2008 8:21:00 AM

0

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.