Advertisement
Guest User

Untitled

a guest
May 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4. float Fun(float x)
  5. {
  6. float y = x*x*x - 1;
  7. return y;
  8. }
  9. bool ZeroN(float a, float b, int n, float &x)
  10. {
  11. int i = 0;
  12. if (Fun(a)*Fun(b) > 0) return false;
  13. while (i < n)
  14. {
  15. float srodek = (a + b) / 2; //wyznaczamy srodek przedzialu
  16.  
  17. if (Fun(srodek) == 0) //sprawdzamy, czy srodek przedzialu jest miejscem zerowym funkcji
  18. {
  19. x = srodek;
  20. return true;
  21. }
  22.  
  23. if (Fun(a)*Fun(srodek) < 0) b = srodek;
  24. else a = srodek;
  25. i++;
  26. }
  27. x = a + b / 2;
  28. return true;
  29. }
  30. int main() {
  31. float a,b,x;
  32. int n;
  33. cout << "Podaj przedzial, ktory chcesz badac: ";
  34. cin >> a >> b;
  35. cout << "Podaj maksymalna ilosc wykonan: ";
  36. cin >> n;
  37. if (ZeroN(a, b, n, x)) cout << "Miejsce zerowe to: " << x;
  38. else cout << "Nie da sie wyznaczyc miejsca zerowego.";
  39. cin.ignore();
  40. getchar();
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement