// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>

using namespace std;

double f1(double x)
{
    return x - 10;
}

double f2(double x)
{
    return x * x - 25;
}

const double SMALL = 0.0000001;

typedef double (*fptr)(double x);

double solve(fptr f)
{
    double xlast = 0;
    double x = 1;
    while(fabs(x-xlast)>SMALL)
    {
        xlast = x;
        x = x - f(x) / ((f(x+SMALL/2)-f(x-SMALL/2))/SMALL);
    }
    return x;
}

int main()
{
    cout << "x - 10 = 0 => x = " << solve(f1) << endl;
    cout << "x * x - 25 = 0 => x = " << solve(f2) << endl;
    return 0;
}
