Summary: compute population standard deviation. Given a set of values representi
ID: 3595950 • Letter: S
Question
Summary: compute population standard deviation. Given a set of values representing a complete population, the population standard deviation is found by taking the square root of the average of the squared deviations of the values from their average value. Huh? In other words, given a sequence of real numbers, you do the following 1. compute the average 2 for each real number R, compute (R-avg)*2- call these our temp values 3. compute the variance- the average of the temp values compute the square root of the variance this is the population standard deviation For a concrete example, see the following Wikipedia page. The goal is to develop a complete C++ program to input a complete population of real numbers, compute the population standard deviation, and output this value. For simplicity of testing, the input comes from the keyboard (cin), and ends with a -1. Assume at most ,000 numbers. For example, if the program is given the following input sequence 2.0 4.0 4.0 4.0 5.0 5.0 7.0 9.0 -1 then the population standard deviation is 2 The main0 function, which inputs the values and stores them into an array, is written for you. Your assignment is to write the StandardDev0 function, which is called by main0 to compute the population standard deviation. BEFORE YOU START We need to avoid a bug in some browsers. Above the editor pane you'll see the text "Current file: main.cpp", with a little drop-down arrow to the right. Click the drop-down and select functions.cpp. Then click the link "Load default template... this will ensure the file is properly oaded for you to edit; you only need to do this once.Explanation / Answer
The function is written assuming that -1 is not present in array A. I don't understand the need of having a temp array as it is unnecessary wastage of space because we can have a new variable avg1 which stores the value of the squared term and is incremented every time the loop runs.then we can divide it by n so as to get variance.And i am also assuming math.h is included for me to use sqft function of math header file.
double Standarddev(double A[],int n)
{
double temp[1000];
double avg=0.0;
int i;
for(i=0;i<n;i++)
{
avg+=A[i];
}
avg=avg/n;
for(i=0;i<n;i++)
{
temp[i]=(A[i]-avg)*(A[i]-avg);
}
avg=0;
for(i=0;i<n;i++)
{
avg+=temp[i];
}
avg=avg/n;//this time the avg variable contains the variance.
return sqrt(avg);
}
Please give this a thumbs up and if you have any doubt leave a comment i will be happy to answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.