write a program that can read a text file of numbers and calculate the mean and
ID: 3787036 • Letter: W
Question
write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED! (Use Dev C++)
Data are:
35
44
36
12
45
89
96
87
54
can anyone give me the full answer of this question, not only code. help me to get the full program, so I can just copy and paste it. I am very busy with other classes. so, please help me out!!!
***please put all the data manually in the
program...do not just make read data.txt...this is third times, I am posting this question. ..as when we are not using each data manually, it did not run ...but compile correctly. ..so use all data manually and comment each line please.
Hopefully, this time I will get the right program. ..
thank you.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
#include<cmath>
using namespace std;
int main() {
vector<int> v;
ifstream inputFile; // input file stream object
inputFile.open("data.txt"); // opening file
ofstream outputFile;
outputFile.open ("output.txt"); // output file open
int n;
int total = 0;
int standardDeviation = 0;
if (inputFile.is_open()) { //checking whether file can be opened or not
while (!inputFile.eof()) { //loop to get each value for file until End of file.
inputFile >> n; // reading each value into variable n
total = total + n; //making sum of all numbers in file
v.push_back(n); //adding each value from file to vector.
}
double mean = total/ (double)v.size(); //calculating mean
for(int i=0; i<v.size(); i++){
standardDeviation += pow(v[i] - mean, 2); //calculating standardDeviation
}
standardDeviation = sqrt(standardDeviation / (double)v.size());
cout<<"Mean: "<<mean<<endl;
outputFile << "Mean: "<<mean<<endl;
cout<<"Standard Deviation: "<<standardDeviation<<endl;
outputFile << "Standard Deviation: "<<standardDeviation<<endl;
cout<<"File has been generated."<<endl;
}
outputFile.close();//closing output file
inputFile.close();//closing input file
return 0;
}
data.txt
35
44
36
12
45
89
96
87
54
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Mean: 55.3333
Standard Deviation: 27
File has been generated.
Output.txt
Mean: 55.3333
Standard Deviation: 27
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.