// ----------------------------------------------------------- // ftab.cpp Function tabulation ftab // ------------------------------------------------------------ #include // Stream library #include // String library #include // Math library // pointer to function double -> double typedef double (*fptype1)(double); // ----------------------------------------------------------- // function declarations double sqr(double x); void ftab(fptype1 f, char fs[20], double x1, double x2, int nx); // ---------------------------------------------------------- void main () { cout << " ------------------------ \n"; cout << " Welcome to ftab.cpp \n"; cout << " ------------------------\n"; ftab(sin,"sin",1,2,5); ftab(sqr,"sqr",1,2,3); } // ----------------------------------------------------------- // definition (implementation) void ftab(fptype1 f, char fs[20],double x1, double x2, int nx) { double dx = (x2-x1)/nx; cout << "\n"; double x, y; for (int i = 0; i <= nx; i++) { x = x1 + i*dx; y = f(x); cout << " " << fs << "(" << x << ") = " << y << "\n"; } } double sqr(double x) { return x*x; } // ----------------------------------------------------------- /* output ------------------------ Welcome to ftab.cpp ------------------------ sin(1) = 0.841471 sin(1.2) = 0.932039 sin(1.4) = 0.98545 sin(1.6) = 0.999574 sin(1.8) = 0.973848 sin(2) = 0.909297 sqr(1) = 1 sqr(1.33333) = 1.77778 sqr(1.66667) = 2.77778 sqr(2) = 4 */