I have been working on this for a few days now and I still can't wrap my head around arrays and filling in arrays to compute the Standard Deviation of an input file.
This is what I have:
Code:
#include <fstream>#include <iostream>
#include <cmath>
using namespace std;
double get_standard_dev( double Num [], int x );
const int size= 100;
int main ()
{
int x, size;
double Num[x];
ifstream in_stream;
cout << "We are going to find the standard deviation for the file stddev.dat" << endl;
// Opens the input file.
in_stream.open ( "stddev.dat" );
// If the file fails to open it will close the program and output the statement.
if ( in_stream.fail( ) )
{
cout << "Please check if the file is saved properly. It could not open." << endl;
}
cout << get_standard_dev( Num, size) << endl;
return 0;
}
double get_standard_dev( double Num [], int x )
{
double average, temp, mean;
double sum = 0;
int i;
for( i = 0; i < x;i++ )
{
sum += Num[i];
}
average = sum / x;
for( i = 0; i < x;i++ )
{
temp = Num[i] - average;
sum += temp * temp;
}
mean = sum / x;
return sqrt(mean);
}