Question
Create a program using C - Integral Approximation 1. Introduction In this program, you will design functions that allow you to approximate the integral of
Create a program using C - Integral Approximation
1. Introduction
In this program, you will design functions that allow you to approximate the integral of a function, f(x), using the trapezoidal rule. To find this integralthe area under the curvewe can approximate the area as a series of trapezoids.
2. Specification
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
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.
***There are 3 files to edit:
prog5integral.c
#include
int main() { /************************************************* COMPLETE THIS FUNCTION WITH APPROPRIATE CODE *************************************************/ }
prog5_functions.h
// Lines used for conditional compilation--ensures header isn't included multiple times #ifndef prog5_functions_h #define prog5_functions_h
// Calculate integral of f(x) over interval min to max, using n trapezoids double integrate(double min, double max, int n);
// f(x) as defined in the program specification double f(double x);
// Used to clear line if input formatting error occurs void badInput();
#endif /* More conditional compilation--end of prog5_functions_h */
prog5_functions.c
#include
#include
#include "prog5_functions.h"
// Calculate integral of f(x) over interval min to max, using n trapezoids
double integrate(double min, double max, int n) {
/*************************************************
COMPLETE THIS FUNCTION WITH APPROPRIATE CODE
*************************************************/
printf("integrate() doesn't work! ");
return 0;
}
// f(x) as defined in the program specification
double f(double x) {
/*************************************************
COMPLETE THIS FUNCTION WITH APPROPRIATE CODE
*************************************************/
printf("f() doesn't work! ");
return 0;
}
// Used to clear line if input formatting error occurs
void badInput() {
/*************************************************
COMPLETE THIS FUNCTION WITH APPROPRIATE CODE
*************************************************/
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started