Hi all,
I am trying to read double numbers from a txt file and calculate average and standard deviation. It builds successfully but gives an error when it is run. The error I see on the screen is -1.#IND. Do you have any idea what it is and how can I correct it ?
I appreciate for helps,
My code is :
double avg_file(ifstream& source_file)
{
double number_in_file;
double total = 0;
int count = 0;
source_file >> number_in_file;
while (! source_file.eof())
{
total = number_in_file + total;
source_file >> number_in_file;
count ++;
}
//average of numbers in file
return (total/count);
}
double std_dev (ifstream& in_file, double average)
{
double number_in_file;
double std_dev_f;
double sum_diff = 0;
int count = 0;
in_file >> number_in_file;
while (! in_file.eof())
{
sum_diff = pow((average-number_in_file),2)+ sum_diff;
count++;
}
std_dev_f =sqrt(sum_diff/count);
return (std_dev_f);
}
int main()
{
ifstream fin;
//ofstream fout;
fin.open("numbers.txt");
if (fin.fail())
{
cout << "erorr on opening the file \n";
exit (1);
}
cout << "the calculated average is " << avg_file(fin) << endl;
cout << "the std devitation is " << std_dev(fin,avg_file(fin)) << endl;
}