Write a C+- program that computes, and outputs the mean (average) and standard d
ID: 3631046 • Letter: W
Question
Write a C+- program that computes, and outputs the mean (average) and standard deviation of a set of floating point values that are stored in an input file called "inputFile-l.txt". This sample data file that you must use to test your program is posted next to the project link. The number of floating point values, n. is stored as an integer value in the first data item in the file then the actual data values follow, one per line The formula for the standard deviation (s) is given below where x' is the mean Your output should be labeled clearly and formatted neatly. Floating point output should be displayed using two decimal points The summation symbol. can be expanded as follows: (x1 - x')2 (x1 - x') + (x1 - x')2 (x2 - x') + . . . . . +(x2 - x')2(x2 - x'). Your program must also check if the input data file was opened properly and only proceed if so. To be able to compute the average, your program needs to first open the data file, read the first value then use the first read value to determine how many iteration loop will have. Inside the loop, the program should read individual floating point data items and sum them up To compute the standard deviation, you need to close the input data file then reopen it and check again Make sure you use a different input file stream variable name to ensure proper reading of values for the second time In a similar loop, the program will read the floating points values again and use them to compute the summation expression above and hence the standard deviation You MUST test your program with the given data file and your snapshot should match the one given belowExplanation / Answer
//Header file section
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
//prototype function
double Average(double x[],int n);
double standard_Deviation(double x[], double average,int n);
//main fuction
void main()
{
//Declartion
ifstream inputFile;
ofstream outputFile;
double x[100];
double mean,sdeviation;
int i,n;
//file section
inputFile.open("inputFile_1.txt");
if(!inputFile)
cout<<"Error: ";
else
{
inputFile >> n;
}
inputFile.close();
cout<<"Enter values:"<<endl;
for(i=0;i<n;i++)
cin>>x[i];
mean=Average(x,n);
sdeviation=standard_Deviation(x,mean,n);
//output file
outputFile.open("Output.txt",ios::out);
cout<<"Now data writing to file ";
outputFile <<mean;
outputFile <<sdeviation;
outputFile.close();
cout<<"Done";
system("pause");
}
double Average(double x[],int n)
{
double m=0;
for(int i=0;i<n;i++)
m=m+x[i];
return m/n;
}
double standard_Deviation(double x[],double mean,int n)
{
double sd=0;
for(int i=0;i<n;i++)
sd=sd+pow((x[i]-mean),2);
sd=sqrt(sd/n);
return sd;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.