Write a program that reads data from a text file Include in
Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL INES OF THE PROGRAM MUST BE COMMENTED.
This is My egr111 class, please try to make the answer as easy as you can. This is first programming class for engineering. Please give the full answer with comments, so i can copy and paste it. Follow all the instructions what the professor gave. Please need help.
Thank you.
Solution
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <limits.h>
#include <math.h>
using namespace std;
double mean = 0.0, standardDeviation;
int totalData = 0;
int main()
{
string filename;
cout << \"Enter filename: \";
cin >> filename;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail())
{
cout << \"File does not exist\" << endl;
cout << \"Exit program\" << endl;
return 1;
}
double number,variance,standardDeviation, sum=0, sumSquare=0;
while(inFile >> number)
{
sum = sum + number;
sumSquare = sumSquare + (number*number);
totalData++;
}
mean=sum/totalData;
variance=(totalData*sumSquare-sum*sum) / (totalData*(totalData-1));
standardDeviation=sqrt(variance);
inFile.close();
cout << \"Mean: \" << mean << endl;
cout << \"Standard Deviation: \" << standardDeviation << endl;
return 0;
}
/*
input.txt
1
2
3
4
5
6
7
8
9
10
output:
Enter filename: input.txt
Mean: 5.5
Standard Deviation: 3.02765
*/

