#include<iostream>
#include<cmath>
#include<iomanip>
#define f(x) (6)*(x)*(x)*(x) + (5)*(x)*(x) + (7)*(x) - 2
usingnamespace std;
int main()
{
int i = 1;
float a,b,c,e1,e2;
double a1,b1,c1,d;
cout<<"Welcome to the root solver."<<endl;
cout<<"We are going to find the root of 6x^3+5x^2+7x-2=0 using Bisection Method."<<endl;
cout<<"Enter the upper limit: ";cin>>a;
cout<<"Enter the lower limit: ";cin>>b;
cout<<"Enter the stopping error: ";cin>>e1;
while (a<b)
{
cout<<"Your lower limit is larger than your upper limit. Please enter the correct limits."<<endl;
cout<<"Enter the upper limit: ";cin>>a;
cout<<"Enter the lower limit: ";cin>>b;
}
a1 = f(a);
b1 = f(b);
if (a1==0)
{
c = a;
}
elseif (b1==0)
{
c = b;
}
else
{
while (a1*b1>0)
{
cout<<"The root is not found within the given limits. Please enter another limits."<<endl;
cout<<"Enter another upper limit: ";cin>>a;
cout<<"Enter another lower limit: ";cin>>b;
while (a<b)
{
cout<<"Your lower limit is larger than your upper limit. Please enter the correct limits."<<endl;
cout<<"Enter the upper limit: ";cin>>a;
cout<<"Enter the lower limit: ";cin>>b;
}
a1 = f(a);
b1 = f(b);
}
cout<<"_______________________________________________________________________________"<<endl;
cout<<"Iteration\tXl\t Xu\t Xr\t Error"<<endl;
cout<<"_______________________________________________________________________________"<<endl;
cout<<" "<<"0"<<"\t "<<fixed<<setprecision(4)<<b<<" "<<a<<endl;
do
{
c = (a + b)/2;
c1 = f(c);
a1 = f(a);
b1 = f(b);
cout<<" "<<i<<"\t "<<fixed<<setprecision(4)<<b<<" "<<a<<" "<<fixed<<setprecision(8)<<c<<" "<<fixed<<e2<<endl;
if (c1==0)
{
cout<<"The root is "<<c<<"."<<endl;
}
elseif (a1*c1<0)
{
b = c;
e2 = a - b;
}
else
{
a = c;
e2 = a - b;
}
i++;
}while(fabs(e2)>e1);
}
cout<<"_______________________________________________________________________________"<<endl;
cout<<endl;
cout<<"The root is "<<fixed<<setprecision(8)<<c<<"."<<endl;
system("pause");
return 0;
}
If you run this code in the error part it will show a -1.#QNAN00 and after that is 0.5. I want to remove the -1.#QNAN00 and move the succeeding number up by one .