Problem Statement
The standard deviation of a group of numbers is a very useful statistic in many settings. It measures how spread out a group of numbers is and shows up in all sorts of statistical analyses.
To find the standard deviation of a group of numbers, you must use the following formula:
StdDev = sqrt( sum over all elements[ (numsi - mean)^2 ]/N )Here, numsi represents the ith element of the input int[], nums. mean represents the average of all the elements in nums. N represents the number of elements in nums. The character '^' denotes exponentiation. Your method should compute this standard deviation, and return the result as a double.
Definition
- Class:
- StdDev
- Method:
- stdDev
- Parameters:
- int[]
- Returns:
- double
- Method signature:
- double stdDev(int[] nums)
- (be sure your method is public)
Notes
- You do not need to worry about small rounding errors. As long as your result is within (10^-9*the exact result) of the exact result your solution will be evaluated as correct. In other words, as long as the first 9 digits are correct, your result will be considered correct. Floating point arithmetic is plenty accurate to solve this problem without worrying about rounding.
Constraints
- nums will contain between 1 and 50 elements, inclusive.
- Each element of nums will be between -1,000,000,000 and 1,000,000,000, inclusive.
Examples
{0,2,3,4,6}Returns: 2.0
The mean is 3, so sum over all elements[ (numsi - mean)^2 ] = 9 + 1 + 0 + 1 + 9 = 20 So, the standard deviation is sqrt(20/5) = 2.{1,2,3,4}Returns: 1.118033988749895
{15672,-458661,4567231,-48981111,4567231,-1000000000}Returns: 3.7015176854905784E8