fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Definicja funkcji f(x)
  7. double f(double x) {
  8. return sin(x); // <- tutaj możesz zmienić funkcję
  9. }
  10.  
  11. // Metoda prostokątów (lewych)
  12. double prostokaty(double a, double b, int n) {
  13. double dx = (b - a) / n;
  14. double x = a;
  15. double s = 0.0;
  16.  
  17. for (int i = 1; i <= n; i++) {
  18. s += dx * f(x);
  19. x += dx;
  20. }
  21.  
  22. return s;
  23. }
  24.  
  25. int main() {
  26. double a1 = 0.0, b1 = 3.14;
  27. int n1 = 10;
  28.  
  29. double a2 = 0.0, b2 = 3.14;
  30. int n2 = 100;
  31.  
  32. double s1 = prostokaty(a1, b1, n1);
  33. double s2 = prostokaty(a2, b2, n2);
  34.  
  35. cout << "s1 (n = 10) = " << s1 << endl;
  36. cout << "s2 (n = 100) = " << s2 << endl;
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
s1 (n = 10)  = 1.98329
s2 (n = 100) = 1.99981