0

help me with this program :

#include <iostream>
using namespace std;

#include <math.h>

double F(double x){
        double res = pow(x, 9) - pow(x, 7) + 2 * pow(x, 2) -1;

        return res;
}

void print_roots(){
        double low = 0, high = 1;

        for(int i = 0; i < 1000 ; ++i){
                double mid = (float)(low + high)/2;
                double val = F(mid);

                cout << mid << ":" << val << endl;

                if(val < 0.00001 && val > -0.00001){
                        cout << "Root is " << mid << endl;
                        return;
                }
                else if (val < 0 )
                        low = mid;
                else
                        high = mid;
        }
}

int main(int argc, char **argv){
        print_roots();
        system("pause");
        return 0;
}

can it be made to add induction of different interval or different task.
i mean to assign a task and interval and the program to give resolution.
example : i start the program and it ask me to enter the task(bisection method) after entering it to ask me the intervals and when we do this to show the answer.
thank you in advance.i hope you understand me.sorry for bad english

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Lerner
0

Yes, you should be able to do that. You might want to look up use of switch statements and use a menu to select the task. Here's a rough sketch of the process, but looking it up in a book to learn the details is strongly recommended.

cout  << enter the number before the task you want to do
cout << 1) bisection
cout << 2) foo
cout << 3) bar

cin >> selection
switch(selection)
{
   case 1:
      cout << "enter the interval you wish to use
      cin >> interval
      bisection(interval);
      break;
   case 2:
      foo();
      break;
   case 3:
      bar();
      break;
}

There are alternative ways to accomplish the same thing, but this is usually one of the easier methods to understand.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.