fork(25) download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. double f(double x){return (-3*x*x+2*x+9);}
  5. double I(double a,double b,int n,double y){return ((b-a)/(2*n)*y);}
  6. int main() {
  7. int n; double a,b,y,dy,In;
  8. cin>>a>>b>>n;
  9. if (n>1){
  10. dy=(b-a)/n;
  11. y+=f(a)+f(b);
  12. for (int i=1; i<n; i++) {y+=2*(f(a+dy*i));}
  13. In=I(a,b,n,y);
  14. cout << In;
  15. }
  16. else {cout << "Wrong data";}
  17. }
Success #stdin #stdout 0s 3416KB
stdin
-1 2 25
stdout
20.9784