1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
//variance.cpp
#include<iostream>
#include<fstream>
#include<string>
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<s; i++) {
if (pf == NULL) sum+= *p;
else sum+= (*pf)(mean,*p);p++;
}
return sum/(s);
}
float var (float mean, int k)
{
return... // declare correct return
}
|