C++ Homework Question

I'm doing an assignment for my C++ class and I've run into a problem writing a simple program that finds various types of averages given user input for 5 integer values. For some reason, my program will crash when given integer values from user than exceed 1 digit and I can't figure out why.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>                   //setw(), fixed, setprecision()
#include <cmath>                     //pow(), sqrt()
using namespace std;

int main()

{

  //Data Abstraction

    int    number_1,                 //User Input
           number_2,                 //User Input
           number_3,                 //User Input
           number_4,                 //User Input
           number_5;                 //User Input

    double arithmetic,               //Calculated Values
           geometric,                //Calculated Values
           harmonic,                 //Calculated Values
           standard;                 //Calculated Values


  //Input

    cout << "Enter five numbers:" <<endl;
    cin  >> number_1
         >> number_2
         >> number_3
         >> number_4
         >> number_5;


  //Process

    arithmetic  =  ( (number_1)  +   (number_2)
                   + (number_3)  +   (number_4) + (number_5)) / (5);

    geometric   = pow (((number_1) + (number_2)
                      + (number_3) + (number_4)  + (number_5)), (1/5));

    harmonic    = (5) / ((1 / number_1) + (1 / number_2)
                      + (1 / number_3)  + (1 / number_4) + (1 / number_5));

    standard    = sqrt( pow((number_1 - arithmetic), 2)
                      + pow((number_2 - arithmetic), 2)
                      + pow((number_3 - arithmetic), 2)
                      + pow((number_4 - arithmetic), 2)
                      + pow((number_5 - arithmetic), 2));

  //Output

    cout << "Result:" << endl
                      << endl;
    cout << "Data:"   << endl;

    cout << number_1  << endl;
    cout << number_2  << endl;
    cout << number_1  << endl;
    cout << number_4  << endl;
    cout << number_5  << endl
                      << endl;

    cout << fixed << showpoint << setprecision(2);

    cout << "Arithmetic mean" << setw(11)   << "= " << arithmetic << endl;
    cout << "Geometric  mean" << setw(11)   << "= " << geometric  << endl;
    cout << "Harmonic   mean" << setw(11)   << "= " << harmonic   << endl;
    cout << "Standard Deviation\t"          << "= " << standard   << endl;


    return 0;
}
Last edited on
Avoid integer division for these computations. (1/5 == 0 and 1.0/5 == 0.2)

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
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    int  n1, n2, n3, n4, n5 ;
    std::cout << "Enter five numbers: "  ;
    std::cin  >> n1 >> n2 >> n3 >> n4 >> n5 ;

    const double arithmetic_mean =  (n1+n2+n3+n4+n5) / 5.0 ;

    // note: multiply the numbers
    const double geometric_mean = std::pow( n1*n2*n3*n4*n5, (1.0/5) ) ;

    const double a = arithmetic_mean ;
    const double standard_deviation = std::sqrt( (n1-a)*(n1-a) + (n2-a)*(n2-a) + (n3-a)*(n3-a) +
                                                 (n4-a)*(n4-a) + (n5-a)*(n5-a) ) ;

    std::cout << "Result:\n\nData:\n"
              << n1 << ", " << n2 << ", " << n3 << ", " << n4 << ", " << n5 << "\n\n"
              << std::fixed << std::setprecision(2)
              << "   Arithmetic mean = " << std::setw(11) << arithmetic_mean << '\n'
              << "   Geometric  mean = " << std::setw(11) << geometric_mean  << '\n' ;

    if( n1>0 && n2>0 && n3>0 && n4>0 && n5>0  ) // harmonic mean is defined only for positive numbers
    {
        const double harmonic_mean = 5.0 / ( (1.0/n1) + (1.0/n2) + (1.0/n3) + (1.0/n4) + (1.0/n5) ) ;
        std::cout << "   Harmonic   mean = " << std::setw(11) << harmonic_mean   << '\n' ;
    }

    std::cout << "Standard Deviation = " << std::setw(11) << standard_deviation << '\n';
}
Thanks so much!
Topic archived. No new replies allowed.