Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double f(double n){
- return n * n;
- }
- double maxNumber(double a, double b){
- if(a >= b)
- return a;
- else
- return b;
- }
- double minNumber(double a, double b){
- if(a >= b)
- return b;
- else
- return a;
- }
- int main()
- {
- // Inputs from the user
- cout << "I will calculate the area of the function f(x) = x^2 from a to b.\n";
- cout << "Give me the a: ";
- double a;
- cin >> a;
- cout << "Give me the b: ";
- double b;
- cin >> b;
- cout << endl;
- // Determine the minNumber and the maxNumber
- double maximum = maxNumber(a, b);
- double minimum = minNumber(a, b);
- // Calculate
- double sum = 0.0;
- double step = 0.000001; // Step = Basis of the rectangle
- for(double i=minimum; i<=maximum; i+=step){
- sum += step * f(i);
- }
- // Display the result
- cout << "Area E = ";
- if(a <= b){
- cout << sum << endl;
- }
- else{
- cout << -sum << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement