QuestionAsk Questions?

program to calculate the standard deviation of an array of values.the array elements are read from terminal.use function to calculate standard deviation and mean.

View Answers

December 10, 2010 at 1:39 PM

Hi Friend,

Try the following code:

import java.util.*; public class StandardDeviation{ public double findMean(int array[]){ double total = 0; for(int i = 0; i < array.length; i++){ total = total + array[i]; } double mean = total/array.length; return mean; } public void findStandardDeviation(int array[]){ double mean = findMean(array); System.out.println("Mean is: "+mean); double d1 = 0; double d2 = 0; double sum = 0; for(int i = 0; i < array.length; i++){ d2 = (mean - array[i])*(mean - array[i]); d1 = d2 + d1; } System.out.println("Standard Deviation: " + Math.sqrt((d1/(array.length-1)))); } public static void main(String args[]){ Scanner input=new Scanner(System.in); System.out.println("Enter Array Elements: "); int array[] = new int[5]; for(int i=0;i<array.length;i++){ array[i]=input.nextInt(); } StandardDeviation sd = new StandardDeviation(); sd.findStandardDeviation(array); } }

Thanks

Related Tutorials/Questions & Answers: