001package algs11;
002import stdlib.*;
003/* **********************************************************************
004 *  Compilation:  javac Average.java
005 *  Execution:    java Average < data.txt
006 *  Dependencies: StdIn.java StdOut.java
007 *
008 *  Reads in a sequence of real numbers, and computes their average.
009 *
010 *  % java Average
011 *  10.0 5.0 6.0
012 *  3.0 7.0 32.0
013 *  <Ctrl-d>
014 *  Average is 10.5
015
016 *  Note <Ctrl-d> signifies the end of file on Unix.
017 *  On windows use <Ctrl-z>.
018 *
019 *************************************************************************/
020
021public class Average {
022        public static void main(String[] args) {
023                //StdIn.fromString ("10.0 5.0 6.0 3.0 7.0 32.0");
024                StdIn.fromFile ("data/1kints.txt");
025
026                int count = 0;       // number input values
027                double sum = 0.0;    // sum of input values
028
029                // read data and compute statistics
030                while (!StdIn.isEmpty()) {
031                        double value = StdIn.readDouble();
032                        sum += value;
033                        count++;
034                }
035
036                // compute the average
037                double average = sum / count;
038
039                // print results
040                StdOut.println("Average is " + average);
041        }
042}