/****************************************************************/
/*  FcnPtr.cpp  -  Demonstrating the use of function pointers   */
/*                 (actually, arrays of function pointers...)   */
/****************************************************************/

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

/****************************************************************/

const double PI  =  3.14159265;

double Identity (double dArgument)
{
    return dArgument;
}

double Radians (double dDegrees)
{
    return (dDegrees * PI) / 180.0;
}

/****************************************************************/
/*  Let C++ calculate the appropriate length of this 1-D array  */
/*  of pointers to common single-argument double valued fcns.   */
/****************************************************************/

double (*pFcn []) (double dArg) =
    {  Identity, sin, cos, tan, sqrt };

char *sNames [] =
    {  "Rad", "sin", "cos", "tan", "sqrt"  };

const int    nNumFcns  =  sizeof (pFcn) / sizeof (pFcn [0]);

/****************************************************************/

int main (void)
{
    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);

    cout << "Demonstrating the use of function pointers" << endl << endl;
    cout << "There are " << nNumFcns << " functions..." << endl << endl;
    cout << setw(6) << "Deg";
    for (int i = 0; i < nNumFcns; i++)
        cout << setw(14) << sNames [i];
    cout << endl;

    for (double dDegrees = 0.0; dDegrees <= 180.0; dDegrees += 15.0)
    {
        cout << setw(6) << setprecision(1) << dDegrees;
        cout.precision(3);
        for (int i = 0; i < nNumFcns; i++)
            cout << setw(14) << (*pFcn[i]) (Radians(dDegrees));
        cout << endl;
    }

    cout << endl << "Fini" << endl << endl;
    return 0;
}

/****************************************************************/

/*  The output...

Demonstrating the use of function pointers

There are 5 functions...

   Deg           Rad           sin           cos           tan          sqrt
   0.0         0.000         0.000         1.000         0.000         0.000
  15.0         0.262         0.259         0.966         0.268         0.512
  30.0         0.524         0.500         0.866         0.577         0.724
  45.0         0.785         0.707         0.707         1.000         0.886
  60.0         1.047         0.866         0.500         1.732         1.023
  75.0         1.309         0.966         0.259         3.732         1.144
  90.0         1.571         1.000         0.000 557135115.022         1.253
 105.0         1.833         0.966        -0.259        -3.732         1.354
 120.0         2.094         0.866        -0.500        -1.732         1.447
 135.0         2.356         0.707        -0.707        -1.000         1.535
 150.0         2.618         0.500        -0.866        -0.577         1.618
 165.0         2.880         0.259        -0.966        -0.268         1.697
 180.0         3.142         0.000        -1.000        -0.000         1.772

*/
