PROGRAM 32
// FILE NAME: PROG32.CPP PROGRAMMER NAME: ANTHONY F. ORTIZ // THIS PROGRAM DEMONSTRATES PASSING A FUNCTION AS A PARAMETER INSIDE OF // ANOTHER FUNCTION. #include #include #include #include #include "ourstuff.h" int makearray (int min, int max, double (*f) (int x), double listd []); double f (int x); double min (double listd [], int size); const int max = 100; int main () { int size; double listd [max]; decimals (cout, 2); clrscr (); size = makearray (-10, 10, f, listd); cout << "SMALLEST: " << min (listd, size); return 0; } makearray (int min, int max, double (*f) (int x), double listd []) { cout << "HERE ARE THE ELEMENTS OF THE DOUBLE ARRAY: " << endl; for (int count = min; count <= max; count++) { listd [count + 10] = f (count); cout << setw (16) << f (count) << endl; } return (abs (max - min)); } double f (int x) { return sqrt (fabs (pow (x, 4) + 5 * pow (x, 3) + 10000)); } double min (double listd [], int size) { double smallest = listd [0]; for (int count = 0; count < size; count++) { if (listd [count] < smallest) { smallest = listd [count]; } } return smallest; } // FILENAME: OURSTUFF.H, OURSTUFF.CPP // SEE PROGRAM 14. // OUTFILE: PROG32.OUT HERE ARE THE ELEMENTS OF THE DOUBLE ARRAY: 122.47 113.65 107.41 103.37 101.07 100.00 99.68 99.73 99.88 99.98 100.00 100.03 100.28 101.07 102.84 106.07 111.25 118.81 129.06 142.15 158.11 SMALLEST: 99.68
BACK TO COMP251 PAGE.