统计信息

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

P5Q18

2008-04-09 15:06:08

天气: 晴朗 心情: 高兴

/*fileName:P5Q18.java
*Name:Wang Chaoran
* Description:
18 (Computing mean and standard deviation)
In business applications, you are often asked to compute
the mean and standard deviation of data. The mean is simply
the average of the numbers. The standard deviation is a statistic
that tells you how tightly all the various data are clustered around
the mean in a set of data. For example, what is the average age of the
students in a class? How close are the ages? If all the students are the
same age, the deviation is 0. Write a program that generates ten random
numbers between 0 and 1000, and computes the mean and standard deviations
of these numbers using the following formula:
*/
public class P5Q18{
 
  public static void main(String[] args){
   
     int[] random = new int[10];
     for(int i=0;i<random.length;i++)
     random[i]=(int)(Math.random()*1001);
     System.out.println("The ten random numbers are: ");
     for(int element:random)
     System.out.print(element+" ");
     System.out.println("\nThe mean of the ten numbers are: "+mean(random));
     System.out.println("The deviation of the ten numbers are: "+deviation(random));

    }
  public static double mean(int[] random){
    double sum = 0;
    for(int i=0;i<random.length;i++)
      sum += random[i];
    return sum/random.length;
  }
  public static double deviation(int[] random){
    double result = 0,sigmaTwo = 0;
    for(int i=0;i<random.length;i++){
      result += random[i]*random[i];
      sigmaTwo += random[i];
    }
    result -= sigmaTwo*sigmaTwo/random.length;
    result /= random.length-1;
    return Math.sqrt(result);
  }
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

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