Cant understand why formula wont work

I'm trying to write a program that returns the Artithmetic, Geometric, and Harmonic mean of 5 intgers input by a user.

Arithmetic and Harmonic function properly, but I cannot seem to get the correct answer for Geometric mean. I have searched online and am near certain my code is proper for what to use with geometric mean. I cant figure out why this formula isn't returning the correct value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int a, b, c, d, e;
    cout << "Enter five numbers:" << endl;
    cin >> a >> b >> c >> d >> e;
    cout << endl;

    double arMean = (a + b + c + d + e) / 5;
    double N = 5.0;
    double X = (a * b * c * d * e);
    double geoMean = pow(X,(1/N));
    double harMean =  (1.0 / ((1.0/a + 1.0/b + 1.0/c + 1.0/d + 1.0/e) / 5));

    cout << "Result:" << endl << "Data:" << endl << a << endl << b << endl << c << endl
    << d << endl << e << endl;
    cout << "Arithmetic Mean    = " << arMean << endl;
    cout << "Geometric  Mean    = " << fixed << geoMean << endl;
    cout << "Harmonic  Mean     = " << harMean << endl;


Any help is greatly appreciated as I'm new to coding and really don't have a clue as to the issue. Thanks
When I run the program everything complies fine, but when I enter the values my geometric mean says "-1.#IND00". I'm using the values 85, 95, 78, 43, and 100 as test values.

I tried making 1 a double and got the same result.

Is it some type of overflow when the ints are all multiplied together? I'm just spitballing because when I request a return of just abcde multiplied its a negative value when it should be positive.
Last edited on
Yes my problem is still persisting.

It seems when I multiply my test values for a b c d and e the value it assigns to X is negative. I dont understand why when 100*85*95*78*43 is 2708355000. It's assigning -1586612296.00 to X when using those test values.
Last edited on
Here is the output of the program. I added standard deviation which works fine. But as you can see the geometric mean is returning nonsense. It seems to function properly with smaller values, but breaks with larger inputs.

Result:
Data:
85
95
78
43
100
Arithmetic Mean = 80.20
Geometric Mean = -1.#J
Harmonic Mean = 73.13
Standard Deviation = 20.11

and here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int a, b, c, d, e;
    cout << "Enter five numbers:" << endl;
    cin >> a >> b >> c >> d >> e;
    cout << endl;

    double arMean = (a + b + c + d + e) / 5.0;
    double N = 5.0;
    double X = (a * b * c * d * e);
    double geoMean = (pow(X,(1.0/N)));
    double harMean =  (1.0 / ((1.0/a + 1.0/b + 1.0/c + 1.0/d + 1.0/e) / 5.0));
    double stdDev = sqrt(
                        ((pow((a - arMean),2) +
                          pow((b - arMean),2) +
                          pow((c - arMean),2) +
                          pow((d - arMean),2) +
                          pow((e - arMean),2)) / 5)
                         );

    cout << "Result:" << endl << "Data:" << endl << a << endl << b << endl << c << endl
    << d << endl << e << endl;
    cout << "Arithmetic Mean    = " << fixed << setprecision(2) << arMean << endl;
    cout << "Geometric  Mean    = " << fixed << setprecision(2) << geoMean << endl;
    cout << "Harmonic  Mean     = " << fixed << setprecision(2) << harMean << endl;
    cout << "Standard Deviation = " << fixed << setprecision(2) << stdDev << endl;
Last edited on
YES!!!! Thank you!
Topic archived. No new replies allowed.