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) {
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;
}
I'm just putting your program in code format because I am bored. :)

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
55
#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) {
        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.