fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5. // zdefiniuj funkcję
  6. double pierwiastek(double x, double eps) {
  7. double a = 0.0;
  8. double b = x;
  9. double c;
  10. while (fabs(b - a) > eps) {
  11. c = (a + b) / 2;
  12. if (c * c > x)
  13. b = c;
  14. else
  15. a = c;
  16. }
  17. return c;
  18. }
  19. int main() {
  20. double x = 0.0;
  21. while (x < 1.0){
  22. x = x + 0.1;
  23. cout << x << endl;
  24. }
  25. cout << setprecision(2) << pierwiastek(2.0, 0.1) << endl;
  26. cout << setprecision(3) << pierwiastek(2.0, 0.1) << endl;
  27. cout << setprecision(5) << pierwiastek(2.0, 0.1) << endl;
  28. // sprawdź działanie funkcji
  29. return 0;
  30. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.4
1.44
1.4375