fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Definicja funkcji f(x)
  7. // Możesz ją zmienić zgodnie z zadaniem
  8. double f(double x)
  9. {
  10. return x * x; // przykładowo f(x) = x^2
  11. }
  12.  
  13. int main()
  14. {
  15. double a, b;
  16. int n;
  17. double s = 0.0;
  18.  
  19. cout << "Podaj a i b (a < b): ";
  20. cin >> a >> b;
  21.  
  22. cout << "Podaj liczbe prostokatow n: ";
  23. cin >> n;
  24.  
  25. double h = (b - a) / n;
  26.  
  27. for (int i = 0; i < n; i++)
  28. {
  29. double x = a + (i + 0.5) * h; // srodek przedzialu
  30. s += f(x);
  31. }
  32.  
  33. s *= h;
  34.  
  35. cout << "Przyblizona wartosc pola: " << s << endl;
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Podaj a i b (a < b): Podaj liczbe prostokatow n: Przyblizona wartosc pola: 0