Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. int bisect(float a, float b, float p);
  5. int f(float x);
  6.  
  7. int main(void)
  8. {
  9.     float x1, x2;
  10.     float p, ans;
  11.    
  12.     x1 = 0;
  13.     x2 = 100;
  14.    
  15.     scanf("%f", &p);
  16.     ans = bisect(x1, x2, p);
  17.     printf("%f", ans);
  18.    
  19.     return(0);
  20.    
  21. }
  22.  
  23. int bisect(float a, float b, float p)
  24. {
  25.     float c;
  26.     c = (a-b)/2;
  27.    
  28.     if (abs(a-b) > 2*p)
  29.     {
  30.         return(c);
  31.     }
  32.    
  33.     else if(f(a)*f(c) < 0)
  34.     {
  35.         return(bisect(a,c,p));
  36.     }
  37.     else if(f(b)*f(c) < 0)
  38.     {
  39.         return(bisect(c, b, p));
  40.     }
  41.     else
  42.     printf("midpoint is exacly on the root");
  43.    
  44. }
  45.  
  46. int f(float x)
  47. {
  48.     float func;
  49.     func = ((x-sqrt(3))*(x+x+1));
  50.     return(func);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement