Starting with Functions
Create the following functions in a file func.c
:
-
rational
that evaluates the function usingdouble
arguments. -
order
that takes three integer arguments and returns1
if they are in order and0
if not. So,order(2,5,9)
should return1
;order(2,9,5)
should return0
. -
input
which takes no arguments. The user should be asked for a positive integer. If they give good input, it should be returned. If they give a negative integer, they should be asked again until they give a positive integer. You don't have to deal with any other errors.
Create a main
function that demonstrates each of these functions.
The problem
For this assignment, you will have to write a program to approximate the values of several integrals. Your program should be capable of approximating the integrals of five functions:
Your program will approximate integrals of these functions with the left, right, midpoint and trapezoid approximation methods. Your program should be called a4.c
.
Functions
You should create the following functions:
double f ( int i, double x )
double slice_left ( int i, double a, double b )
double slice_right ( int i, double a, double b )
double slice_mid ( int i, double a, double b )
double slice_trap ( int i, double a, double b )
The first function, f(i,x)
should return the value of
.
The other four functions are responsible for calculating one slice of the approximation area. In the image below, the red area is what should be returned by slice_left
. The blue and green areas (and the other colours below them) are what should be returned by slice_mid
and slice_right
, respectively.
You should create each of the above functions and test them to make sure they work. Then, your main
function, should use these to approximate the integral requested by the user.
Note that the slice_*
functions return the area of a single rectangle. You will have to call them several times to calculate the whole approximation. So, for example, in the figure below, calling slice_left
should return the area of one of the red squares (the filled rectange will be one call to slice_left
); you'll have to add up each result to get the total approximation.
Input and Output
For this assignment, input and output must be exactly the same as that given here.
You should get four pieces of input: the function number, the left and right limits of the sum and the number of steps to divide the interval into.
You are not required to do (and should not spend your valuable time doing) any error checking, except:
- the function number should not be smaller than 1 or larger than 5;
- the left limit cannot be greater than the right limit;
- the number of steps should be at least 1;
- there should be correct input for each of these (ie. the user can't enter "q" steps).
This isn't easy--here's a hint.
Here is some sample output:
userid_fruit: ./a4
Which function number? [1-5]
3
Enter the left and right limits for the integration:
1.0 2.59267
How many steps?
1000
Integral of function #3 approximated with 1000 steps,
starting at 1, ending at 2.59267.
Left approximation: 0.95
Right approximation: 0.95
Midpoint approximation: 0.95
Trapezoid approximation: 0.95
userid_fruit: ./a4
Which function number? [1-5]
a
Which function number? [1-5]
6
Which function number? [1-5]
-1
Which function number? [1-5]
2
Enter the left and right limits for the integration:
3 s
Enter the left and right limits for the integration:
4 2
Enter the left and right limits for the integration:
2 4
How many steps?
20
Integral of function #2 approximated with 20 steps,
starting at 2, ending at 4.
Left approximation: 0.32
Right approximation: 0.15
Midpoint approximation: 0.24
Trapezoid approximation: 0.24
As mentioned above, your output must be exactly the same as this.
The limits of the integration (starting at...ending at...
) should be printed with format %g
and the approximations (???? approximation: ...
) with %10.2f
.
Testing the Input and Output
To help you get your input exactly like is required, there is a program that you can run which will compare your program's output to some reference output. You will be told what the differences are. There is also another which will display your output beside the reference output.
If you run the command /gfs1/CMPT/102/102cmds
, you will be able to access these commands the next time you log in.
The command a4-test
will test your output and display differences. When you're in the same directory as your compiled a4
program, type the command a4-test 1
to test against the first reference output. If there is not output, there were no differences.
The command a4-beside
will display your output beside the reference output. Type the command a4-beside 1
to display first reference output.
You can also change the number in the commands to any number from 1
to 6
to get different test data.
Style
Style on this assignment will be marked more closely than on previous ones. You should keep in mind the style points that we discussed in lectures, including (but not limited to):
- good variable (and function) names
- comments used where appropriate
- indenting of code
- spacing used to make code more readable
Submitting
You have to use the Submission server to submit your work. You should submit the files func.c
and a4.c
.
You can do this by typing these commands:
tar cvf a4.tar func.c a4.c
gzip a4.tar
Then, submit the file a4.tar.gz
.