#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;


double f(double x) {
    return cos(x) - x;
}

int main() {
    double x1, x2, x0, f1, f2, f0, E;
    int iteration = 1;

    cout << fixed << setprecision(6);

    
    cout << "Enter the value of x1: ";
    cin >> x1;
    cout << "Enter the value of x2: ";
    cin >> x2;
    cout << "Enter the stopping criterion E: ";
    cin >> E;

    f1 = f(x1);
    f2 = f(x2);

    
    if (f1 * f2 > 0) {
        cout << "The initial guesses do not bracket the root. Please choose different values." << endl;
        return 1;
    }

    cout << "\nIteration\tx1\t\tx2\t\tx0\t\tf(x0)\t\tf(x1)\t\tf(x2)" << endl;

    do {
        
        x0 = x1 - (f1 * (x2 - x1)) / (f2 - f1);
        f0 = f(x0);

       
        cout << iteration << "\t\t" << x1 << "\t" << x2 << "\t" << x0 << "\t" << f0 << "\t" << f1 << "\t" << f2 << endl;

        
        if (f0 == 0.0) {
            cout << "\nExact root found: " << x0 << endl;
            break;
        }

       
        if (f1 * f0 < 0) {
            x2 = x0;
            f2 = f0;
        } else {
            x1 = x0;
            f1 = f0;
        }

        
        if (fabs((x2 - x1) / x2) < E) {
            double root = (x1 + x2) / 2;
            cout << "\nApproximate root found: " << root << endl;
            break;
        }

        iteration++;
    } while (true);

    return 0;
}
