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;
}