#include <bits/stdc++.h>
using namespace std;
double computeFunction(double x) {
return x * x * x - 4.0;
}
int main() {
double a, b, c, f_a, f_b, f_c, tolerance = 0.0001, root;
int iteration = 0;
while (true) {
cout << "Enter two initial guesses: ";
cin >> a >> b;
f_a = computeFunction(a);
f_b = computeFunction(b);
cout << "f(a) = " << f_a << ", f(b) = " << f_b << endl;
if (f_a * f_b < 0) break;
else cout << "Invalid interval. Try again.\n";
}
c = (a - f_a * (b - a)) / (f_b - f_a);
f_c = computeFunction(c);
if (f_c == 0.0) {
cout << fixed << setprecision(8);
cout << "Exact root found: " << c << endl;
return 0;
}
cout << "Iter\tc\t\ta\t\tb\t\tf(c)\t\tf(a)\t\tf(b)\n";
cout << iteration << "\t" << fixed << setprecision(8)
<< c << "\t" << a << "\t" << b << "\t"
<< f_c << "\t" << f_a << "\t" << f_b << endl;
while (true) {
c = (a - f_a * (b - a)) / (f_b - f_a);
f_c = computeFunction(c);
iteration++;
cout << iteration << "\t" << fixed << setprecision(8)
<< c << "\t" << a << "\t" << b << "\t"
<< f_c << "\t" << f_a << "\t" << f_b << endl;
if (fabs((b - a) / b) < tolerance || iteration > 8) {
break;
}
if (f_a * f_c < 0) {
b = c;
f_b = f_c;
} else {
a = c;
f_a = f_c;
}
root = c;
}
cout << fixed << setprecision(8);
cout << "Approximate root = " << root << endl;
return 0;
}