In statistics, the standard deviation measures the amount of variation or disper
ID: 3689573 • Letter: I
Question
In statistics, the standard deviation measures the amount of variation or dispersion from the average. It can be calculated using the following formula Where is the standard deviation, x is each value in the data set, is the mean of all values in the data set, and n is the number of values in the data set. For your program you will ask the user for double values. Your program should stop when the user enters -99 when at this point your program will display the mean and the standard deviation from the data set. Last Modified: 10/29/2015 Sample output: Enter a value (or -99 to quit): 3 Enter a value (or -99 to quit): 5 Enter a value (or -99 to quit): 11 Enter a value (or -99 to quit): 14 Enter a value (or -99 to quit): 11 Enter a value (or -99 to quit): -99 The average is 8.8 with a standard deviation of 4.11. must be progammed by C++
Explanation / Answer
#include <iostream>
#include <math.h>
using namespace std;
double calMean(int n[],int size)
{
double total;
for(int i=0;i<size;i++)
{
total+=n[i];
}
return total/size;
}
double calSD(int n[],int size)
{
double mean=calMean(n,size),total;
for(int i=0;i<size;i++)
{
total+=(n[i]-mean)*(n[i]-mean);
}
return sqrt(total/size);
}
int main()
{
int n[20],flag=1,i=0;
while(flag==1)
{
int tmp;
cout << "enter an integer or -99: ";
cin>>tmp;
if(tmp==-99)
flag=0;
else
{
n[i]=tmp;
i++;
}
}
for(int j=0;j<i;j++)
cout<< "The average is "<<calMean(n,i);
cout<<" with a standard deviation of "<<calSD(n,i)<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.