我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*filename:P6Q11
*Name:Wang Chaoran
Desciprtion:
11 (Computing deviation)
Exercise 5.21 computes the standard deviation of numbers.
This exercise uses a different but equivalent formula to compute the standard deviation of n numbers.
To compute deviation with this formula, you have to store the individual numbers using an array,
so that they can be used after the mean is obtained.
Use {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} to test the method.
*/
class P6Q11{
public static void main(String[] args){
double[] test = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double average = mean(test);
System.out.println("The average of this array is: "+average);
System.out.println("The deviation of this array is: "+deviation(test,average));
}
static double mean(double[] array){
double sum =0;
for(int i=0;i<array.length;i++)
sum+=array[i];
return sum/array.length;
}
static double deviation(double[] array,double mean){
double sum =0;
for(int i=0;i<array.length;i++)
sum+= (array[i]-mean)*(array[i]-mean);
return sum/(array.length-1);
}
}
导入论坛查看(32)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing