It is very common in science and engineering to work with large data sets (large
ID: 3572316 • Letter: I
Question
It is very common in science and engineering to work with large data sets (large sets of numbers which are stored in a data file), which represent particular measurements in an experiment. We typically summarize the data set using statistical measurements. We are interested in the average (or mean), variance and statistical deviation of the data. The following mathematical equations represent these parameters: average: mu = 1/N sigma_i=1^N X_i is the i^th sample out of N samples variance: sigma^2 sigma_i=1^N (x_i - mu)^2/N - 1 standard variance: sigma = Squareroot sigma^2 Standard deviation is a measure of the amount of scatter on the measurements; the greater the sigma the more scattered the points are in the data set. Implement an algorithm the reads in a set of measurements and calculates the mean, variance and standard deviation on the input data set using a WHILE loop. Include your script file and a screen shot of your interactive session.Explanation / Answer
Here is the C++ code for you:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
double sumOfNumbers(double *array, int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
sum += *(array + i);
return sum;
}
double averageOfNumbers(double *array, int n)
{
return sumOfNumbers(array, n) / n;
}
double varianceOfNumbers(double *array, int n)
{
double sum = 0;
double avg = averageOfNumbers(array, n);
for(int i = 0; i < n; i++)
sum += pow(*(array + i) - avg, 2);
return sum / n;
}
double standardDeviationForVariance(double variance)
{
return sqrt(variance);
}
int main()
{
string fileName;
cout<<"Enter the name of the file to read from: ";
cin>>fileName;
ifstream fin;
fin.open(fileName);
if(!fin.is_open())
{
cout<<"Unable to open the file."<<endl;
return 0;
}
int numOfValues;
fin>>numOfValues;
double *array = (double *)malloc(sizeof(double) * numOfValues);
for(int i = 0; i < numOfValues; i++)
fin>>*(array + i);
double sum = sumOfNumbers(array, numOfValues);
double avg = averageOfNumbers(array, numOfValues);
double var = varianceOfNumbers(array, numOfValues);
double std = standardDeviationForVariance(var);
cout<<"The sum of values in the array is: "<<sum<<endl;
cout<<"The average of values in the array is: "<<avg<<endl;
cout<<"The variance of values in the array is: "<<var<<endl;
cout<<"The standard deviation of values in the array is: "<<std<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.