Write a program that takes its input from a file of numbers of type double. The program outputs to the screen the standard deviation of the numbers in the file. The file contains nothing but numbers of type double separated by blanks and/or line breaks. The standard deviation of a list of numbers x1, x2, x3, and so forth is defined as the square root of:
((x1 – a)2 + (x2 – a)2 + (x3 – a)2 + ...) / (n - 1)
Where the number a is the average of the numbers x1, x2, x3, and so forth and the number n is the count of how many numbers there are.
Your program should take file name as input from the user.
I have already created a file in the computer. When I finish the code and try to compiled it, it cannot open the file in the compiler. I don't know what is the problem going on. Can anyone give me some advices?
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double next, avg,var,stdDev, sum=0, sumSq=0;
int count=0;
cout << "Enter filename:" << endl;
std::string filename;
cin >> filename;
ifstream in_stream;
in_stream.open(filename.c_str());
while(in_stream >> next)
{
sum+=next;
sumSq+=(next*next);
++count;
}
avg=sum/count;
var=(count*sumSq-sum*sum) / (count*(count-1));
stdDev=sqrt(var);
in_stream.close();
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(3);
cout << "The standard deviation is " << stdDev << endl;
return 0;
}
[1]: https://i.stack.imgur.com/luJZx.png
[2]: https://i.stack.imgur.com/ofZGR.png
[3]: https://i.stack.imgur.com/xTFSL.png