Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- // This Function: x^3 + 3x- 5;
- double func(double x){
- double f;
- f=pow(x,3)+(3*x)-5; // Calculate Function
- return f;
- }
- int main()
- {
- // E-> Stopping criterion
- double a,b,E;
- cin>>a>>b>>E;
- if(func(a)*func(b)>=0) goto Stop; // jump return 0;
- else {
- Step:
- double Root;
- Root=(a+b)/2.0;
- if(func(a)*func(Root)<0) b=Root;
- else{
- a=Root;
- }
- if(abs((b-a)/b) >E) { // relative Error
- cout<<"Iteative:"<<Root<<"\n";
- goto Step; // jump up step;
- }
- else {
- Root=(a+b)/2.0;
- cout<<"Root:"<<Root<<"\n";
- goto Stop; // jump return 0;
- }
- }
- Stop:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement