Hey guys I'm having trouble figuring out how to calculate the standard divination of an array in Java. As you can see I have already calculated the mean, and I know that at the end I will have to divide by the sample size minus 1 (n-1) and square that number. The problem I'm having is how to take every number and calculate how far it is away from the mean, then square that number. I know I could do every number from the data set separately but there has to be an easier way. Any help would be appreciated, heres my code.

public class CalculateArray
{

    public static void main(String[] args)
    {
        int [] numbers = new int[]{1,2,3,4,5,6,7,8,9};

        int sum = 0;
        int max = 0;
        int min = numbers[0];
        double sd = 0;

        for(int i=0; i<numbers.length; i++)
        {
            sum = sum + numbers[i];
        }

        double average = sum / numbers.length;

        System.out.println("Average value is : " + average);

        for(int i=0; i<numbers.length; i++)
        {
            if(numbers[i] > max)
            {
                max = numbers[i];
            }
        }

        System.out.println("max number is : " + max);

        for(int i=0; i<numbers.length; i++)
        {
            if(numbers[i] < min)
            {
                min = numbers[i];
            }
        }

        System.out.println("min number is : " + min);

        for (int i=0; i<numbers.length;i++)
        {
           //this is where im having problems
           sd = ???
        }

        double standardDeviation = math.sqrt(sd/(numbers.length-1));

        System.out.println("The standard deviation is : " + standardDeviation);
    }
}

Take each number and subtract the mean, then square the result. Finally, divide by the length of the array and add that to your sum. The sum is the variance, and the square root of that is the standard deviation:

double sd = 0;

for (int i = 0; i < numbers.length; i++)
{
    sd += square(numbers[i] - average) / numbers.length;
}

double standardDeviation = math.sqrt(sd);

You could total up the squared difference and do the division after the loop, but if there are a ton of large numbers you may end up overflowing the data type.

hows this look?

double sd = 0;
for (int i=0; i<numbers.length;i++)
        {
            //this is where im having problems
            {
                sd += ((numbers[i] - average)*(numbers[i] - average)) / (numbers.length - 1);
            }
        }

        double standardDeviation = Math.sqrt(sd);

        System.out.println("The standard deviation is : " + standardDeviation);

hows this look?

Does it give you the correct answer? If so, it looks fine. If not, it doesn't look fine. ;)

It works ;) thanks for the help!

Your opinion is relevant, I support and will follow you regularly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.