PROGRAM 33
// FILE NAME: PROG33.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);
double smallest (double f, double g);
pause ();
const int max = 100;
int main ()
{
int sizef, sizeg;
double listf [max], listg [max];
decimals (cout, 2);
clrscr ();
sizef = makearray (1, 10, f, listf);
cout << endl << "MIN (F): " << min (listf, sizef);
pause ();
clrscr ();
sizeg = makearray (11, 20, f, listg);
cout << endl << "MIN (G): " << min (listg, sizeg);
pause ();
clrscr ();
cout << "SMALLEST (F AND G): " << smallest (min (listf, sizef), min (listg, sizeg));
pause ();
return 0;
}
makearray (int min, int max, double (*f) (int x), double listd [])
{
int count2 = 0;
cout << "HERE ARE THE ELEMENTS OF THE DOUBLE ARRAY: " << endl << endl;
for (int count = min; count <= max; count++)
{
listd [count2] = f (count);
cout << setw (16) << listd [count2] << endl;
count2 = count2 + 1;
}
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;
}
double smallest (double f, double g)
{
if (f > g)
{
return g;
}
else
{
return f;
}
}
pause ()
{
cout << endl << endl << "PRESS ENTER KEY TO CONTINUE ..... ";
getch ();
return 0;
}
// FILENAME: OURSTUFF.H, OURSTUFF.CPP
// SEE PROGRAM 14.
// OUTFILE: PROG33.OUT
HERE ARE THE ELEMENTS OF THE DOUBLE ARRAY:
100.03
100.28
101.07
102.84
106.07
111.25
118.81
129.06
142.15
158.11
MIN (F): 100.03
PRESS ENTER KEY TO CONTINUE .....
HERE ARE THE ELEMENTS OF THE DOUBLE ARRAY:
176.91
198.43
222.59
249.27
278.39
309.86
343.64
379.65
417.87
458.26
MIN (G): 176.91
PRESS ENTER KEY TO CONTINUE .....
SMALLEST (F AND G): 100.03
PRESS ENTER KEY TO CONTINUE .....
BACK TO COMP251 PAGE.