Bisection Method C++
Bisection Method C++
int main()
{
int iter = 0;
double a, b, c, e, fa, fb, fc;
cout.precision(4);
cout.setf(ios::fixed);
//calculation
while(fabs(a-b)>=e)
{
fa = f(a);
fb = f(b);
c = (a + b) / 2.0;
fc = f(c);
iter++;
cout << iter << setw(14) << a << setw(18) << b << setw(18) << c << setw(18)
<< fabs(a - b) << endl;
if (fc == 0)
{
cout << "The root of the equation is " << c << endl;
return 0;
}
else if (fa * fc > 0)
{
a = c;
}
else if (fa * fc < 0)
{
b = c;
}
}
cout << "The root of the equation is " << c << endl;
return 0;
}