You are required to: v. write pseudocode algorithm to determine the integral of a function between two specified points using the rectangular rule. vi. write C++ computer programs to determine the integral of a function between two specified points using the rectangular rule.
v.
1. Start
2. Define function function(a)
3. Input begin_inter, end_inter, iterations
4. Calculate the steps: s = (end_inter - begin_inter) / iterations
5. Set: n=1
6. Loop to n<=iterations
res=begin_inter + (n - 1) * s
integral += s * function(res);
7. Output integral
8. Stop
vi.
#include <iostream>
using namespace std;
double function(double a){
return a*a;
}
int main() {
double begin_inter, end_inter,s;
int iterations;
double integral = 0;
cout << "Enter interval beginning: ";
cin >> begin_inter;
cout << "Enter interval end: ";
cin >> end_inter;
cout << "Enter number of iterations: ";
cin >> iterations;
s= (end_inter - begin_inter) / iterations;
for (int n = 1; n <= iterations; n++) {
double res=begin_inter + (n - 1) * s;
integral += s * function(res);
}
cout << "Integral is: " << integral << endl;
return 0;
}
Comments
Leave a comment