/* Kelvin Cheung
   1.12 
   PS 3
*/

#include<iostream.h>
#include<math.h>

class Newton {
private:
  double (*f)(double);
  double (*fprime)(double);
public:
  Newton(double (*eff)(double),double (*effprime)(double)) 
       // constructor which takes two function pointers (f and its derivative)
  {
    f=eff;
    fprime=effprime;
  }
  double find_root(double x0, double epsilon) 
       // given an initial guess and a tolerance, find_root computes and
       // returns the root Newton's method finds for f.
  { 
    double deltax;
    
    deltax=2.0*epsilon;
    while(fabs(deltax)>epsilon) {
      deltax=f(x0)/fprime(x0);
      x0=x0-deltax;
    }
    return x0;
  }
};

double f(double x)
     // M2 from the problem
{
  return -80*x*x+500*(x-1);
}

double fprime(double x)
     // derivate of f with respect to x
{
  return -160*x+500;
}

main()
{
  double guess,epsilon;

  cout << "Enter guess: ";
  cin >> guess;
  cout << "Enter epsilon: ";
  cin >> epsilon;

  Newton newt=Newton(f,fprime);
  cout << newt.find_root(guess,epsilon) << endl;
}
