/****************************************************
Write a program that reads in the file numbers.txt
and prints out the standard deviation of the numbers
there. The file numbers .txt looks like this:
N = 3
DATA:
67
61
82
I.e. there's a header that tells you how many numbers
are in the file, and then you get all the numbers.
Recall that the standard deviation is
______________________
/ -- n
/ \ (xi - ave)^2
/ | ------------
/ / n - 1
\/ -- i = 1
****************************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Open input file, read header, get N = # of int's
ifstream IN("numbers.txt");
char c;
int N;
string s;
IN >> c >> c >> N >> s;
// Create array A and read N int's from input
int *A = new int[N];
for(int i = 0; i < N; i++)
IN >> A[i];
// Cycle through array summing all the elements
int sum = 0;
for(int j = 0; j < N; j++)
sum = sum + A[j];
double average = double(sum)/N;
// Cycle through the array summing the squares of
// the elements minus the average
double sqsum = 0;
for(int j = 0; j < N; j++)
sqsum = sqsum + (A[j]-average)*(A[j]-average);
double stddev = sqrt(sqsum/(N-1));
// Print out standard deviation
cout << "The standard deviation is " << stddev << endl;
return 0;
}