Calculating mean

When I run my program, I should be able to calculate the mean, however I keep getting the incorrect value and I am baffled as to why this is. My program is much longer than this but I am hoping to just find the reason why I am getting an incorrect value. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  //Calculate the mean
double mean (int userArray[], int numberElements)
{
   double average = 0.0;
   int sum = 0;
   
   //Calculates the overall sum
   for (int i = 0; i <= numberElements; i++)
   {
       sum = (sum + userArray[i]);
       }//end for loop 
       
   (double)sum;
   (double)numberElements;
   
   average = (sum / numberElements);
   cout << "The MEAN of the array is: " << average << endl;
    
   return average; 
}//end mean

//*****************************************************************

int main()
{
    int numberElements, searchNumber, SIZEARRAY;
    int userArray[SIZEARRAY];
    
    cout << "Please enter the number of elements: ";
    cin >> numberElements;
    SIZEARRAY = (numberElements - 1);
    
    //This loop allows the user to enter the array elements
    for (int i = 0; i < numberElements; i++){
        cout << "Please enter number: ";
        cin >> userArray[i];
        }//end for loop
    
    //Prints the size of the array
    cout << endl << "The size of the array is: " << numberElements << endl;
    
    //Prints the array as entered by the user
    cout << ("Original Array as entered: ");  
    for (int i = 0; i < numberElements; i++) {
      cout << userArray[i] << " ";
    }

    //Finds the mean of the array
    mean(userArray, numberElements);
    
system("pause");
return 0;    
    
} //end main 
Double-check your mean function. Are you accessing only elements in your array? Or are you accessing something more?
I am only using my array and the number of array entrys. I've looked through this for about an hour and it keeps giving me bad values and I don't know why.
Double check line 8 in the code above. I think you'll find an error there.
Topic archived. No new replies allowed.