# Calculate Standard Deviation!!!

I am having trouble with calculations of standard deviation & variance! The calculations aren't coming out correctly! Can someone please help me

`

# include

using namespace std;

int main ()
{

``````cout << "\nThis program will produce statistics (Mean, Standard Deviation, "
"Maximum and Minimum values of the list) for a list of integer values."
"The user will provide the names of input and output files.\n" ; // << endl;

cout <<"\nEnter name and location for input file:   " ;
string file_input ;
getline(cin, file_input);
ifstream fin( file_input.c_str() ) ;

if(fin.fail())
{
cout << "Bad file name or location.\n" ;
return 0 ;
}

cout <<"Enter name and location for output file:   ";
string file_output ;
getline(cin, file_output);
ofstream fout( file_output.c_str() );

cout << "\nReading values for first time. . .\n" ;
fout << "\nReading values for first time. . .\n" ;

int num;
int count = 0 ;
int total = 0 ;
int Min = 100;
int Max  = 0;
double sumsq = 0, variance, stdv;

fin >> num;
while( !fin.eof() )
{
cout << num << ' ' ;
fout << num << ' ' ;
if( ++count%10 == 0 )

{
cout << '\n' ;
fout << '\n' ;
}

{
total += num ;
if( num > Max ) Max = num ;
if( num < Min ) Min = num ;
fin >>num;

}
}

if(count > 0)
{
const double avg = double(total) / count ;
sumsq+= (num-avg)*(num-avg);

variance = sumsq/count;
stdv=sqrt(variance);

cout << "\n\nNumber of values read:  " << count;
cout << "\nMean of the values:  " << avg << '\n' ;
cout << "\nStandard deviation using method 1:   " << sumsq << " " << variance << " " << stdv;
cout << "\nGreatest value:  " << Max << '\n' ;
cout << "Least value:  " << Min << '\n' ;

fout << "\n\nNumber of values read:  " << count;
fout << "\n\nMean of the values :  " << avg << '\n' ;
fout << "\nStandard deviation using method 1:   ";
fout << "Greatest value :  " << Max << '\n' ;
fout << "Least value :  " << Min << '\n' ;
}

return 0;
``````

}`

• Well, your sumsq calculation doesn't look right. You are only doing the calculation for the last value read from the file. That part needs to be in a loop.

If you are concerned about checking the validity of your calculations, then you should be able to look at (print) the values at your critical points and verify that they look good.

There is a slightly easier way to do your first read-loop without resorting to an end-of-file check which are generally not done correctly by newbs but you do seem to have addressed correctly. You can get rid of the priming read and the end of loop read and just replace the eof test directly with the read operation as follows:

``````while ( fin >> num )
{
... rest of your loop code goes here (minus the extra read at the end)
}
``````