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

// ফাংশন f(x) = 2x² - x - 8
double f(double x) {
    return 2 * x * x - x - 8;
}

int main() {
    double x1, x2, x0, f1, f2, f0, error;
    int iteration = 1;
    const double EPSILON = 0.0001;

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

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

    // প্রাথমিক অনুমানগুলোর মধ্যে ফাংশনের মানের চিহ্ন বিপরীত কিনা পরীক্ষা
    if (f1 * f2 > 0) {
        cout << "The initial guesses do not bracket any root." << endl;
        return 0;
    }

    cout << "\nIteration    x1            x2            x0            f(x1)        f(x2)        f(x0)\n";

    do {
        // মধ্যবিন্দু নির্ণয়
        x0 = (x1 + x2) / 2.0;
        f0 = f(x0);

        // ফলাফল প্রদর্শন
        cout << fixed << setprecision(6);
        cout << iteration << "            "
             << x1 << "     " << x2 << "     "
             << x0 << "     "
             << f1 << "     "
             << f2 << "     "
             << f0 << endl;

        // নতুন সীমা নির্ধারণ
        if (f1 * f0 < 0) {
            x2 = x0;
            f2 = f0;
        } else {
            x1 = x0;
            f1 = f0;
        }

        // ত্রুটি নির্ণয়
        error = fabs((x2 - x1) / x2);
        iteration++;
    } while (error > EPSILON);

    cout << "\nApproximate root = " << x0 << endl;
    return 0;
}

