And you were not able to think of a solution? Consider the function 'abs', which apparently has definitions based on http://www.cplusplus.com/reference/clibrary/cmath/abs/ (a quick google for 'abs iostream' reveals this)
1 2 3 4 5
double abs ( double x );
float abs ( float x );
longdouble abs ( longdouble x );
In your code, a-b is an int subtracted by an int which also returns an int. Is there a function 'int abs(int x)' above? If not, is there a way to force the compiler to choose on of the available functions?
hey rollie thanks! I was able to compile the program by your suggestion but still i was not able to get the correct root that was 3.54337. my program looked like
however I tried to do the program to do by do while statement to make it more logical but still i was not able to get the correct root value. The program looked like
#include <iostream>
#include <cmath>
usingnamespace std;
int n;
double bisection (int z)
{
double r;
r= 2 * (z)^3 - 11*z - 50;
return (r);
}
int main (int argc, char * const argv[]) {
double fa;
int x;
double fb;
double m, product;
double a, b;
cout << "Please enter your a value " << endl;
cin >> a;
cout << "Please enter your b value " << endl;
cin >> b;
cout << "How many iterations do you want to do ";
cin >> x;
while (n<x) {
fa= bisection (a);
fb = bisection (b);
product = fa * fb;
if (product < 0) {
b=m;
}
else {
a=m;
}
m = (a+b)/2;
n++;
}
cout << "The root value is " << m << endl;
return 0;
}
I haven't reviewed everything in the above but, I doubt the line r= 2 * (z)^3 - 11*z - 50; does what you think it does...z^3 is not z*z*z; ^ is the bitwise XOR operator.