### New to C++ and need help!!

closed account (zqXLy60M)
I cannot find out what is wrong with my meanFunction. It keeps returning a weird set of numbers. PLEASE HELP!

Code Posted:

#include <iostream>
#include <cmath>
using namespace std;

float meanFunction(float *arr, int count);
float medianTotal(float *arr, int count);

int main() {
int count = 0, go_on = 0;
float num[100], mean, median, *arr = num;
tryAgain: // Statement Label
cout << "This program will calculate the mean, median, mode, and standard deviation. " << endl;
cout << "Enter the amount of values that you will select (must be 100 or less), the program will self terminate once hitting that value." << endl;
cin >> go_on;
if (go_on > 100) {
goto tryAgain;
}
cout << "You have entered " << go_on << " values." << endl;
do {
cout << "Enter a number: ";
cin >> num[count];
count++;

median = medianTotal(arr, count);

}
while (count < go_on);
mean = meanFunction(arr,count);
cout << "The Mean is: " << mean << endl;
cout << "The Median is: " << median << endl;

return 0;
}

float meanFunction(float *arr, int count) {
float sum = 0;
for (int i = 0; i <= count; i++) {
sum += arr[i];
}
return (sum / count);
}

float medianTotal(float *arr, int count){
int middle = (count / 2);
float average;
if ((count % 2) == 0){
average = ((arr[middle - 1] + arr[middle]) / 2);
}
else {
average = (arr[middle]);
}
return average;
}
I'm just putting your program in code format because I am bored. :)

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include using namespace std; float meanFunction(float *arr, int count); float medianTotal(float *arr, int count); int main() { int count = 0, go_on = 0; float num[100], mean, median, *arr = num; tryAgain: // Statement Label cout << "This program will calculate the mean, median, mode, and standard deviation. " << endl; cout << "Enter the amount of values that you will select (must be 100 or less), the program will self terminate once hitting that value." << endl; cin >> go_on; if (go_on > 100) { cout << "Please follow the directions!" << endl;; goto tryAgain; } cout << "You have entered " << go_on << " values." << endl; do { cout << "Enter a number: "; cin >> num[count]; count++; median = medianTotal(arr, count); } while (count < go_on); mean = meanFunction(arr,count); cout << "The Mean is: " << mean << endl; cout << "The Median is: " << median << endl; return 0; } float meanFunction(float *arr, int count) { float sum = 0; for (int i = 0; i <= count; i++) { sum += arr[i]; } return (sum / count); } float medianTotal(float *arr, int count){ int middle = (count / 2); float average; if ((count % 2) == 0){ average = ((arr[middle - 1] + arr[middle]) / 2); } else { average = (arr[middle]); } return average; }``````
Line 39 your loop goes too far.
It should be
 `` `` ``for (int i = 0; i < count; i++)``

or
 `` `` ``for (int i = 0; i <= count-1; i++)``

but not
 `` `` ``for (int i = 0; i <= count; i++)``
closed account (zqXLy60M)
That fixed the issue... Thank you Toum!
Topic archived. No new replies allowed.