Thread: Average of an Array of integer

  1. #1
    Registered User
    Join Date
    Jul 2008

    Average of an Array of integer

    Something weird is happening with this problem. I am trying to take an array of integers and get its average, but i keep getting this error. Its probably something small i am missing. Can anyone see it?

    theres code in there for more that i made not visible

    int average(int array[] )
        int sum=0;
        for (int i=0 ; i <= arraysize; i++)   
        sum = sum+ array[i];
        float avg = sum/arraysize;
        return avg;
    #include "variables_for_max_and_min.h"
    #include "average.h"
    //#include "min.h"
    //#include "max.h"
    using namespace std;
    int main() {
    //cout << minimumArray(arraysize, 0, (arraysize - 1)) << endl;
    //cout << maxArray(arraysize, 0, (arraysize - 1)) << endl;
    cout << average(anArray[arraysize]);
        return 0;
    Last edited by Salem; 08-09-2008 at 12:34 PM. Reason: Fixed code tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > #include "variables_for_max_and_min.h"
    Like we have any idea what you've stuffed in here.

    > for (int i=0 ; i <= arraysize; i++)
    Array subscripts run from 0 to N-1
    So we usually say < N

    > cout << average(anArray[arraysize]);
    To pass an array, just use the name, like
    cout << average(anArray);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2008
    The problem is that the function has no way of knowing how big your array is. The proper way to pass in an array to a function is
    int array[10];
    int array_size = 10;
    void foo( int arr[], int size )
          /* implementation */
    int main()
    Second problem, arraysize is not defined in your function. By putting
    you aren't passing the size into the function. In fact the only thing that is passed is the
    pointer( memory address ) of the first element of the array.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Quote Originally Posted by Emeighty View Post
    i keep getting this error.
    What error? How can you says this error without actually showing us what it is? Furthurmore, surely it was not only one error you were getting.

    Header files are not for arbitrarily putting code into. If other cpp files don't need to know about every line of code in that function then it doesn't belong in the header file. At most, just the function prototype would be appropriate in this case.

    In C++ we use std::accumulate
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to multidimensional arrays
    By Luciferek in forum C++ Programming
    Replies: 37
    Last Post: 10-02-2008, 12:57 PM
  2. Declaring an array with an integer
    By rpalmer in forum C Programming
    Replies: 4
    Last Post: 04-30-2007, 12:39 AM
  3. Convert Integer into an array
    By ubernos in forum C Programming
    Replies: 2
    Last Post: 11-08-2005, 10:30 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM