This is the question: Define a function that takse a partially filled
array of numbers as its arguments and returns the standard deviation of
the numbers in the partially fille array. Since a partially filled array
requires two arguments, the function will automatically have two formal
parameters: An array parameter and a formal parameter of type int that
gives the number of array positions used. The numbers in the array will be
of type double. Embeded your function in a suitable test program.
I am having some problems regarding filling the array and getting the standard deviation. Is there something wrong with my function or how can I declare my array and fill it? This chapter in class is driving me crazy..
#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);
}