### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130`` ``````#include #include 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.

 ``123456`` ``````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.