//Include Files go here
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <math.h>
#include <iostream>

//End include files

//Global constants go here
const int max_file_name = 100;
const double pi = atan(1.0)*4;
const int max_line_length = 100;
const int max_name_length = 50;
const int max_descript_length = 80;
// End Globals

//Put function prototypes here (don't forget semicolons
double f(double x);

using namespace std;
//g++ simple.cpp -o simple <--- this will compile and link it!

//Main program goes here
int main(int argc, char *argv[])
{
	double xl = 0, xu = 1, xr, xr_old, es = 0.01, ea = 1.1*es;
	bool sign_of_fxl_times_fxu_less_than_zero = true;
	bool sign_of_fxl_times_fxr_less_than_zero = true;
	bool xr_is_the_root = false;
	
	if(f(xl)*f(xu) < 0)
	{
		//do nothing
	}
	else if(f(xl)*f(xu) > 0)
	{
		sign_of_fxl_times_fxu_less_than_zero = false;
	}
	else
	{
		xr_is_the_root = true;
	}
	
	if(xr_is_the_root)
	{
		if(f(xl) == 0)
		{
			//root_is_xl = true;
			cout << endl << "Root is " << xl << endl;
			exit(0);
		}
		else if(xr_is_the_root)
		{
			//root_is_xu = true;
			cout << endl << "Root is " << xu << endl;
			exit(0);			
		}
	}
	else if(!sign_of_fxl_times_fxu_less_than_zero)
	{
		cout << endl << "xl and xu do not bracket the root" << endl;
		exit(0);
	}
	xr = (xl + xu)/2;
	
	int n = 1;	
	while(ea > es)
	{
		if(f(xl)*f(xr) < 0)
		{
			xu = xr;
		}
		else if(f(xl)*f(xr) == 0)
		{
			//root_is_xl = true;
			cout << endl << "Root is " << xr << endl;
			exit(0);
		}
		else
		{
			xl = xr;
		}
		n++;				
		xr_old = xr;
		xr = (xl + xu)/2;
		ea = fabs((xr - xr_old)/xr);
		cout << endl << n << "\t\t" << xr << "\t\t" << xr_old << "\t\t" << ea;
	}
return 1;
	

}

double f(double x)
{
	double function = exp(-x) - x;
	return function;

}








