fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x) {
  7. return sqrt(1 - x * x);
  8. }
  9.  
  10. int main() {
  11. int n = 100000;
  12. double a = -1.0;
  13. double b = 1.0;
  14. double h = (b - a) / n;
  15.  
  16. double suma = 0.5 * (f(a) + f(b));
  17.  
  18. for(int i = 1; i < n; i++) {
  19. double x = a + i * h;
  20. suma += f(x);
  21. }
  22.  
  23. double pi = 2 * h * suma;
  24.  
  25. cout << "Przyblizona wartosc pi (metoda trapezow): " << pi << endl;
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Przyblizona wartosc pi (metoda trapezow): 3.14159