fork download
  1. #include <iostream>
  2. #include <utility>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. double compute_by_rectangle_method(double from, double to, double precision, double (*func)(double) ) {
  7. if(to < from){
  8. return compute_by_rectangle_method(to, from, precision, func);
  9. }
  10. double accumulator = 0.0;
  11. for(double i = from + precision; i < to; i += precision) {
  12. accumulator += func(i);
  13. }
  14. return accumulator * precision;
  15. };
  16.  
  17. int main() {
  18. double from = 0.0; //prompt_for_double("Enter lower limit of integral: ");
  19. double to = 1000.0; //prompt_for_double("Enter upper limit of integral: ");
  20. double result = compute_by_rectangle_method(from, to, 1.0, [](double x){
  21. return std::sin(x) + std::sqrt(x) * std::cos(x/3);
  22. });
  23. cout << result;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
11.6404