My assignment is to find the sum of an array, sum of the cubes of the array (i.e. each value raised to the power 3) and the variance of the array. At the moment I'm getting compilation errors so I can't do any trial and error. My problem conceptually is that the array is a file (array.dat) so I'm a bit confused on how to access the values of the array using my professors template. Here is what I have, and any pointers are appreciated!

#include<iostream>
#include <math.h>
#include <fstream>

using namespace std;

float sum (float (*) (float, int), int*, int s);
float cubic(int) ;
float var (float, int);
ifstream infile ("array.dat", ios::in);


int main(){
    int array = 10;
    int s = 0; 
    int a[array];
while(infile >> a[s]) {s++;}
	cout << "Sum = " << sum(NULL, a, s) << endl;  
cout << "Cubic Sum" << sum(cubic, a, s) << endl;   
   cout << "\n\n\nPress any key to close console window:  ";
   char c; cin >> c;
   return 0;
}

//calculate sum
float sum (float (*pf) (float, int), int *n, int s) {
	float sum=0; 
    int *p=n;
	for (int i=0; i<s; i++) {
			if (pf == NULL) sum+= *p;
        			//else if ...
			else sum+= (*pf)(sum, *p);
			p++; 
    }
   return sum;
}

//calculate sum of cubes
float cubic(int cubic){
      //int cube = 0;
      //for (int j=0; j<s; j++) {
      int cube = pow(cube,3);
      
return cube;
}

What errors are you getting?
Just copy each array value from the fileinto a array or vector in your program.

In function `int main()':
20 invalid conversion from `float (*)(int)' to `float (*)(float, int)'
20 initializing argument 1 of `float sum(float (*)(float, int), int*, int)'

Also, I can't tell if in the template there is a loop that runs each function and then sums up the results or if I need to write a loop in each function to find the sum

If line 17 is a comment in the following code, the program runs and finds Mean and Variance correctly. However, with line 17 left as code it returns the compilation error:
In function `int main()':
17 invalid conversion from `float (*)(double)' to `float (*)(float, int)'
17 initializing argument 1 of `float sum(float (*)(float, int), int*, int)'

Any ideas as to what this error means? Here is the new code:

//variance.cpp
#include<iostream>
#include <math.h>
#include <fstream>
using namespace std;
float sum (float (*) (float, int), int*, int s);
float cubic(double) ;
float var (float, int);
ifstream infile ("array.dat", ios::in);    //input data file
 
int main(){
   int size = 10;
int s = 0;
int a[size];
while (infile >> a[s]) {s++;} 
    cout << "Mean is " << sum(NULL, a, s) << endl;  // passing a NULL pointer to calculate mean
    cout << "Cubic Sum" << sum(cubic, a, s) << endl;
    cout << "Variance is " << sum(var, a, s) << endl;  // passing pointer to function var()
    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;
    float mean=0.0;
    int *p=n;
    for (int j=0; j<s; j++) {mean+=*n; n++;}
    mean = mean/s;
    for (int i=0; i<s; i++) {
       if (pf == NULL) sum+= *p;      
       else sum+= (*pf)(mean, *p);
      p++;
    }
   return sum/(s);
}

float cubic (double cube) {return pow(cube,3);}

float var (float ave, int k) {return pow((ave-k),2);}

In this prototype float sum (float (*) (float, int), int*, int s); what are you trying to accomplish with the first parameter float (*) (float, int) ?

Does the code have to be this complex or are you allowed to simplify it?

The instructions for this assignment was to do the assigned operations by passing a function as a parameter. I simply started with a template my professor provided, which already had the sum function declared like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.