What's wrong with my Average Array program?

This is a discussion on What's wrong with my Average Array program? within the C Programming forums, part of the General Programming Boards category; I made a program that calculates the average of an array, but I'm getting wrong answers. When I enter 10 ...

  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
    22,308
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,308
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,308
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,308
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21