fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Funkcja f(x) – NIEUJEMNA na [a, b]
  7. double f(double x)
  8. {
  9. return x * x; // przykład
  10. }
  11.  
  12. int main()
  13. {
  14. // Dane wbudowane
  15. double a = 0.0;
  16. double b = 1.0;
  17. int n = 1000;
  18.  
  19. double h = (b - a) / n;
  20. double s = 0.0;
  21.  
  22. for (int i = 0; i < n; i++)
  23. {
  24. double x = a + (i + 0.5) * h; // środek przedziału
  25. s += f(x);
  26. }
  27.  
  28. s *= h;
  29.  
  30. cout << "Przyblizona wartosc pola: " << s << endl;
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5332KB
stdin
Standard input is empty
stdout
Przyblizona wartosc pola: 0.333333