# calculating the standard deviation from a set of numbers in an array

Page 1 of 1

## 1 Replies - 633 Views - Last Post: 15 November 2012 - 05:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300364&amp;s=0476433923303c600d559c87aa9bf814&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kevinja

Reputation: 0
• Posts: 32
• Joined: 29-September 10

# calculating the standard deviation from a set of numbers in an array

Posted 15 November 2012 - 02:01 PM

Hello,

My program calculates/determines and displays various statistical data from a set of numbers stored in an array. One of the methods I need to implement is one that will calculate the standard deviation. I'm having a real heck of a time trying to get this to work correctly, because I am consistently getting incorrect results even after changing some of the code and looking at examples elsewhere on the Web. Could anyone please give me some suggestions/pointers? I have a method called mean as well, if that helps. My code for the stddev (standard deviation) is below:

```public static double stddev(double[] nums)
{
double standdev=0;
double x=0;
double x2=0;
for(int i=0; i<nums.length; i++)
{
x+=nums[i];
x2+=Math.pow(nums[i], 2);
}
standdev=(Math.sqrt(x2-((x)*(x)/nums.length))/(nums.length-1));
return standdev;
}

```

Thank you!

Is This A Good Question/Topic? 0

## Replies To: calculating the standard deviation from a set of numbers in an array

### #2 g00se

• D.I.C Lover

Reputation: 3542
• Posts: 16,146
• Joined: 20-September 08

## Re: calculating the standard deviation from a set of numbers in an array

Posted 15 November 2012 - 05:27 PM

No - you need to subtract the mean for each number (i.e. in the loop)