Hey guys,
Working on a project to calculate the standard deviation of an array. Instructions call for a program that will allow the user to enter a list of test scores with 999 as a sentinel value. Then a method calcStDev is invoked with the scores array as a parameter and should return the standard deviation. That is what I need help with. Writing the method calcStDev.
Here is my code:

import java.util.Scanner;

public class standardDeviation
    public void arrayBuilder()
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\fHow many numbers do you want to enter?  If you are unsure, enter an abnormally large number, and enter 999 when done.");
        int num = keyboard.nextInt();
        int count = -1;
        int i = 0;
        int j=0;
        int del = 999;
        int sum = 0;
        int [] grades = new int[num];
        System.out.println("Enter the " + num + " numbers now.");
        for (i = 0; i < grades.length; i++)
            grades[i] = keyboard.nextInt();
            if (grades[i]==999)
        System.out.println("You have entered: " + count + " number(s)");
        //copy old array to new array(grades2), getting rid of all the 0's and the 999
        int [] grades2 = new int[(count)];
        System.arraycopy(grades, 0, grades2, 0, grades2.length);
        System.out.println("These are the numbers you have entered."); //print the array out with no 999 or 0s attached
        for (i = 0; i < grades2.length; i++)
            System.out.print(grades2[i] + " ");
    public static void calcStDev(int [] grades2)

Please help me out! Thanks!

what I need help with. Writing the method calcStDev.

Do you have the formula for calculating the std dev given a list of numbers?
You will need that before you can write any code.

Yea I have the formula. Basically you take the average of all the numbers. Then subtract the average from each number and square the result. Then add up all the results and divide it by the amount of numbers you have. Then take the square root of that, and there is your standard deviation.

In my mind, I have some of the steps listed out
1) Find the sum
2) Find the average
3) Subtract average from each number and square it
4) Add up the results from number 3
5) Divide number 4 by the amount of numbers
6) Take square root of number 5

I have the pseudocode, just can't think of how to write it out in code

Do the steps one at a time.
What do you need to do to find the sum of the numbers? Sounds like a loop is needed.

for (i=0; i<grades2.length; i++)

This would go in the calcStDev method.

Have you coded it and tested it yet?
You should key in the code, provide a simple array of numbers in the code and print out the sum after the loop, compile it and execute it and check the results are good before going to the next step.

I tested it before and it worked. I put it in after where the code prints out the array grades2. It correctly printed out the sum of whatever numbers I entered in. Now onto step 2.
I would assume to find the average, I would instantiate an integer named average, and then set it equal to the sum divided by grades2.length

int average = sum/(grades2.length);

How does that look to you?

How does that look to you?

I'll say the same as I said my last post: try it and see what happens.

The one gotcha is you do NOT want to do integer division. Make one of the components of the expression a double by casting it: (double)

Yea I forgot that. double average = double(sum)/(grades2.length);
Tested and works!

On to steps 3 & 4.
Another loop and some math.

Okay...Not as simple as previous steps, but I'll make something.


for (i=0; i<grades2.length; i++)

I would instantiate int sumOfNumberMinusAverage earlier in the code. The loop will take every element of grades2 and subtract the average, then square it.

Again code it and test it with a few small numbers that you can easily look at the results and manually verify the results are as desired.

I coded and tested all the steps, and got everything to work just as planned.
Here is my testing code:

import java.util.Scanner;
import java.lang.Math;

public class tester
    public void test()
        double sum = 0;
        double average;
        double sumOfNumberMinusAverage = 0;
        int i;
        double divided;
        double sqrt;
        int [] grades2 = new int[5];
        grades2[0] = 1;
        grades2[1] = 2;
        grades2[2] = 3;
        grades2[3] = 5;
        grades2[4] = 5;
        System.out.println("\fThese are the numbers you have entered."); //print the array out with no 999 or 0s attached
        for (i = 0; i < grades2.length; i++)
            System.out.print(grades2[i] + " ");
        //find sum
        for (i=0; i<grades2.length; i++)
        System.out.println("The sum is: " + sum);
        //find average
        average = (sum)/grades2.length;
        System.out.println("The average is: " + average);
        //subtract average from number entered
        for (i=0; i<grades2.length; i++)
        System.out.println("Sum after subtracting average and squaring is: " + sumOfNumberMinusAverage);
        //Divide by amount of numbers entered
        divided = sumOfNumberMinusAverage/grades2.length;
        System.out.println("That sum divided by the amount of numbers is: " + divided);
        //Take square root
        sqrt = Math.sqrt(divided);
        System.out.println("The square root of the previous number is: " + sqrt);

Now how do I take all of this and make it a method so I can just invoke it in the initial code under arrayBuilder and it will print out the standard deviation?

Rewrite the test() method to setup the data for testing and then call a new method:
public static void calcStDev(int [] grades2) {...}

Put the rest of the test method's code into this new method and run a test to make sure it works. Then you can copy that new method into the other part of your code.

I get that. Can you just explain to me how I will invoke it in the first method named arrayBuilder? Like would it be calcStDev(int [] grades2), then print it out?

Almost. When you call a method you do NOT include the data type (int[]) in the ()s. Just the name(s) of the variables. The data types are used in the ()s when you define the method.