Question: Program Structure Your program should follow the general outline below: Step 1: Prompt for and read the following values: The low and high points of

Program Structure

Your program should follow the general outline below:

Step 1: Prompt for and read the following values:

The low and high points of the interval [a, b], over which f(x) is to be integrated.

The number of trapezoids, n, to be used in that integration.

If an input error occurs, print an error message and repeat the prompt for that input. Input errors are as follows:

scanf() cannot read the input values (for example, if the user types "A 3" for the interval endpoints). In this case, you must clear the rest of the line before retrying scanf(). See Lecture 11 (PE1: Conditionals and While Loops") for a reminder of how to check that the input is properly formatted and how to clear the rest of the line if it is not.

The low interval endpoint is greater than or equal to the high endpoint.

The number of trapezoids is less than 1.

Step 2: Once the user has entered error-free input values, call the integrate() function (described below), which will use the trapezoidal method to approximate the integral of f(x) over the interval [a, b] using n trapezoids.

Step 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' Return to Step 1.

'N' or 'n' End the program.

Any other character Print an error message and repeat the question.

See the program test cases. to view proper input and output formatting.

Functions

Your program should contain functions with the prototypes shown below:

double f(double x); The function being integrated, which should calculate the value: f(x) = sin(x) + x2 / 10

Note that you should include the math library 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, as described above. The return value of the function is the result of the approximation. Note that the function should not print any values to the screenthe output should be handled in the main function.

void badInput(); Clear the current line of input. This function should be used after input formatting errors occur.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!