// 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>
#include <cmath>

using namespace std;

typedef double (*fptr)(double x);

const double EPS = 0.0000001;

double f(fptr lhs, fptr rhs, double x)
{
	return lhs(x) - rhs(x);
}

double df(fptr lhs, fptr rhs, double x)
{
	return (f(lhs, rhs, x + EPS) - f(lhs, rhs, x)) / EPS;
}

double solve(fptr lhs, fptr rhs)
{
	double x = 0;
	double oldx = -1;
	while(fabs(x - oldx) > EPS)
	{
		oldx = x;
		x = oldx - f(lhs, rhs, oldx) / df(lhs, rhs, oldx);
	}
	return x;
}

double f1(double x)
{
	return -2 * x - 4;
}

double f2(double x)
{
	return 7 * x + 1;
}

int main()
{
	double res = solve(f1, f2);
	cout << res << " " << f1(res) << " " << f2(res) << endl;
	return 0;
}
