We would like to calculate the (biased) variance of an array inputted from array:dat by passing a function as a parameter. In the following program variance.cpp, you need to define function

`float var (float, int);`

and modify function

`float sum (float (*) (float, int), int*, int s);`

to complete the program of calculating the variance. The code segments to be
defined are highlighted (in red) (also have //comments written)

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````//variance.cpp #include #include #include using namespace std; float sum (float (*) (float, int), int*, int s); float var (float, int); int main(){ int a[10] = {8,5,12,6,13,11,9,7,10,14}; // input from array.dat instead int s = (sizeof a)/4; cout << "Mean = " << sum(NULL, a, s) << endl; cout << "Variance=" << sum(var, a, s) << endl; cout << "\n\n\nPress any key to close console window: "; char c; cin >> c; return 0; } float sum (float (*pf) (float, int), int*n, int s) { float sum=0; int*p=n; ...// calculate mean for (int i=0; i
What I have so far...
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` `````` //variance.cpp #include #include #include using namespace std; float sum (float (*) (float, int), int*, int s); float var (float, int); int main(){ ifstream arrayfile; // me trying to input .dat file arrayfile.open ("array.dat"); // so lost? int s = (sizeof a)/4; cout << "Mean = " << sum(NULL, a, s) << endl; cout << "Variance=" << sum(var, a, s) << endl; cout << "\n\n\nPress any key to close console window: "; char c; cin >> c; return 0; } float sum (float (*pf) (float, int), int*n, int s) { float sum=0; int*p=n; ...// calculate mean ... I know its the average, but since i dont know how to input the .dat I can't start on this yet. for (int i=0; i
Do you have a question or problem?

I see a possible problem in your sum() function. You have named a variable with the same name as the function, probably a problem. You shouldn't name variables the same name as your functions.

1. You want to calculate the mean, variance, and sum of a collections of data. You might therefore want methods called CalculateMean(), CalculateVariance(), and CalculateSum(). You seem to want to use the same method to do all of these, depending on the values you pass in.
2. 'mean' is not defined in your sum() method so it's not compiling

Last edited on
Topic archived. No new replies allowed.