20120201

Java Project #12: Statistics


/**
 * class Statistics finds the mean and standard deviation of given numbers.
 * 
 * @author (Andrew Samuels) 
 * @version (04-09-2010)
 */
//import scanner
import java.util.Scanner;

public class Statistics
{
    
    public static void main(String[] args)
    {
        //create variables
        int size;
        int i;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture size from user
        System.out.print("How many numbers will be entered: ");
        size = keyboard.nextInt();
        
        //create array of "size"
        double[] p = new double[size];
        
        //read numbers from user one by one
        for(i = 0; i < size; i ++)
        {
            System.out.print("Enter number " + (i + 1) +": ");
            p[i] = keyboard.nextDouble();
        }
        
        //find mean
        double sum = 0;
        for(i = 0; i < size; i++)
        {
            sum = sum + p[i];
        }
        
        double mean = sum/(double)size;
        System.out.println("Mean: " + mean);
        
        //find standard deviation
        double temp = 0;
        for (i = 0; i < size; i++)
        {
            temp = temp + (p[i] - mean)*(p[i] - mean);
        }
        temp = Math.sqrt(temp/size);
        
        System.out.println("Standard deviation: "+ temp);
    }
}