/******************************************************************************
 *  Name:    Reference
 *  NetID:   ref
 *  Precept: P00
 *
 *  Description:  Computes the rolling mean and max of a sequence
 *                of floating-point numbers, with window size k.
 ******************************************************************************/

public class RollingStats {
    private final int k;              // window size
    private int n;                    // number of data values
    private Queue<Double> history;    // last k data values
    private double sum;               // sum of all data values

    // creates a new object with window length k
    public RollingStats(int k) {
        this.k = k;
        history = new Queue<Double>();
    }

    // adds the data value x to the data structure
    public void add(double x) {
        if (n >= k) {
            history.dequeue();
        }
        sum += x;
        n++;
        history.enqueue(x);
    }

    // returns the rolling mean
    public double rollingMean() {
        if (n <= k) return mean();
        double queueSum = 0.0;
        for (double x : history)
             queueSum += x;
        return queueSum / k;
    }

    // returns the overall mean
    public double mean() {
        return sum / n;
    }

    // unit tests this data type
    public static void main(String[] args) {
        int k = Integer.parseInt(args[0]);
        RollingStats stats = new RollingStats(k);
        while (!StdIn.isEmpty()) {
            double x = StdIn.readDouble();
            stats.add(x);
            StdOut.printf("%.2f  %.2f\n", stats.mean(), stats.rollingMean());
        }
    }
}