where the mistake is

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yuanshuaisd@yahoo.cn

    where the mistake is

    #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
  • osmium

    #2
    Re: where the mistake is

    <yuanshuaisd@ya hoo.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

    Comment

    • AnonMail2005@gmail.com

      #3
      Re: where the mistake is

      On Nov 3, 9:05 am, yuanshua...@yah oo.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

      Comment

      • Juan Antonio Zaratiegui Vallecillo

        #4
        Re: where the mistake is

        yuanshuaisd@yah oo.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

        Comment

        • Christopher Dearlove

          #5
          Re: where the mistake is

          <yuanshuaisd@ya hoo.cnwrote in message
          news:0fdbc6bb-1c2f-411b-8eaf-90c322e931bf@v1 3g2000pro.googl egroups.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.


          Comment

          • Juan Antonio Zaratiegui Vallecillo

            #6
            Re: where the mistake is

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

            Comment

            • Juha Nieminen

              #7
              Re: where the mistake is

              yuanshuaisd@yah oo.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?

              Comment

              • Fred

                #8
                Re: where the mistake is

                On Nov 3, 11:55 am, Juha Nieminen <nos...@thanks. invalidwrote:
                yuanshua...@yah oo.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

                Comment

                • asterisc

                  #9
                  Re: where the mistake is

                  On Nov 3, 4:05 pm, yuanshua...@yah oo.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.

                  Comment

                  Working...