Answer to Question #191253 in C++ for jay

Question #191253

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.


1
Expert's answer
2021-05-11T23:18:11-0400

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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment