Thread: Standard Deviation of an array of numbers...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    Standard Deviation of an array of numbers...

    I am writing a program that tells the user to enter 20 numbers, then it uses those and finds the average and standard deviation.

    I put the 20 numbers in an array.

    Does anyone know the c++ formula for standard deviation of an array? It's blowing my mind. I've been trying for hours.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't know what "standar deviation" is...someone else here might, but in the meantime it would help if you could explain it a bit.

    Edit: after a google search I found one explanation:

    standard deviation is the square root of the variance of a mean of a set of numbers

    so for the set 1,2,3 the variance is:

    ( (1-avg)+(2-avg)+(3-avg) ) / n

    in this case avg is 2 and n is 3

    it also went on to say that you should divide by n-1 to give an unbiased estimate...although it didn't really explain why

    If this is correct it shouldn't be too hard to do...
    Last edited by JaWiB; 11-19-2003 at 10:26 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    It's a real weird formula.

    It's the square root of a summation of each number minus the average, squared and divided by the amount of numbers minus 1.

    It's hard to explain, even harder to code.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Sounds like i had it somewhat correct, but I had an error, I meant to square those terms:

    (1-avg)*(1-avg), etc

    So you should be able to do something like:
    Code:
    for (int i=0;i<n;i++)
       total+=(nums[i]-avg)*(nums[i]-avg);
    total/=n-1;
    std::cout<<sqrt(total);
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Thanks!

    I got it to work, I think. Even if not so, you helped a lot. Awesome!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. loops and I/O file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 09:41 AM