You are using a version without Ads of this website. Please, consider donating:

### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` `````` //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.