The standard deviation of a list of numbers is a measure of how much the numbers deviate from the average. If the standard deviation is small, the numbers are clustered close to the average. If the standard deviation is large, the numbers are scattered far from the average. The standard deviation of a list of numbers n1, n2, n3, and so forth is defined as the square root of the average of the following numbers:

(n1 - a)^2, (n2 - a)^2, (n3 - a)^2, and so forth.

The number a is the average of the numbers n1, n2, n3, and so forth. Define a static method that takes a partially filled array of numbers as its argument and returns the standard deviation of the numbers in the partially filled array. Since a partially filled array requires two arguments, the method will actually have two formal parameters, an array parameter and a formal parameter of type int that gives the number of array positions used. The numbers in the array will be of type double. Write a suitable test program for your method.

this is my program so far with driver

public class Deviations
{
	public static double standard(double[] a, int n)
	{
		double average = 0;
		for(int i = 0; i < a.length; i++)
			average = average + a[i];
		if(n > 0)
		{
			return (average/n);
		}
		else
		{
			System.out.println("ERROR: Can't average 0 numbers.");
			return 0;
		}
		
	}
}

// driver program

import java.util.Scanner;

public class DeviationsDemo 
{
	public static void main(String[] args)
	{
		int n;
		Deviations deed = new Deviations();
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Enter length of the array.");
		n.standard(keyboard.nextInt());
		double[] a = new double[15];
	}
}

if anyone can help me get this right according to the directions i would really appreciate it
i am confused on how to do this, but i am still thinking this through
thank you for any help

Start with the driver.

import java.util.Scanner;

public class DeviationsDemo 
{
	public static void main(String[] args)
	{
		int n;
		Deviations deed = new Deviations();
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Enter length of the array.");
		n.standard(keyboard.nextInt());
		double[] a = new double[15];
	}
}

Line 11 makes no sense. n is an int. There's no int.standard function. Maybe you mean deed.standard ? deed.standard takes two parameters, not one. It requires an array and you aren't giving it one.

Line 12 - You just asked for the user to enter the number of numbers. Then you hard code the number of numbers as 15. And you never have the user enter 15 numbers.

My advice is forget the standard deviation part for now. Get it so you can ask the user to enter an array of a size and contents of their choosing and display it correctly. Once you have that, figure out how to take the average of those numbers. Then and only then worry about how to take the standard deviation.

import java.util.Scanner;

public class Deviations
{
	public static double standard(double[] a, int n)
	{
		double average = 0;
		for(int i = 0; i < a.length; i++)
			average = average + a[i];
		if(n > 0)
		{
			return (average/n);
		}
		else
		{
			System.out.println("ERROR: Can't average 0 numbers.");
			return 0;
		}
	}
	
	public static int fillTheArray(double[] a)
	{
		System.out.println("Enter up to " + a.length + " nonnegative numbers.");
		System.out.println("Enter a -1 when you are finished.");
		Scanner keyboard = new Scanner(System.in);
		
		double next;
		int index = 0;
		next = keyboard.nextDouble();
		while((next >= 0)&&(index < a.length))
		{
			a[index] = next;
			index++;
			next = keyboard.nextDouble():
		}
		if(next>=0)
		{
			System.out.println("Could only read in " + a.length + " input values.";
		}
		
		return index;
	}
}
import java.util.Scanner;

public class DeviationsDemo 
{
	public static void main(String[] args)
	{
		double[] a = new double[15];
		int numberUsed = 0;
		Deviations deed = new Deviations();
		Scanner keyboard = new Scanner(System.in);
		numberUsed = fillTheArray(a); // compiler error
	}
}

here is my new code
the driver still doesn't work
i still need to do the standard deviation but i still don't understand it and i still have to fix up this code
thank you for your help

Your formula for standard deviation only works for an entire population. If you're taking the standard deviation of a sample, then you need to divide by (n-1), not by n. (Note: This might not be important to you, depending on what your teacher said.)

In any case, it looks like you are not following either formula properly (you're leaving parts out), read the "basic example" and you will immediately see what parts you are missing. .

http://en.wikipedia.org/wiki/Standard_deviation

import java.util.Scanner;

public class Deviations
{
	public static double standard(double[] a, int n)
	{
		double average = computeAverage(a, n);
		double total = 0;
		for(int i = 0; i < n; i++)
		{
			double[] all = Math.pow(a[i]-average, 2);
			total = total + all;
			double total2 = total/n;
			double[] all2 = Math.sqrt(total2);
		}
	}
	
	public static int fillTheArray(double[] a)
	{
		System.out.println("Enter up to " + a.length + " nonnegative numbers.");
		System.out.println("Enter a -1 when you are finished.");
		Scanner keyboard = new Scanner(System.in);
		
		double next;
		int i = 0;
		next = keyboard.nextDouble();
		while((next >= 0)&&(i < a.length))
		{
			a[i] = next;
			i++;
			next = keyboard.nextDouble();
		}
		if(next>=0)
		{
			System.out.println("Could only read in " + a.length + " input values.");
		}
		
		return i;
	}
	
	public static double computeAverage(double[] a, int n)
	{
		double average = 0;
		for(int i = 0; i < n; i++)
			average = average + a[i];
		if(n > 0)
		{
			return (average/n);
		}
		else
		{
			System.out.println("ERROR: Can't average 0 numbers.");
			return 0;
		}
	}
}

could you check my standard method to see how i could possibly do what im trying to do
i get a compiler error with the double[] in the for loop
how can i do that so i dont get a compiler error

could you check my standard method to see how i could possibly do what im trying to do
i get a compiler error with the double[] in the for loop
how can i do that so i dont get a compiler error

You're going to get a compiler error HERE because you haven't specified what class the fillTheArray function resides in:

import java.util.Scanner;

public class DeviationsDemo 
{
	public static void main(String[] args)
	{
		double[] a = new double[15];
		int numberUsed = 0;
		Deviations deed = new Deviations();
		Scanner keyboard = new Scanner(System.in);
		numberUsed = fillTheArray(a); // compiler error
	}
}

Change it to:

numberUsed = Deviations.fillTheArray(a);

sorry i forgot to put that i fixed that up
i need help with the standard method in my class program

import java.util.Scanner;

public class Deviations
{
	public static double standard(double[] a, int n)
	{
		double average = computeAverage(a, n);
		double total = 0;
		for(int i = 0; i < n; i++)
		{
			double[] all = Math.pow(a[i]-average, 2);
			total = total + all;
			double total2 = total/n;
			double[] all2 = Math.sqrt(total2);
		}
	}
	
	public static int fillTheArray(double[] a)
	{
		System.out.println("Enter up to " + a.length + " nonnegative numbers.");
		System.out.println("Enter a -1 when you are finished.");
		Scanner keyboard = new Scanner(System.in);
		
		double next;
		int i = 0;
		next = keyboard.nextDouble();
		while((next >= 0)&&(i < a.length))
		{
			a[i] = next;
			i++;
			next = keyboard.nextDouble();
		}
		if(next>=0)
		{
			System.out.println("Could only read in " + a.length + " input values.");
		}
		
		return i;
	}
	
	public static double computeAverage(double[] a, int n)
	{
		double average = 0;
		for(int i = 0; i < n; i++)
			average = average + a[i];
		if(n > 0)
		{
			return (average/n);
		}
		else
		{
			System.out.println("ERROR: Can't average 0 numbers.");
			return 0;
		}
	}
}

//driver program

import java.util.Scanner;

public class DeviationsDemo 
{
	public static void main(String[] args)
	{
		Deviations deed = new Deviations();
		double[] a = new double[15];
		int n = 0;
		Scanner keyboard = new Scanner(System.in);
		n = deed.fillTheArray(a); 
		deed.standard(a, n);
	}
}

for(int i = 0; i < n; i++)
{
double total2 = total/n;
double[] all2 = Math.sqrt(total2);
}

where in the formula does it say to take the average over and over again? You're only supposed to take the average once when you are finished with the other stuff.

1. Compute the mean (average) of all of your values
2. Compute (your number-the mean) squared. Do this for every number in your list.
3. Add the numbers from step 2.
4. Divide the total from step 3 by the number of numbers you had in your original list.
5. Take the square root of the total in step 4. This is your answer.


Step 4 should not be done inside a for loop, otherwise, it will be done over and over again, when the formula requires that it is done only one time.

but how can i add the numbers in step 2
i get a compiler error message that says i cant add an array to a regular number

So you have an array of your numbers? I'm assuming they are integers? Then use

int result = array[index] - the mean;

Your first number is array[0], second number is array[1], and so on. If you had an array that was declared as int[] array = new int[3]; your 3 numbers would be stored at array[0], array[1], and array[2].

forget what i just said

public static double standard(double[] a, int n)
	{
		double average = computeAverage(a, n);
		double total = 0;
		double all;
		for(int i = 0; i < n; i++)
		{
			all = Math.pow(a[i]-average, 2);
			total = total + all;
			total2 = total/n;
		}
		double all2 = Math.sqrt(total2);
		return all2;
		
	}

i fixed up the method but a compiler error message comes up saying that it can't find symbol variable total2
how can i fix this method

You can fix it by declaring the total2 variable. You declared total, you need to do the same thing for total2. You also need to read some java tutorials and learn to fix obvious things like this yourself.

double total2 = 0;

thank you very much for all your help

Write a program that computes the average and standard deviation of the text floating point numbers in a file. Use the following formulas for the average and the standard deviation of N values. The formulas compute the sum, the sum of squares, the average, the average square, the variance, and finally, the standard deviation.
sum = x1 + x2 + x3 + ... + xN-1 + xN

sumSQ = x12 + x22 + x32 + ... + xN-12 + xN2

avg = sum/N

avgSQ = avgSQ/N

var = avgSQ - avg2

sd = var(1/2)
The input file will contain any number text floating point numbers, similar to the following:
10.5
12.9
9.67
12.05
8.23
10.08
10.23
7.7
10.4
11.34
Numbers could be several, or none per line, and negative numbers are perfectly OK.

@looklikeasfine:

did you seariously re-open a dead thread in order to ask us to do your homework for you? here are a few pointers:
we don't mind helping you with your problems but, keep in mind:
1. if you have a problem, start a new thread, don't hijack someone elses thread, because
a. it might be closed/solved
b. even though it might be about something that looks like the same problem as you are having, it might be completely unrelated.
2. and this is very important: we're willing to help you do your work, not to do it for you. so we do expect you to show some effort before we'll start helping

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.