Thread: How to calculate standard deviation

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    How to calculate standard deviation

    I have no idea how to make a program that will calculate the standard deviation of 10 numbers. I dont even know the formula. I just learned of arrays the day before and our professor gave this assignment.

    Any help??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At the very least, search the Web for a formula that you can implement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    i got the formula

    but no idea where to start and how to solve this problem..!>!>>!~

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, what is the formula that you intend to use?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by hobilla View Post
    i got the formula

    but no idea where to start and how to solve this problem..!>!>>!~
    you said you learned arrays

    Start with the program that fills array of 10 floats (from stdin) and then prints them out to the stdout
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Unless I'm missing something, you have to rip through the array to first calculate the average of all the elements. Start with that and print out the average. Hand-calculate it also to double check. Then loop through again applying the formula. Maybe there's a way to do it all at once.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    it is not being helpful to me..
    sorry guys but im really having trouble with it.
    the formula i got is...http://en.wikipedia.org/wiki/Standard_deviation from here..

    now can sum1 help me

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That article provides several versions of the formula. Which do you intend to implement?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, let's get your program started. Post it with the headers: stdio.h.

    Let's use the Wikipedia set of numbers so we'll be sure when it's right or wrong:

    Code:
    int a[8] = {2,4,4,4,5,5,7,9};
    I'll refer to the first formula (the easiest one).

    Using a for loop, how would you calculate the average (median) of these numbers?

    You have great timing - I just finished putting together a program, from the Wikipedia site!
    Quite amazing coincidence, since the thread had drifted to the second page of the forum.

    Edit:
    This is a very simple formula, so don't let the math give you a headache - it's ez:

    1) Go thru the list of numbers in the array, and add them all up
    2) Divide by the number of numbers that you have - in our example it's 40/8 = 5, which is our Median or Average
    3) Now subtract that average, from all those numbers in the same list, and
    4) Square the sum or those numbers you got in #3, above
    5) Now take the sqrt() of that number you got in #4. That's the standard deviation.

    So it's really just using simple arithmetic, except for the square root right at the last step.
    Last edited by Adak; 03-10-2009 at 11:30 AM.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    i get the steps. Thanks. But no idea of how to put it in programming language..

    help

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by hobilla View Post
    i get the steps. Thanks. But no idea of how to put it in programming language..

    help
    Well, you'll need to include a standard header file, and put in your main() function. Then declare some variables - some integers and two doubles.

    You'll need a for or while loop to make that addition of all the numbers, so you can find the average.

    You can find for and while loops sprinkled all over the net, including this very forum.

    You need to seriously get off your arse and get *doing*, however. Your helpless act has worn thin, already.

    Post what you can do. We can help you with the rest of it, but no assistance should be given, if you aren't willing to work on this, and show some actual effort.

    Despite what it may sound like, this is in fact, a trivial program.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hobilla View Post
    I have no idea how to make a program that will calculate the standard deviation of 10 numbers. I dont even know the formula. I just learned of arrays the day before and our professor gave this assignment.

    Any help??
    You don't need an array if you use the "calculator method."

    sqrt((sum_of_squares - square_of_sums / N) / (N-1))
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i get the steps. Thanks. But no idea of how to put it in programming language..

    help
    show us whatever you've done so far then we can get your program to run.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  2. Standard Deviation in C++
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2001, 11:09 AM
  3. loops and I/O file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 09:41 AM
  4. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM