Hi, I have to calculate the standard deviation and use dynamic array.

When I double check with excel, both my results don't match.

could you tell me what I am doing wrong in my code?

Thanks.

Ps: the professor provided us with the std deviation formula:

sd= 1/(n-1)[ Sum(x^2)-1/(n-1)[(Sum(x))^2] ]

thanks for the help.

I have entered 6 numbers: 2 2 3 3 4 4.

in excel I get: .89

in my code: .8.....why???

When I double check with excel, both my results don't match.

could you tell me what I am doing wrong in my code?

Thanks.

Ps: the professor provided us with the std deviation formula:

sd= 1/(n-1)[ Sum(x^2)-1/(n-1)[(Sum(x))^

thanks for the help.

I have entered 6 numbers: 2 2 3 3 4 4.

in excel I get: .89

in my code: .8.....why???

```
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n=0;
cout<<"How many data: ";
cin>>n;
int *a= new int[n];
cout<<"Enter "<<n<<" data: ";
for (int i=0;i<n;++i)
cin>>a[i];
//sdt deviation
double sd =0;
int sumXsquare =0, sumx =0;
for(int i=0;i<n;++i)
{sumx+=a[i];
sumXsquare += a[i]*a[i];
}
sd = (1.0/(n-1))*(sumXsquare-((1.0/n)*(sumx*sumx)));
cout<<"Standard Deviation = "<<setprecision(4)<<sd<<endl;
return 0;
}
```

IMO you 'n' instead of (n-1) at '... sumXsquare-((1.0/n)...' - shouldn't this be '... sumXsquare-((1.0/(n-1)) ...'?

ZOPPO

If sigma = sqrt( Sum((xi - mu)Â²) / n )

then

n sigmaÂ²

= Sum( (xi - mu)Â² )

= sum( xiÂ² - 2 xi mu + muÂ² )

= sum(xiÂ²) - 2mu sum(xi) + muÂ² * sum(1)

= sum(xiÂ²) - 2mu sum(xi) + n muÂ²

But mu = sum(xi)/n

==> sum(xi) = n mu

Then

n sigmaÂ² = Sum( (xi - mu)Â² )

= sum(xiÂ²) - 2mu sum(xi) + n muÂ²

= sum(xiÂ²) - 2mu (n mu) + n muÂ²

= sum(xiÂ²) - 2 n muÂ² + n muÂ²

= sum(xiÂ²) - n muÂ²

sigmaÂ² = ( sum(xiÂ²) - n muÂ² )/n = ( sum(xiÂ²)/n - muÂ² )

sigma = sqrt( sum(xiÂ²)/n - muÂ² )

The (n-1) term in the OP (instead of n) is related to a particular method of sampling populations IIRC.

You put a bit more effort into it than me ;)

That would mean that the formula from the question (assuming the second (n-1) was supposed to be n) is meant to calculate the sample variance, and not the standard deviation. In which case, 0.8 is the right answer.

Or, assuming that the sqrt got lost while copy-pasting the formula, it would be meant to calculate the sample standard deviation. In which case 0.89 is the right answer.

Neither of these is the standard deviation though (as I mentioned earlier). So, if you actually want to calculate the standard deviation, you'll have to use the formula I mentioned in my previous post, and you should get the result 0.81650

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.