Need help calculating standard deviation

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.

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
#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;
	}
}
The standard deviation is the square root of an average of squares. An average of squares can't be negative.
Topic archived. No new replies allowed.