The Standard Deviation is a measure of how spread out numbers are from the norma
ID: 3827054 • Letter: T
Question
The Standard Deviation is a measure of how spread out numbers are from the normal. The symbol for Standard Deviation is sigma (sigma), which has the following formula: sigma = Squareroot 1/N sigma^N _i=1 (x_i - mu)^2 To calculate the standard deviation of those numbers: Work out the Mean mu (the simple average of the numbers) Then for each number: subtract the Mean and square the result Then work out the mean of those squared differences. fake the squareroot of that and we are done! A 20 students took a C++ quiz in which they scored the following grades: 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, and 4. find the Standard Deviation. Work out the mean, in the formula above p is the mean of all our values. The mean is: mu = 9+2+5+4+12+7+8+11+9+3+7+4+12+5+4+10+9+6+9+420 = 140/20 = 7. Then for each number: subtract the Mean and square the result. This is the part of the formula that says: (x_i - mu)^2 subtract the mean and square the result", like this: (9 - 7)^2 = (2)^2 = 4 (2-7)^2 = (-5)^2 = 25 (5 - 7)^2 = (-2)^2 = 4 (4 - 7)^2 = (-3)^2 = 9 (12 - 7)^2 = (5)^2 = 25 (7 - 7)^2 = (0)^2 = 0 (8 - 7)^2 = (1)^2 = 1 (11- 7)^2 = (4)^2=16 (9.7)^2 = (2)^2=4 (3- 7)^2 = (4)^2 =16 (7- 7)^2 = (0)^2 =0 (4- 7)^2 = (3)^2 =9 (12- 7)^2 = (5)^2 =25 (5- 7)^2 = (2)^2 =4 (4- 7)^2 = (3)^2 =9 (10- 7)^2 = (3)^2 =9 (9- 7)^2 = (2)^2 =4 (6- 7)^2 = (1)^2 =1 (9- 7)^2 = (2)^2 =4 (4- 7)^2 = (3)^2 =9 Then work out the mean of those squared differences. To work out the mean, add up all the values then divide by how many. First add up all the values from the previous step. We want to add up all the values from 1 to N, where N=20 in our case because there are 20 values: Example (continued): sigma^N _i=1 (x, - mu)^2 Which means: Sum all values from (X_1 - 7)^2 to (X_N - 7)^2 We already calculated (X_1 - 7)^2 = 4 etc. in the previous step, so just sum them up: = 4+25+4+9+25+0+1+16+4+16+0+9+25+4+9+9+4+1+4+9 =178 But that isn't the mean yet, we need to divide by how many, which is simply done by multiplying by "1/N": Example (continued): 1/N sigma^N _i =1(x_i - mu)^2 Mean of squared differences = (1/20) times 178 = 8.9 Take the squareroot of that to find the Standard Deviation: sigma = Squareroot (8.9) = 2.983 Write a complete C++ program that calculates Standard Deviation using data from input file called: grades.txt. Use data in the example above to populate the data file: grades.txt Output the average and the standard deviation to an external file called: results.txt.. Increase your program modularity by implementing functions so the main contains only function callsExplanation / Answer
//if you like solution given, please rate
//program
#include<iostream>
#include<fstream>
#include<cmath>
#define MAX 100
using namespace std;
float calculate_mean(int a[],int size);
float sum_of_difference(int a[], int size, float mean);
int main()
{
//declare array to hold grades
int grades[MAX];
int count = 0;
ifstream in;
//open file for reading
in.open("grades.txt");
//check if input file open
if (!in)
{
cout << "Cannot open the input grades.txt" << endl;
return 1;
}
while (!in.eof())
{
in >> grades[count++];
}
//calculate mean
float mean = calculate_mean(grades, count);
float sum = sum_of_difference(grades, count, mean);
//now devide the sum by number of students and take sqrt
float standard_deviation;
standard_deviation = sqrt(sum/count);
cout << "Standard deviation is : " << standard_deviation << endl;
}
float calculate_mean(int a[], int size)
{
float sum = 0;
for (int i = 0; i < size; i++)
{
sum += a[i];
}
sum /= size;
return sum;
}
float sum_of_difference(int a[], int size, float mean)
{
float sum = 0;
for (int i = 0; i < size; i++)
{
sum += (a[i] - mean)*(a[i] - mean);
}
return sum;
}
--------------------------------------------------------
//output
Standard deviation is : 2.98329
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.