统计信息

  • 访问数:8751
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q11

2008-04-15 13:26:15

天气: 晴朗 心情: 高兴

/*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); 
}
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd