fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // Funkcja f(x) = sin(x)
  7. double f(double x)
  8. {
  9. return sin(x);
  10. }
  11.  
  12. // Metoda prostokątów (lewych)
  13. double metodaProstokatow(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 = s + dx * f(x);
  22. x = x + dx;
  23. }
  24.  
  25. return s;
  26. }
  27.  
  28. int main()
  29. {
  30. double A1 = 0.0;
  31. double B1 = 3.14;
  32. int N1 = 10;
  33.  
  34. double a2 = 0.0;
  35. double b2 = 3.14;
  36. int N2 = 100;
  37.  
  38. double s1 = metodaProstokatow(A1, B1, N1);
  39. double s2 = metodaProstokatow(a2, b2, N2);
  40.  
  41. cout << "Wynik dla N1 = 10: " << s1 << endl;
  42. cout << "Wynik dla N2 = 100: " << s2 << endl;
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Wynik dla N1 = 10:  1.98329
Wynik dla N2 = 100: 1.99981