#include #include #include /* Here are the 'function prototypes', specifying the format for all parameters. */ void AllSolve(double (*f)(double), double a, double b, double L, double tol, double* Roots, double* PossibleRoots, int sizeRoots, int* nroots, int* npossroots); double myf(double x); // End of function prototypes. int main(int argc, char* argv[]) { double myRoots[100]; double myPossibleRoots[100]; double mya=0.0; double myb=1.0; double myL = 2; double mytol = 1.e-3; int mysizeRoots = 100; int i, mynroots, mynpossroots; AllSolve(myf, mya, myb, myL, mytol, myRoots, myPossibleRoots, /* No & here, because array names indicate their addresses. */ mysizeRoots, &mynroots, &mynpossroots); printf("\n"); /* Here is an example of how to display the results. */ printf("myRoots:\n"); for (i=0; i<2*mynroots; i+=2) { printf(" %f, %f \n",myRoots[i],myRoots[i+1]); } printf("\n"); printf("myPossibleRoots:\n"); for (i=0; i<2*mynpossroots; i+=2) { printf(" %f, %f \n",myPossibleRoots[i],myPossibleRoots[i+1]); } } void AllSolve(double (*f)(double), double a, double b, double L, double tol, double* Roots, double* PossibleRoots, int sizeRoots, int* nroots, int* npossroots) { double ans; if (sizeRoots > 3) { Roots[0] = (a+b)/2.0; Roots[1] = Roots[0] + tol; Roots[2] = 3.0*(a+b)/4.0; Roots[3] = Roots[2] + tol; *nroots = 2; /* Note that we change the contents of nroots, not the pointer itself. If we leave off the star, gcc will complain about a "cast". */ /* Note that star can mean 'contents of' or 'multiply'. */ } else { *nroots = 0; } if (sizeRoots > 1) { PossibleRoots[0] = (a+b)/4.0; PossibleRoots[1] = PossibleRoots[0] + tol; *npossroots = 1; } else { *npossroots = 0; } ans = f(a); /* This is how the function would be called from AllSolve. */ printf("The result of the function call in AllSolve is f(%f) = %f\n", a, ans); } double myf(double x) { return x*x - 2.0; }