fork descargar
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double pole_trapezow(double a, double b, int n) {
  7. double h = (b - a) / n;
  8. double suma = sin(a) + sin(b);
  9.  
  10. for (int i = 1; i < n; i++) {
  11. double x = a + i * h;
  12. suma += 2 * sin(x);
  13. }
  14.  
  15. return (h / 2) * suma;
  16. }
  17.  
  18. int main() {
  19. double a = 0.0;
  20. double b = 3.14;
  21.  
  22. int n1 = 10;
  23. int n2 = 100;
  24.  
  25. cout << "Pole metoda trapezow dla n = 10: "
  26. << pole_trapezow(a, b, n1) << endl;
  27.  
  28. cout << "Pole metoda trapezow dla n = 100: "
  29. << pole_trapezow(a, b, n2) << endl;
  30.  
  31. return 0;
  32. }
  33.  
Éxito #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Pole metoda trapezow dla n = 10:  1.98354
Pole metoda trapezow dla n = 100: 1.99983