Need help calculating standard deviation

Posts 1–5 of 5 · Page 1 of 1
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.


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;
	}
}
Quote Originally Posted by robater View Post
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;
	}
}
You shouldn't need to get the square root of a negative number. You start by squaring all the differences, and that always returns a positive number.
NB: https://en.wikipedia.org/wiki/Standa...Basic_examples
Yes, you can't sqrt negative numbers.
You should make sure to use the abs() function I think it was. Which is the the absolute value returned, ensuring the number will never be negative.
It is in the cmath library

EDIT:
You can either use absolute value or just make a condition statement to make sure the value is positive.
Quote Originally Posted by Pie View Post
Yes, you can't sqrt negative numbers.
Yes, but have you accidentally doubted what I said?

Quote Originally Posted by Pie View Post
It is in the cmath library
That function is also included in <cstdlib> (this only works on integers)
If you want the abs() returns a double you need to include <cmath> (this on floating point values)

In C you need fabs for floating point values, and abs should work on integers, rather in C++ std::abs is absolutely overloaded for any numerical types: (watch the standard)
Quote Originally Posted by maestro1994 View Post
Yes, but have you accidentally doubted what I said?


That function is also included in <cstdlib> (this only works on integers)
If you want the abs() returns a double you need to include <cmath> (this on floating point values)

In C you need fabs for floating point values, and abs should work on integers, rather in C++ std::abs is absolutely overloaded for any numerical types: (watch the standard)
I'm not doubting what you said. Squaring does return a positive number. I'm just telling him another means of getting a positive number just to make sure his code doesn't break (a double check).
Standard Deviation if I remember correctly is just the sqrt of the Variance. So, along the way he should of not had issues with negative numbers since you have to square the difference in one of the steps.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us