#include <iostream>
#include <fstream>
#include <cmath>
int main()
{
constchar file_name[] = "C:\\Users\\Josh\\Desktop\\scores.dat" ;
double sum = 0 ;
int count = 0 ;
{
// sum up all the numbers in the file
std::ifstream file(file_name) ; // open the file for input
int number ;
while( file >> number ) // for each number in the file
{
sum += number ;
++count ;
}
// the file is automagically closed at the end of the scope
}
if( count > 0 ) // if we read anything at all
{
// compute the mean
constdouble mean = sum / count ;
// read each number and compute the variance
double sum_square_of_dev_from_mean = 0 ;
{
std::ifstream file(file_name) ; // open the file once again
int number ;
while( file >> number ) // for each number
{
// sum up squares of deviation from mean
constdouble deviation_from_mean = number - mean ;
sum_square_of_dev_from_mean += deviation_from_mean * deviation_from_mean ;
}
}
constdouble variance = sum_square_of_dev_from_mean / count ;
// standard deviation is the square root of the variance
constdouble standard_deviation = std::sqrt(variance) ;
// TO DO: print out the results
}
}