printf("\nThe value of:\n"); for(int k = 0 ; k < length ; k++) { printf("%.2lf%s", array[k], (k<length-1?" * ":"\n")); } printf(" is %.2lf\n", array_op(array, length, multiply)); return 0; }
/* Function to calculate a+b */ double add(double a, double b) { return a+b; }
/* Function to calculate a-b */ double subtract(double a, double b) { return a-b; }
/* Function to calculate a*b */ double multiply(double a, double b) { return a*b; }
/* Function to apply an operation, pfun, between successive pairs of elements */ double array_op(double array[], int size, double (*pfun)(double,double)) { double result = array[size-1]; int i = 0; /* Work from last to first to accommodate alternating signs for subtract */ for(i = size-1 ; i>0 ; i--) result = pfun(array[i-1], result); return result; }