Problem using Input data file and calculating mean. Please help :(

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)

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
      }
What I have so far...
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
37
38
39
40

//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(){
    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<s; i++) {
          if (pf == NULL) sum+= *p;
          else sum+= (*pf)(mean,*p);p++;
}
return sum/(s);
}
float var (float mean, int k) 
{
      return var, mean; // return the var and mean? Maybe do cout print of var/mean?
     
      }

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.

Two comments:
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.