Thread: What's wrong with my Average Array program?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    What's wrong with my Average Array program?

    I made a program that calculates the average of an array, but I'm getting wrong answers. When I enter 10 9 8 7 6 5 4 3 2 1, the average is 4199211. Are there any errors in my program? Thanks

    Code:
    #include <stdio.h>
    double average (int ary[ ]);
    int main (void)
    {
        double ave;
        int ary[10];
        int numbers;
        printf("Enter 10 numbers: \n");
        for (numbers = 0; numbers < 10; numbers++)
            scanf("%d", &ary[numbers]);
        ave = average(ary);
        printf("Average : %d\n", average);
    
        system ("pause");
        return 0;
    }
    
    double average (int ary[ ])
    {
           int sum = 0;
           int numbers;
           for (numbers = 0; numbers < 10; ++numbers)
               sum += ary[numbers];
           return (sum / 10);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You printed a function pointer (average) instead of ave. In fact, the ave variable is not needed since you can print the result of average(ary) directly.

    By the way, instead of using integer division, you probably want to return (sum / 10.0).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    Thanks, but since I changed the average to ave. I get the average as 0. What is wrong with it now? lol. Thanks for all your help btw.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is a mistake which could cause the problem:
    Quote Originally Posted by special1zed View Post
    Code:
           for (numbers = 0; numbers < 10; ++numbers)
               sum += ary[numbers];
           return (sum / 10);
    }
    Because you pre-increment (++numbers), instead of post-incrementing (numbers++). So the real range was 1-10 and not 0-9.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by special1zed
    Thanks, but since I changed the average to ave. I get the average as 0. What is wrong with it now?
    Read my comment about integer division.

    Quote Originally Posted by MK27
    Because you pre-increment (++numbers), instead of post-incrementing (numbers++). So the real range was 1-10 and not 0-9.
    No, that is not the problem. Pre-increment and post-increment have the same net effect when used as stand alone expressions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    I changed that and I'm still getting 0 as the average. Thanks for the help though. I also changed the 10 to 10.0 and nothing changed.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What is your current code?

    EDIT:
    Oh wait, I notice that you are printing the average with a %d format specifier. It should be %f.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    That did the trick, thank you sooooo much again!

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    No, that is not the problem. Pre-increment and post-increment have the same net effect when used as stand alone expressions.
    Oh yeah...it's "for" that performs the operation. I guess that was too much presumption by me.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    33
    well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space..so cin n cout would be da better choice
    dis code can easily be implemented on cin and cout n we also dont think about "%f" or "%s"...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by salmanriaz
    well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space.
    In this case there is only integer input, so scanf is fine.

    Quote Originally Posted by salmanriaz
    dis code can easily be implemented on cin and cout n we also dont think about "%f" or "%s"...
    Not so easy, since this is in C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by salmanriaz View Post
    well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space...
    maybe you cannot. I still can
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by salmanriaz View Post
    well guys y u guys using printf and scanf i think it binds us in da limits because we can not read the string after space..so cin n cout would be da better choice
    dis code can easily be implemented on cin and cout n we also dont think about "%f" or "%s"...
    I didn't know Ali G could program.

    Sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM