Need help with Standard Deviation and Arrays please?

So I have searched extensively... I am not finding much.

I had to build a calculator program, I did that pretty easily. But now I'm stuck on the second part of this assignment, and that's including average and standard deviation into the calculator.

What I'm struggling with is that for average and standard deviation its suppose to allow up to 100 variables, and the user inputs how many variables there will be before they put in their variables. I am stuck on how I will do this and then be able to change those variables later to allow for the standard deviation equation to work...

Any help would be greatly appreciated, just need a point in the right direction.

Average and Standard Deviation deviation are case 8 and case 9, there is nothing there because everything I tried failed.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<iostream>
#include<cmath>
using namespace std;

//---------- Function Prototypes -----------
void print_menu();
float get_value();
float divide(float,float);
float multiply(float,float);
float vrc(float,float);
float vs(float);
float cube(float);

//--------------  Main -------------------

int main()
{
        float operand1, operand2, answer;
        int choice, valid_choice;
        do{
                print_menu();
                cin >> choice;
                valid_choice = 1; // assume choice is valid
                switch(choice){
                case 0:
                        break;
                case 1:
                        operand1 = get_value();
                        operand2 = get_value();
                        answer = operand1 + operand2;
                        break;
                case 2:
                        operand1 = get_value();
                        operand2 = get_value();
                        answer = operand1 - operand2;
                        break;
                case 3:
                        operand1 = get_value();
                        operand2 = get_value();
                        answer = multiply(operand1,operand2);
                        break;
                case 4:
                        operand1 = get_value();
                        operand2 = get_value();
                        answer = divide(operand1,operand2);
                        break;
                case 5:
                        operand1 = get_value();
                        operand2 = get_value();
                        answer = vrc(operand1, operand2);
                        break;
                case 6:
                        operand1 = get_value();
                        answer = vs(operand1);
                        break;
                case 7:
                        operand1 = get_value();
                        answer = cube(operand1);
                        break;
                case 8:
                        cout << "Enter how many variables up to 100 : ";
                        operand1 = get_value();
                case 9:
                        cout << "Enter how many variables up to 100 : ";
                default:
                        valid_choice = 0;
                        cout << "Invalid Choice." << endl;
                }
                if(valid_choice){
                        cout << endl << "Answer = " << answer << endl;
                }
        }while(choice != 0);
        return 0;
}

//--------------  Functions -------------------
float divide(float dividend, float divisor){
        if(divisor == 0)
                return 0;  // avoids divide by zero errors
        else
                return (dividend/divisor);
}

float multiply(float multi, float multiplier){

                return (multi*multiplier);

}

float vrc(float r, float h) {

                return (3.14159 * (pow(r,2)) * h);
}

float vs(float r) {

                return (divide(4,3)) * (3.14159) * (pow(r,3));
}


float cube(float s) {

                return (pow(s,3));
}


float get_value(){
        float temp_value;
        cout << "Enter a value: ";
        cin >> temp_value;
        return temp_value;
}

void print_menu(){
        cout << endl;
        cout << "Add (1)" << endl;
        cout << "Subtract (2)" << endl;
        cout << "Multiply (3)" << endl;
        cout << "Divide (4)" << endl;
        cout << "Volume of a Right Cylinder (5)" << endl;
        cout << "Volume of a Sphere (6)" << endl;
        cout << "Volume of a Cube (7)" << endl;
        cout << "Average (8)" << endl;
        cout << "Standard Deviation (9)" << endl;
        cout << "Exit (0)" << endl;
        cout << "Enter you choice: ";
}


                        
So you need a for loop to put the values into an array or vector, then some code to calc the sum and whatever else you need.

use loop for taking user input...
Well I know I need an Array, but I'm having trouble figuring out how to apply it in the switch statements, and then to apply changes to the variables to get the standard deviation formula to work.
In your switch, call a function that gets the input to put into the array.

The math library has a M_PI which you can use directly instead of using your own pi.


1
2
3
4
5
6
float divide(float dividend, float divisor){
        if(divisor == 0)  //always use 0.0 instead of 0 for floating point
                return 0;  // avoids divide by zero errors
        else
                return (dividend/divisor);
}


Be careful comparing floating point, the binary fraction representation means that direct comparisons won't work. You need to compare the absolute value of the difference between the number & 0.0 to an appropriate epsilon variable. Changing the type to double doesn't fix the problem, although I always use doubles insead of floats. Google C++ epsilon example

Also check out numeric_limits<double>epsilon

Last edited on
Will do, thanks for the help! The assignment said just to use that number for pi, that's why its being used like that.
Topic archived. No new replies allowed.