fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // definicja funkcji
  7. double f(double x)
  8. {
  9. return x*x*x - 2;
  10. }
  11.  
  12. int main()
  13. {
  14. double a, b, srodek;
  15. double eps = 0.000001; // dokladnosc
  16.  
  17.  
  18. // sprawdzenie warunku metody bisekcji
  19. if (f(a) * f(b) >= 0)
  20. {
  21. cout << "Brak miejsca zerowego w tym przedziale." << endl;
  22. return 0;
  23. }
  24.  
  25. // petla bisekcji
  26. while ((b - a) / 2 > eps)
  27. {
  28. srodek = (a + b) / 2;
  29.  
  30. if (f(srodek) == 0)
  31. break;
  32. else if (f(a) * f(srodek) < 0)
  33. b = srodek;
  34. else
  35. a = srodek;
  36. }
  37.  
  38. srodek = (a + b) / 2;
  39.  
  40. cout << "Przyblizone miejsce zerowe: " << srodek << endl;
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5308KB
stdin
1 10
stdout
Brak miejsca zerowego w tym przedziale.