So i am trying to write a bisection method code for finding the roots of a given equation (x^3 - 6x^2 +11x -6), but my code keeps getting errors and I am unsure what to change.
The purpose I'd like to achieve is to have the user type in the interval and number of loop iterations, and have the code spit out the root.
void main()
{
float a, b, m;
int count = 0;
int iter;
cout << "Enter a = "; //Low bound (1.5 for this problem)
cin >> a;
cout << "Enter b = "; //High bound (4 for this problem)
cin >> b;
cout << "Enter number of iterations = ";
//Number of iterations loop will run before breaking
cin >> iter;
//Here is what the f(a) and f(b) are:
// int f(a) = fa = (a^3) - 6*(a^2) + (11*a) -6;
// int f(b)= fb = (b^3) - 6*(b^2) + (11*b) - 6;
The problem is the the while condition at line 54. The condition you've given is when you want to exit the loop, not when you want to repeat it. Also, you need to use fabs() instead of abs. abs() is for integers. So the correct statement is while (fabs(a - b) >= 0.000001 && f(n) != 0);
What errors are you getting? If they are compiler errors post the complete error listing, exactly as they appear in your development environment.
Also if you're going to use the outdated conio.h header file you should be using angle brackets <> not quotation marks " ". Quotation marks are used primarily for include files that are located relative to the project. Angle brackets are usually for include files that are in system/compiler specified directories. This header file is an implementation defined header file located in one of the system/compiler specified directories.