std deviation and dynamic array

Posted on 2011-04-26
Last Modified: 2012-05-11
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?

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???

using namespace std;

int main(){

	int n=0;
	cout<<"How many data: ";
	int *a= new int[n];

	cout<<"Enter "<<n<<" data: ";
	for (int i=0;i<n;++i)

	//sdt deviation
	double sd =0;
	int sumXsquare =0, sumx =0;
	for(int i=0;i<n;++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;

Open in new window

Question by:pgmerLA
    LVL 30

    Expert Comment

    Hi pgmerLA,

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

    LVL 53

    Accepted Solution

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

    That formula doesn't look like any standard deviation formula I know.

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

    Be aware that the STDEV function in Excel does NOT calculate the standard deviation. It calculates the sample standard deviation :


    To calculate the standard deviation in Excel, use STDEVP :


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

    0.8 happens to be the sample variance  (ie. the square of the sample standard deviation). But that might be by accident rather than by design :)

    The standard deviation of the 6 values you entered would be 0.81650, and is obtained using the formula :

            mu = Sum(xi) / n                              <--- average (mean)
            sigma = sqrt( Sum((xi - mu)²) / n )   <--- standard deviation
    LVL 31

    Expert Comment

    Just some arithmetic to show equivalences:

    If sigma = sqrt( Sum((xi - mu)²) / n )
    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

    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.
    LVL 53

    Expert Comment

    >> Just some arithmetic to show equivalences:

    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
    LVL 53

    Expert Comment

    @pgmerLA : could you explain how that post (http:#35465466) answers your question, please ? It seems I completely misunderstood what your problem was. It appeared to me that your question was about what you were doing wrong in the code, and why you were getting 0.8 as the result.

    Author Comment

    @Infinity08:I added sqrt to my formula and it worked. I actually wanted to give you the points. I did too fast.
    How can I retract it?
    LVL 53

    Expert Comment

    >> How can I retract it?

    You can click the "Request Attention" button, and ask a moderator to re-open the question.

    Thanks for clarifying :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now