/********************************************
This program implements the Smoothing Problem
from the last lab.
 ********************************************/
#include <iostream>
#include <fstream>
using namespace std;

double average(double *A, int N);
double shiftleft(double *A, int N, double x);

int main()
{
  // Get k, the smoothing factor
  int k;
  cout << "Enter k: ";
  cin >> k;

  // Read first k values from the file
  ifstream IN("data.txt");
  double *A = new double[k];
  for(int i = 0; i < k; i++)
    IN >> A[i];

  // Write out averages of successive k #'s
  ofstream OUT("out.txt");
  double next;
  do {
    OUT << average(A,k) << endl;
    IN >> next;
    shiftleft(A,k,next);
  }while(IN);

  return 0;
}

// returns the average of the values in
// array A, which has length N
double average(double *A, int N)
{
  double total = 0;
  for(int i = 0; i < N; i++)
    total = total + A[i];
  return total / N;
}

// Shifts all values in A left by 1, shifting
// in value x on the far right, and returning
// the original A[0] value, which gets shifted out
double shiftleft(double *A, int N, double x)
{
  double first = A[0];
  for(int i = 0; i < N - 1; i++)
    A[i] = A[i+1];
  A[N-1] = x;
  return first;
}