# Standard Deviation Function

• 12-03-2005
Standard Deviation Function
Code:

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

const int N = 1000;
void pass_array(double array[N]);

int main()
{
double x_bar, average();
double s_array[N], sam(0), s;
pass_array(s_array);

x_bar = average();

for (int i=0; i<=5; i++)
{
sam += pow((s_array[i] - x_bar),2);
}

s = sqrt(sam/(i-1));

cout << "sam=:" << sam << endl;
cout << "s=" << s << endl;
cout << "i=" << i <<endl;

return 0;
}

double average()
{
double av_array[N], sum(0), average;
pass_array(av_array);

for (int i=0; i<=5; i++)
{
cout << av_array[i] << endl;
}
for (int j=0; j<=(i-1); j++)
{
sum += av_array[j];
}

average = sum/j;
return average;
}

void pass_array(double array[N])
{
string filename;
ifstream data;
int count(0);

cout << "What is the filename where the data is located?" << endl;
cin >> filename;

data.open(filename.c_str());
if (data.fail())
{
cout << "Error opening file, check file status and try again." << endl;
}
else
while(count < (N-1) && !data.eof())
{
data >> array[count];
count++;
}

}

I need to call both the average and the standard deviation functions at different times, can anyone tell me how to write this so it doesn't ask me the name of the file twice.
• 12-03-2005
Salem
Pass the filename as a parameter.
• 12-03-2005