// Simple use of a dynamic array
#include <iostream>
using namespace std;

int main(){

  double *x, mean, s;
  int    i, n;

  // Repeat forever
  while (true) {

    // Get the number of elements
    cout << "How many elements (zero to exit): ";
    cin  >> n;

    // Check for n=0 and n<0
    if (n==0) {
      cout << "Bye." << endl << endl;
      break;
    }
    else if (n<0) {
      cout << "Negative number of elements?!" << endl;
      continue;
    }

    // Request a memory location of a block of n doubles
    x = new double[n];

    // Get the elements and compute the sum
    cout << "Input elements: ";
    s = 0.0;
    for(i=0; i<n; i++){
       cin >> x[i];
        s += x[i];
    }

    // Compute the mean
    mean = s/n;
    cout << "Mean = " << mean << endl << endl;

    // Free the memory and continue the loop
    delete [] x;

  }

  return 0;
}
