Program structure 1 Prompt for and read the following values
Program structure:
1) Prompt for and read the following values: a) The low and high points of the interval [a,b], over where f(x) should be integrated. b) the number of trapezoids, n, to be used in that integration. Note: If an input error occurs, print error message and repeat the prompt for that input. The input errors are as follows: Error1) scanf() - cannot read the input values (ex: if the user types\"A 5\" for the interval endpoints.. for this case you must clear the rest of the line before retrying scanf(). Error 2) The \"low\" interval endpoint is greater than or equal to the \"high\" endpoint. Error3) The number of trapezoids is less than 1.
2) Once the user has entered values, without errors, call the integrate() function, which will use the trapezoidal method to approximate the integral of f(x) over the integral [a,b] using n trapezoids.
3) After printing the results, ask the user if he or she wants to repeat the program and then read a single character for the response to that question. If the user enters: \'Y\' or \'y\' === means will return to Step 1. \'N\' or \'n\' === means the End of the program OR: Any other character === means Print an error message and repeat the question. Note: This program should contain functions with the prototypes: double f(double x): where the function being integrated should calculate the value: f(x) = sin(x) + (x2 /10). Plus please include the math library: <math.h>, in order to use the sin() function, this function takes a single argument and returns the sine of that value: double integrate( double a, double b, int n); This function should use the trapezoidal method to approximate the integral of f(x) over the interval [a,b] using n trapezoids. The return value of the function is the result of the approximation. Note that the function should not print any values to the screen, the ouput should be handled in the main function.: void badInput() === means clear the current line of input, This function should be used after input formatting errors occurs
Solution
#define f(x) sin(x)+(x*x/10)
main()
{
int n;
float a,b;
float integrate(float,float,int);
printf(\"Enter the values of a,b and n\ \");
scanf(\"%f%f%d\",&a,&b,&n);
printf(\"Integral value of f(x) by trapezoidal rule:%f\ \",integrate(a,b,n));
}
float integrate(float a,float b,int n)
{
float x,y[51],h;
float sum;
int i;
h=(b-a)/n;
x=a;
for(i=0;i<=n;i++)
{
y[i]=f(x);
x=x+h;
}
sum=y[0]+y[n];
for(i=1;i<n;i++)
sum+=2*y[i];
sum=sum*h/2;
return sum;
}
![Program structure: 1) Prompt for and read the following values: a) The low and high points of the interval [a,b], over where f(x) should be integrated. b) the n Program structure: 1) Prompt for and read the following values: a) The low and high points of the interval [a,b], over where f(x) should be integrated. b) the n](/WebImages/31/program-structure-1-prompt-for-and-read-the-following-values-1088680-1761572861-0.webp)