Hi,
I was trying to implement a higher order function in C, summation over
a range with a given function. And the summation function is then used
to define sum_cubes. The program works fine when VAR is below 30000 or
around that, but as soon as VAR is bigger, say 40000, or 50000, then I
receive a segmentation error. Can anyone tell me why this is
happening? Thanks in advance.
#include <stdio.h>
#define VAR 30000
double sum(double(*func)(double), double a, double(*next)(double),
double b, double *result){
/*summation over func from a to b as incremented by next*/
if(a b)
return 0;
else{
*result += (*func)(a);
return sum(func, (*next)(a), next, b, result);
}
}
double inc(double n){
return (++n);
}
double cube(double n){
return n*n*n;
}
double sum_cubes(double a, double b){
double result = 0;
sum(&cube, a, &inc, b, &result);
return result;
}
int main(){
printf("sum_cubes(1, %d) = %d\n", VAR, sum_cubes(1, VAR));
return 0;
}