#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

double f(double x) {
    return exp(x) * log(x) - x * x;
}

double fPrime(double x) {
    return exp(x) * (log(x) + 1/x) - 2 * x;
}

void print(int step, double x, double dx) {
    cout.setf(ios::right, ios::adjustfield);
    cout << " " << setw(4) << step << "    ";
    cout.precision(15);
    cout.setf(ios::left, ios::adjustfield);
    cout.setf(ios::showpoint | ios::fixed);
    cout << setw(20) << x << "  " << setw(20) << dx << '\n';
}

int main() {

    cout << " Tangent search for root of exp(x)*log(x) - x*x\n"
         << " ----------------------------------------------\n"
         << " Enter guess x_0, and desired accuracy: ";
    double x, acc;
    cin >> x >> acc;

    cout << " Step            x                    dx\n"
         << " ----    ------------------    ------------------\n";
    int step = 0;
    double dx = 1;
    print(step, x, dx);
    while (abs(dx) > abs(acc)) {
        double slope = fPrime(x);
        if (slope == 0) {
            cerr << " Tangent horizontal ... try again!\n";
            return 1;
        } else {
            dx = - f(x) / slope;
            x += dx;
        }
        ++step;
        print(step, x, dx);
    }
}

