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