In: Computer Science
How do I create this program? Using C++ language!
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.
readvalues.txt (save this file under D Drive.Then the path of the file pointing to it is D:\\readvalues.txt)
98
93
72
86
99
61
89
77
91
73
79
71
86
74
19
______________________
#include <iostream>
#include <fstream>
#include <iomanip> // std::setprecision
#include <cmath>
using namespace std;
//Declaring global variables
double mean,standard_deviation;
//Function declarations
void calAvg(int grades[],int count);
void standardDeviation(int grades[],int count);
int main()
{
//defines an input stream for the data file
ifstream dataIn;
//Declaring variables
int nos,count=0;
//Opening the input file
dataIn.open("D:\\readvalues.txt");
while(dataIn>>nos)
{
count++;
}
//Closing the data input stream
dataIn.close();
//Create an integer array which is of size based on count
value
int grades[count];
//Opening the input file
dataIn.open("D:\\readvalues.txt");
/* Getting the values from the txt file and
* populate those values into grades[] array
*/
for(int i=0;i<count;i++)
{
dataIn>>nos;
grades[i]=nos;
}
dataIn.close();
//Calling the function by passing the grades[] array as
argument
calAvg(grades,count);
standardDeviation(grades,count);
//Setting the precision to Two decimal places
std::cout << std::setprecision(2) << std::fixed;
//displaying the mean and standard deviation
cout<<"Mean is "<<mean<<endl;
cout<<"Standard Deviation is
"<<standard_deviation<<endl;
return 0;
}
//Function implementation which calculates the average value of
grades[] array
void calAvg(int grades[],int count)
{
//Declaring local variables
double sum=0.0;
//calculating the sum of grades[] array elements
for(int i=0;i<count;i++)
{
//calculating the sum
sum+=grades[i];
}
//calculating the average
mean=sum/count;
}
//This function which claculates the standard deviation
void standardDeviation(int grades[],int count)
{
int sum=0;
double variance;
//This loop Calculating the sum of square of eeach
element in the deviation[] array
for(int i=0;i<count;i++)
{
//Calculating the sum of square of eeach element in
the deviation[] array
sum+=pow(grades[i]-mean,2);
}
//calculating the standard deviation of grades[]
array
variance=(double)sum/(count-1);
standard_deviation=sqrt(variance);
}
___________________
output:
____________Thank You