In: Computer Science
In C++, 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 variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables 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 LINES OF THE PROGRAM MUST BE COMMENTED.
349.5
376.2
36.9
283.5
361.0
381.0
344.7
328.5
368.3
370.5
360.6
426.9
434.7
Help please and comment on every line.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// dataFile999.txt
349.5
376.2
36.9
283.5
361.0
381.0
344.7
328.5
368.3
370.5
360.6
426.9
434.7
________________
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int cnt;
double mean,stdDev;
//Function declarations.
void calMean(double vals[]);
void calStdDev(double vals[]);
int main()
{
//setting the precision to two decimal places
std::cout << std::setprecision(2) <<
std::fixed;
//Declaring variables
ifstream dataIn;
double val;
//Opening the input file
dataIn.open("dataFile999.txt");
if(dataIn.fail())
{
cout<<"** File Not Found **"<<endl;
return 1;
}
else
{
while(dataIn>>val)
{
cnt++;
}
dataIn.close();
// Creating array dynamically
double* vals = new double[cnt];
//Opening the input file
dataIn.open("dataFile999.txt");
/* Reading the
data from the file and
* populate the
values in the array
*/
for(int i=0;i<cnt;i++)
{
dataIn>>val;
vals[i]=val;
}
//calling the
functions
calMean(vals);
calStdDev(vals);
cout<<"Mean :"<<mean<<endl;
cout<<"Standard Deviation :"<<stdDev<<endl;
}
return 0;
}
void calMean(double vals[])
{
double sum=0;
for(int i=0;i<cnt;i++)
{
sum+=vals[i];
}
mean=sum/cnt;
}
void calStdDev(double vals[])
{
//Declaring local variables
double standard_deviation = 0.0, variance = 0.0, sum_of_squares =
0.0;
/* This loop Calculating the sum of
* square of eeach element in the array
*/
for (int i = 0; i < cnt; i++) {
/* Calculating the sum of square of
* each element in the array
*/
sum_of_squares += pow((vals[i] - mean), 2);
}
//calculating the variance of an array
variance = ((double) sum_of_squares / (cnt - 1));
//calculating the standard deviation of an array
stdDev = sqrt(variance);
}
_____________________
Output:
_______________Could you plz rate me well.Thank You