SHARE
TWEET

Untitled

a guest Feb 23rd, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double F(double x) {
  6.     return x*x*x + 5*x - 3;
  7.     // return x*x*x - 8;
  8.     // return x*x - 4;
  9. }
  10.  
  11. float input (string msg) {
  12.     cout << msg;
  13.     float x;
  14.     cin >> x;
  15.     return x;
  16. }
  17.  
  18. int main()
  19. {
  20.     float p = -2,
  21.           q = 2,
  22.           E1 = 0.0001,
  23.           s;
  24.  
  25.     p = input("Podaj p: ");
  26.     q = input("Podaj q: ");
  27.     E1 = input("Podaj E1: ");
  28.  
  29.     s = (p + q) / 2;
  30.  
  31.     while (F(p) != 0 && F(q) != 0 && q - p >= E1) {
  32.         s = (p + q) / 2;
  33.         if (F(p) * F(s) > 0) {
  34.             p = s;
  35.         } else {
  36.             q = s;
  37.         }
  38.     }
  39.  
  40.     if (F(p) == 0) {
  41.         cout << p << endl;
  42.         return 0;
  43.     } else if (F(q) == 0) {
  44.         cout << q << endl;
  45.         return 0;
  46.     }
  47.  
  48.     cout << "Wynik: " << s << endl;
  49.     return 0;
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top