. Your program must include a main program that opens an input file, a function that reads the content of the file into a partially filled array, and a function that computes and returns the standard deviation of the array’s content. You must use the formula for standard deviation that is presented in the textbook’s statement of the project. The main program must display the resulting standard deviation on the screen.
Code:
#include <fstream>
#include <iostream>
using namespace std;
float average (float vec[], int size);
float deviation( float sum, float average,float N );
float sum (float vec[], int size);
int main ()
{
ifstream inputfile;
float N;
int size = 0;
const int Num_List = 1000;
float summ, avg;
double List[Num_List];
inputfile.open("program8.txt");
if(inputfile.fail())
{
cout << "Error: Could'nt open program8.txt" << endl;
exit (EXIT_FAILURE);
}
for (int count=0; count < Num_List; count++)
inputfile >> List[count];
float standDeviation [100];
inputfile >> N;
while(!inputfile.eof())
{
standDeviation[size] = N;
size++;
inputfile >> N;
}
summ = sum (standDeviation, size);
avg = average(standDeviation, size);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout << "Standard Deviation: " << deviation(summ, avg, size) << endl;
inputfile.close();
cin.get();
cin.get();
return 0;
}
//-----------------------------------------------------------------------------
// sum function
//-----------------------------------------------------------------------------
float sum (float vec[], int size)
{
float sum;
sum = 0.0;
for (int N = 0; N < size; N++)
{
sum +=vec[N] ;
}
return sum;
}
//-----------------------------------------------------------------------------
// average function
//-----------------------------------------------------------------------------
float average (float vec[], int size)
{
float sum, average;
sum = 0.0;
for (int i = 0; i < size; i++)
sum += vec[size];
if (size == 0)
average = 0.0;
else
average = sum / size;
return average;
}
//-----------------------------------------------------------------------------
// deviation function
//-----------------------------------------------------------------------------
#include <cmath>
float deviation(float sum, float average, float N)
{
double S;
double U;
U = ( sum - average ) * ceil (2);
S = (sqrt (U / N));
return S;
}