# bisection method

• 10-30-2007
bar5037
bisection method
Hi.. I've been working on this bisection method program using some pseudocode of wikipedia, and I have gotten myself in a bind. The compiler tells me there is a syntax error with the last return in my program. I'll post it here. Any comments would be greatly appreciated.
Thanks,
Brian
ill highlight the error in red
Code:

```#include <iostream> #include <math.h> #include <iomanip> #include <fstream> using namespace std; double mdpt (double a, double b) {         double mdptfn = ((a + b) / 2);         return (mdptfn); } int main() {         double a = 0;         double b = 2;         double precision;         cout << " Enter a precision "<<endl;         cin >> precision;         double Fa = pow(a,3) - (7 * pow(a,2)) + (5 * a) + 3;         double Fb = pow(b,3) - (7 * pow(b,2)) + (5 * b) + 3;         cout << Fa <<endl;         cout << Fb <<endl;         // begin the bisection loop         do while (abs(b - a) > precision)         {                 // Find the Midpoint of the funtion                 double midpoint = ((a + b) / 2);                 cout << midpoint <<endl;                 // find the answer for midpoint of the function                 double Fofmid = pow(midpoint,3) - (7 * pow(midpoint,2)) + (5 * midpoint) + 3;                 cout << Fofmid <<endl;                 //determine the appropriate half to search in                 if ((Fa * Fofmid) > 0)                 double        a = midpoint;                 else                 double        b = midpoint;         }                        return (mdpt (a,b));                 }```
• 10-30-2007
bar5037
also
Oh, I also have to pass 2 other functions through this method on the same interval. I was hoping after I got it working with one the rest would fall into place. Is it ok to make the two functions in the program and pass them as something else. I'm really unsure about how to do this. I'm not asking for any code, just possibly an explanation becuause I am pretty new to C++.
Thanks again,
Brian
• 10-30-2007
brewbuck
No such thing as "do while () { }" You either meant:

Code:

`while() { }`
Or

Code:

`do { } while();`
• 10-30-2007
kcpilot
OK, I admit I'm confused by your code. You're finding the midpoint without needing to call the mdpt() function aren't you (in the do-while statement?). And if you need to call the mdpt() function, you don't need a return statement to do it since your mdpt() function already returns that value. But it needs to be assigned to something, e.g., value = mdpt(a,b);
• 10-30-2007
brewbuck
Quote:

Originally Posted by kcpilot
OK, I admit I'm confused by your code. You're finding the midpoint without needing to call the mdpt() function aren't you (in the do-while statement?). And if you need to call the mdpt() function, you don't need a return statement to do it since your mdpt() function already returns that value. But it needs to be assigned to something, e.g., value = mdpt(a,b);

The code is finding an upper bound and a lower bound on the midpoint. The mdpt() function just returns the average of those two values, once they hit a threshold.

The problem is the funky "do while ()..." statement, which isn't valid syntax -- at least not without another closing while() part. The compiler doesn't notice the problem until the block closes and it expects another "while()" part. At that point it spits out an error on the "return" line.
• 10-30-2007
bar5037
Thanks brewbuck,
I don't know how i missed that. In the past half hour I have changed a lot and the program almost doesn't even look the same. i realized I had to create a function for the cubic function given and create another funtion for the Bisection method which would allow me to pass other functions on different intervals through it.
Brian
• 10-30-2007
brewbuck
Quote:

Originally Posted by bar5037
Thanks brewbuck,
I don't know how i missed that. In the past half hour I have changed a lot and the program almost doesn't even look the same. i realized I had to create a function for the cubic function given and create another funtion for the Bisection method which would allow me to pass other functions on different intervals through it.
Brian

Also, this code is wrong:

Code:

```double a = midpoint; else double b = midpoint;```
In your inner loop. That is just declaring a new variable, ALSO called a (or b), storing the result there, then when the loop starts over everything is thrown away. Remove the "doubles"
• 10-30-2007
bar5037
Ok so I have a new problem. The compiler says everything is ok. However when I run the program nothing is outputted. here's the code
Code:

```#include <iostream> #include <cmath> #include <iomanip> #include <fstream> using namespace std; double precision = .000001; double cubic (double x) {         double Fn = ((pow(x,3)) - (7 * (pow(x,2))) + (5 * x) + 3);         return (Fn); } double bisector (double left, double right) {                 double midpoint;                 while (abs(right - left) > precision);         {                 // Find the Midpoint of the funtion                 midpoint = ((left + right) / 2);                 cout << midpoint <<endl;                 //determine the appropriate half to search in                 if ((cubic(left) * cubic(midpoint)) > 0)                 left = midpoint;                 else                 right = midpoint;         }                        return ((left + right) / 2); } int main() {         cout << bisector (0,2) <<endl;         cout << bisector (5,7) <<endl; return 0; }```
• 10-30-2007
bar5037
I've been trying to narrow it down... but i can't seem to find the logic error... maybe an outside source can help me.
• 10-30-2007
brewbuck
Quote:

Originally Posted by bar5037
I've been trying to narrow it down... but i can't seem to find the logic error... maybe an outside source can help me.

Now you have a semicolon on the line with the while() loop. Take it out.
• 10-30-2007
bar5037
thanks brewbuck. I finally have it working. Is there anyone who can explain to me how to pass a function as a parameter of another function. Just the concept behind it will be enough. I just want to know how to pass the cubic function and other functions as a parameter of the bisector function.
• 10-30-2007
brewbuck
Quote:

Originally Posted by bar5037
thanks brewbuck. I finally have it working. Is there anyone who can explain to me how to pass a function as a parameter of another function. Just the concept behind it will be enough. I just want to know how to pass the cubic function and other functions as a parameter of the bisector function.

You pass a pointer to the function. For instance:

Code:

```void hello() {     printf("Hello, world!\n"); } void run_this_function(void (*func)()) {     func(); } int main() {     run_this_function(hello);     return 0; }```
The only weird part is that "void (*func)()" syntax which declares a function pointer. What you are dealing with here are function pointers.

EDIT: But because this is C++, the "better" way would be to use a function object, i.e. a "functor"
• 10-30-2007
bar5037
are pointters necessary to use? My professor always rants in class about how they aren't necessary to use.
• 10-30-2007
brewbuck
Quote:

Originally Posted by bar5037
are pointters necessary to use? My professor always rants in class about how they aren't necessary to use.

I am of the same opinion, actually. They can't ALWAYS be avoided, but sometimes they are the only way to accomplish something.

To avoid a pointer in this case, you can declare a generic base class which represents your functions:

Code:

```class function_base { public:     virtual ~function_base() { }     virtual double operator()(double value) = 0; };```
Then derive from it to implement a specific function:

Code:

```class cubic_function : public function_base {     double operator()(double value)     {         return value * value * value;     } };```
Then you'd pass it to your bisection function as a parameter, through a reference (which allows the polymorphism to work):

Code:

```double bisect(double left, double right, const function_base &func) { }```
Then you call "func()" inside the bisect() routine. When you call bisect() itself, you pass an actual function object:

Code:

`bisect(left_val, right_val, cubic_function());`
No pointers anywhere there, but it does use runtime polymorphism (the virtuals).

There are a lot of "gotchas," such as the necessity of the "const" reference.