I’m working on an integration program using [right] rectangle sums. I am using the beginning bound as a=1, using ‘n’ as the # of rectangles, and ‘inc’ as the increment that increases z. Here is the code I have so far:
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;
int main(){
int n;
float b;
float z;
z=((b-1)/n);
float inc;
float new_sum;
float sum;
int decision;
cout << "Would you like to calculate an area? " << endl;
cout << "Enter 1 for yes, 0 for no: " << endl;
cin >> decision;
cout << "Please enter the number of rectangles you would like to use: " << endl;
cin >> n;
cout << "Please enter the upper bound of integration: " << endl;
cin >> b;
for (inc=0; inc < b; inc++){
new_sum=z*(f(1+(inc*z)));
sum=sum+new_sum;
}
cout << sum << endl;
return 0;
}
I have two questions:
-
How do I use the function f(x)=x^5 + 10 in this? I’m not sure how it should be inputted and formatted in the for loop.
-
How do I loop the first question sequence (Would you like to calculate an area?), using a for loop, to repeat until the user enters 1 for yes (I know how to do this with a while loop, but was wondering how it would be done with a for loop?)
add this lines before function
maindefinition.