where the mistake is

Discussion in 'C++' started by yuanshuaisd@yahoo.cn, Nov 3, 2008.

  1. Guest

    #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, 2008
    #1
    1. Advertising

  2. osmium Guest

    <> 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
    osmium, Nov 3, 2008
    #2
    1. Advertising

  3. Guest

    On Nov 3, 9:05 am, 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, 2008
    #3
  4. 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
    Juan Antonio Zaratiegui Vallecillo, Nov 3, 2008
    #4
  5. <> wrote in message
    news:...
    > 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.
    Christopher Dearlove, Nov 3, 2008
    #5
  6. Juan Antonio Zaratiegui Vallecillo wrote:
    > 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
    Juan Antonio Zaratiegui Vallecillo, Nov 3, 2008
    #6
  7. 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?
    Juha Nieminen, Nov 3, 2008
    #7
  8. Fred Guest

    On Nov 3, 11:55 am, Juha Nieminen <> wrote:
    > 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
    Fred, Nov 3, 2008
    #8
  9. asterisc Guest

    On Nov 3, 4:05 pm, 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.
    asterisc, Nov 4, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \(beta-\) Frank Nitzsche

    where is the mistake?

    \(beta-\) Frank Nitzsche, Jun 25, 2004, in forum: VHDL
    Replies:
    4
    Views:
    530
  2. Jesse

    Beginner Mistake

    Jesse, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    359
    Jesse
    Jul 22, 2003
  3. Joerg Jooss
    Replies:
    1
    Views:
    401
    DotNetJunkies User
    May 22, 2004
  4. John
    Replies:
    6
    Views:
    644
  5. Leon

    Does anyone see my mistake?

    Leon, Nov 10, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    331
Loading...

Share This Page