Advertisement
makispaiktis

Integral of x^2

Apr 7th, 2019 (edited)
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double f(double n){
  6.     return n * n;
  7. }
  8.  
  9. double maxNumber(double a, double b){
  10.     if(a >= b)
  11.         return a;
  12.     else
  13.         return b;
  14. }
  15.  
  16. double minNumber(double a, double b){
  17.     if(a >= b)
  18.         return b;
  19.     else
  20.         return a;
  21. }
  22.  
  23. int main()
  24. {
  25.     // Inputs from the user
  26.     cout << "I will calculate the area of the function f(x) = x^2 from a to b.\n";
  27.     cout << "Give me the a: ";
  28.     double a;
  29.     cin >> a;
  30.     cout << "Give me the b: ";
  31.     double b;
  32.     cin >> b;
  33.     cout << endl;
  34.     // Determine the minNumber and the maxNumber
  35.     double maximum = maxNumber(a, b);
  36.     double minimum = minNumber(a, b);
  37.     // Calculate
  38.     double sum = 0.0;
  39.     double step = 0.000001;                  // Step = Basis of the rectangle
  40.     for(double i=minimum; i<=maximum; i+=step){
  41.         sum += step * f(i);
  42.     }
  43.  
  44.     // Display the result
  45.     cout << "Area E = ";
  46.     if(a <= b){
  47.         cout << sum << endl;
  48.     }
  49.     else{
  50.         cout << -sum << endl;
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement