Given a set of values representing a complete population, the standard deviation
ID: 672811 • Letter: G
Question
Given a set of values representing a complete population, the standard deviation is found by taking the square root of the average of the squared deviations of the values from their average value. For an example of what that means, see the following Wikipedia page; in particular, see the "Basic Example". As discussed in Wikipedia, given the following complete population:
then the standard deviation is
Your task is to write a C++ function StandardDev that receives an array of doubles representing a complete population along with an integer N denoting the number of elements in this array, and returns the standard deviation. The remainder of the program has been written for you and supplied below; the main() function inputs the data from the keyboard into an array, calls your function, and outputs the result. Do not modify the main() function, modify only the function StandardDev.
There is a portion of the code given below. You must fill in the blank portion of code.
Here is the code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double StandardDev(double values[1000], int N)
{
double result = -1.0;
Fill in this portion without touching the rest of code!
return result;
}
int main()
{
double value, values[1000];
int i, N;
i = 0;
cin >> value;
while (value != -1)
{
values[i] = value;
i = i + 1;
cin >> value;
}
N = i;
cout << StandardDev(values, N) << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
double StandardDev(double values[1000], int N)
{
double mean=0.0, result=0.0;
int i;
for(i=0; i<N;++i)
{
mean+=values[i];
}
mean=mean/N;
for(i=0; i<N;++i)
result+=(values[i]-mean)*(values[i]-mean);
return sqrt(result/N);
return result;
}
int main()
{
double value, values[1000];
int i, N;
i = 0;
cin >> value;
while (value != -1)
{
values[i] = value;
i = i + 1;
cin >> value;
}
N = i;
cout << StandardDev(values, N) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.