Average of an Array of integer

This is a discussion on Average of an Array of integer within the C++ Programming forums, part of the General Programming Boards category; Something weird is happening with this problem. I am trying to take an array of integers and get its average, ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    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

    average.h
    Code:
    int average(int array[] )
    {
        
        int sum=0;
        
        for (int i=0 ; i <= arraysize; i++)   
        {
        sum = sum+ array[i];
        
        float avg = sum/arraysize;
        
        return avg;
    }
           
    }
        
    assignement.cpp
    
    
    
    #include<iostream>
    #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]);
    
        
        system("Pause");
        return 0;
        
    }
    Last edited by Salem; 08-09-2008 at 12:34 PM. Reason: Fixed code tags

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > #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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    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
    Code:
    int array[10];
    int array_size = 10;
    
    void foo( int arr[], int size )
    {
          /* implementation */
    }
    
    
    int main()
    {
         foo(array,array_size);
    }
    Second problem, arraysize is not defined in your function. By putting
    Code:
    average(anArray[arraysize]);
    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
    Location
    New Zealand
    Posts
    6,304
    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, 09: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

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