#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace 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, variance=0 ;
int count = 0 ;
int total = 0 ;
int Min = 100;
int Max = 0;
while( fin >> num ) {
cout << num << ' ' ;
fout << num << ' ' ;
if( ++count%10 == 0 )
{
cout << '\n' ;
fout << '\n' ;
}
total += num ;
if( num > Max ) Max = num ;
if( num < Min ) Min = num ;
}
double sumsq=0;
if( count > 0 )
{
constdouble avg = double(total) / count ;
sumsq+= num * num;
cout << "\n\nNumber of values read: " << count;
cout << "\nMean of the values: " << avg << '\n' ;
cout << "\nStandard deviation using method 1: ";
cout << "Greatest 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;
}
You will need to find one other number as you read the values from the file. You need the sum of the squares of the numbers:
totalSq = num1*num1 + num2*num2 + ...
So declare a double totalSq = 0.0;
and inside your while loop: totalSq += num*num;
Then double stdDev = sqrt( (totalSq - total*total/count)/count );
You will need to #include<cmath> for the sqrt function.
EDIT: I see you tried it at line 60, but that's too late. It belongs in the while loop.
You put everything in the while loop?
You just needed to add one line inside the while loop.
Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
double totalSq = 0.0;
while( fin >> num ) {
cout << num << ' ' ;
fout << num << ' ' ;
if( ++count%10 == 0 )
{
cout << '\n' ;
fout << '\n' ;
}
total += num ;
totalSq+= num*num;// new code
if( num > Max ) Max = num ;
if( num < Min ) Min = num ;
}
You then use the formula for stdDev in the lower code section
1 2 3 4 5 6 7 8
if( count > 0 )
{
constdouble avg = double(total) / count ;
sumsq+= num * num;
cout << "\n\nNumber of values read: " << count;
cout << "\nMean of the values: " << avg << '\n' ;
cout << "\nStandard deviation using method 1: " << sqrt((totalSq - total*total/count)/count);
Perhaps change total to type double, instead of int. Integer division may be affecting the result in sqrt((totalSq - total*total/count)/count)
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 ;
double total = 0 ;
int Min = 100;
int Max = 0;
double totalSq=0.0;
cout << "\n\nNumber of values read: " << count;
cout << "\nMean of the values: " << avg << '\n' ;
cout << "\nStandard deviation using method 1: " << " " << totalSq << " " << stdDev;
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;
}
you're right it worked perfectly! thank you for your help I really appreciate it. I was trying to use while(!fin.eof()) because I'm required to use it than (fin>>num) but it obviously causes problems