// Determine the mean and standard deviation of values in an array [method 2]
// m1 is the mean of the values
// m2 is the mean of the squares of the values
// See http://en.wikipedia.org/wiki/Standard_deviation and
// http://en.wikipedia.org/wiki/Expectation_value/
#include <iostream>
#include <cmath>

int main ()
{
  const int n = 10; // number of elements in the array
  double temperature[n] = { 23.5, 32.8, 33.9, 31.3, 26.9,
                            21.3, 19.4, 23.7, 26.9, 31.6 };

  double m1=0., m2=0.;
  for (int i=0; i<n; i++) {
    m1 += temperature[i];
    m2 += (temperature[i]*temperature[i]);
  }
  m1 /= n;
  m2 /= n;
  double sd = sqrt(m2-m1*m1);

  std::cout << "the mean is " << m1 << std::endl;
  std::cout << "the   sd is " << sd << std::endl;

  return 0;
}
