So I started coding C++ about 4 weeks ago as part of my freshman C++ Class and in this "Lab" I have to calculate the Minimum, Maximum, Mean, and Standard Deviation of 3 different numbers (in this case 3 grades). I figured out everything except standard deviation, I tried using sqrt() but since sqrt returns nan for negative numbers I failed completely, if anyone could please tell/show me how to calculate standard deviation in my case, I google'ed it and it was extremely complicated stuff like floats and arrays.
Code:
#include <iostream>
#include<cmath>
#include <complex>
using namespace std;
void input(double &grade1, double &grade2, double &grade3);
void calc(int &while1, double &grade1, double &grade2, double &grade3, double &min, double &max, double &avg, double &dev);
void repeat(int &while1);
int main() {
double min, max, avg, dev, grade1, grade2, grade3;
int def = 1;
int while1;
do {
switch(def) {
case 1:
input(grade1, grade2, grade3);
calc(while1, grade1, grade2, grade3, min, max, avg, dev);
break;
default:
break;
}
} while(while1 == 1);
return 0;
}
void input(double &grade1, double &grade2, double &grade3) {
cout << "Enter the three test scores for the student, separated by a space: " << endl;
cin >> grade1;
cin >> grade2;
cin >> grade3;
}
void calc(int &while1, double &grade1, double &grade2, double &grade3, double &min, double &max, double &avg, double &dev){
double avg1;
if ((grade1 > grade2) && (grade1 > grade3)) {
max = grade1;
} else if (grade2 > grade3) {
max = grade2;
} else if (grade3 > grade2) {
max = grade3;
}
if ((grade1 < grade2) && (grade1 < grade3)) {
min = grade1;
} else if ((grade2 < grade1) && (grade2 < grade3)) {
min = grade2;
} else if ((grade3 < grade2) && (grade3 < grade1)) {
min = grade3;
}
avg = grade1+grade2+grade3;
avg1 = avg/3;
cout << "AVG Output: " << avg1 << endl;
cout << "The student's statistics are: " << endl;
cout << "Minimum = " << min << " Maximum = " << max << " Average = " << avg1 << " Standard Deviation = " << endl;
repeat(while1);
}
void repeat(int &while1) {
char t;
cout << "Do you want to calculate the statistics for another student? (y/n): " << endl;
cin >> t;
if (t == 'y') {
while1 = 1;
} else if (t == 'n') {
while1 = 0;
}
}