Please read the readme file and make the necessary changes to the code. I will attach a zip file with all necessary files and information once question is accepted
The only file you need to modify in this assignment is integrator.c. Please read every file in this assignment and understand all details.
You need to implement two functions:
double integrate1(Range rng);
The argument provides lowerlimit, upperlimit, and the number of intervals (intervals). They are three attributes of the type Range, defined in hw2.h. integrate1 should perform a numerical integration of the function func (which will be provided for you in files named func1.c, func2.c, etc.) using the method described above.
and
void integrate2(RangeAnswer * rngans);
This is the function that hw2.c calls to find the integral: it takes a RangeAnswer struct as an argument (also defined in hw2.h). Because the argument is a pointer, setting the answer field of rngans inside integrate2 will allow whoever calls integrate2 to retrieve the answer.
You are strongly encouraged to use integrate1 when implementing integrate2. Thus, when testing integrate2, integrate1 is also tested, i.e., when INTEGRATE_2 is defined, INTEGRATE_1 is also defined.
Both functions call the function to be integrated func.
Write DRY Code
When writing code, a general rule is “Don’t Repeat Yourself” (DRY). In this assignment, you should implement integrate2 using integrate1 but these two functions are somewhat similar. This is important because you need to implement (and debug) the overlapped part only once.
If you implement integrate2 independently of integrate1, you are creating WET (“We Enjoy Typing”) code. You have two similar but different functions. This invites mistakes. Consider the situation when you find a mistake in integrate1 and correct it. It is highly likely that you forget to correct the mistake in integrate2.
DRY code is good. WET code is bad.
Testing your integrator
Five functions are created for you to test. If a function uses the mathematics library in C (such as func4.c and func5.c), please include math.h and add -lm after gcc. (You can see how this works in the Makefile)
<code>file function integration func1 x x * x / 2 func2 x * x x * x * x / 3 func3 x * x - 3 * x x * x * x / 3 - 3 * x * x / 2 func4 sin(x) -cos(x) func5 cos(x) + sin(x) sin(x) - cos(x) </code>
Each of these functions defines its own version of func that gets compiled together with integrator.c and hw2.c to create your program. So, to integrate the function defined in func1.c, you would compile that and link it together with integrator.c and hw2.c. Here are the Makefile rules that build an integrator for the function in func1.c:
<code>hw2-func1-1: hw2a.o integrator.o func1.o
$(GCC) hw2a.o integrator.o func1.o -o hw2-func1-1
hw2a.o: hw2.c hw2.h
$(GCC) -c -DINTEGRATE_1 hw2.c -o hw2a.o
func1.o: func1.c
$(GCC) -c func1.c
integrator.o: integrator.c hw2.h
$(GCC) -c integrator.c
</code>
(If you just run make you will get executable programs that will test the integrate1 and integrate2 versions of your integrator for all five of the provided test functions)
To test your integrator, run pa03-func1-1 on a test input:
> ./pa03-func1-1 tests/test-func1-1
The format of a test file is:
<code><lower bound (floating point)> <upper bound (floating point)> <# intervals (integer)> <expected answer (floating point)> </code>
The provided test files (in the directory tests/) all have the answer you are expected to get. Feel free to generate new tests, knowing what the “correct” answer is for the functions you are testing.
You can run through all of our test cases using
make pa03-test1
to test integrate1, and
make pa03-test2
<br>


0 comments