fork(2) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. double f(double x)
  8. {
  9. return 0.25 * x * x - 2;
  10. }
  11.  
  12. int main()
  13. {
  14. double a = 2.0;
  15. double b = 4.0;
  16. double eps = 0.0001;
  17. double c;
  18.  
  19. // Sprawdzenie warunku
  20. if (f(a) * f(b) >= 0)
  21. {
  22. cout << "Brak miejsca zerowego w przedziale!" << endl;
  23. return 0;
  24. }
  25.  
  26. // Metoda bisekcji
  27. while ((b - a) >= eps)
  28. {
  29. c = (a + b) / 2.0;
  30.  
  31. if (f(c) == 0.0)
  32. break;
  33. else if (f(a) * f(c) < 0)
  34. b = c;
  35. else
  36. a = c;
  37. }
  38.  
  39. cout << "Miejsce zerowe: x = " << c << endl;
  40.  
  41. // Zapis danych do wykresu
  42. ofstream file("dane.txt");
  43. for (double x = 1.5; x <= 4.5; x += 0.05)
  44. file << x << " " << f(x) << endl;
  45. file.close();
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Miejsce zerowe: x = 2.82843