You need to write a program to calculate the area under a curve f(x) = 5x3 + 7x2
ID: 3638574 • Letter: Y
Question
You need to write a program to calculate the area under a curve f(x) = 5x3 + 7x2 – 3x + 4 for a <= x <= bwhere a and b are greater than or equal to 0.
You input the values of a and b using cin. For each pair of (a, b) values:
Check whether a and b are larger than 0, and whether a is smaller than or equal to b. If NOT, error
message must be displayed and your program should go on to the next set of input values. If YES, your
program approximates the area under the curve between a and b by partitioning the area into small
segments where each segment has width w. To control the recursive depth, the value of w is controlled to
be (b-a)/20.
For each pair of legal (a, b) values, your main function calls findarea(a, b, w). The function findarea(a, b,
w) first checks whether b-a is smaller than or equal to w (note that this is recursive termination check). If
YES, findarea(a, b, w) computes f(b)*(b-a) and then returns the result. If NOT, findarea(a, b, w) begins to
break the problem into two smaller problems by calling findarea(a, (a+b)/2.0, w) and findarea((a+b)/2.0, b,
w) recursively (note that this is the divide and conquer step). When both function calls return, their values
will be summed (this is the results integration step). Eventually, the area will be returned back to the main
function. You have to print the value.
Once your program is working, try to execute your program by changing w to (b-a)/100, (b-a)/10000, (ba)/
10000000, ...and comment on the relationship between w and the runtime of your program.. Write a
small discussion to explain the difference between the loop solution you did before and the recursive
solution in the lab, and also explain your preference in using loop solution or recursive solution.
Explanation / Answer
#include #include int GetRand(int min, int max); int main(void) { int i, r, x; char last; r = GetRand(1, 1000); printf("Pick a number between 1 and 1000 "); scanf("%d", &x); while (x != r) { if (xRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.