CS201
Lab 7 - Function Parameters
NOTE: Directions on implementation of Trap function have been updated by TA on
03/04/08.
Objectives:
- Calculate the area under a curve using a C program.
- Practice use of nested for-type loops, switch statements, enumerated types
and passing function parameters.
Readings:
- Read Chapter 7 in the Hanly/Koffman text. Make sure to read "Case Study:
Bisection Method for Finding Roots" in page 348-356 for helpful ideas and better
understanding of the problem.
Deliverables: (DUE BEFORE THE BEGINNING OF YOUR NEXT LAB!)
- Submittal of the following files:
- Makefile
- Proto.h
- Lab7.c
- Trap.c
- XSquaredSinX.c
- HalfCircle.c
- TestFunction.c
TO DO (for this lab)
- Design and Write a program to solve Programming Project #6
on page 363-364 of the Hanly/Koffman text.
- Your program should find the approximated area under a curve
y = f(x), within a range
[a, b] specified on axis X. The area under the
curve is approximated by repetitive summing of areas of trapezoids. The first
trapezoid is formed by a vertical line going from
(x0,0) up to the point (x0,y0), where
y0 = f(x0), then by a straight line to
(x1,y1)to, where y1 =
f(x1), back down to the X-axis (i.e. our point
(x1,0)), and left to the point of origin (i.e. our
(x0,y0)). Then, the area of the next trapezoid
needs to be calcluated, this time we start in point
(x1,0)...

- We assume that function is positive over the interval
[a, b].
- For n subintervals of length
h, where h=(b-a)/n,
Trapezoidal rule is:

- Write a function Trap with input
parameters a, b, n and
f that implements the trapezoidal rule.
- Call Trap with values for
n of 2, 4, 8, 16, 32,
64, and 128 on functions


- Carefully choose which parameters need to be passed by reference, and when
the passing by value is enough.
- Do not use
printf
in
Trap.c file.
- Calculate
h=(b-a)/n and implement the Trapezoidal rule inside the
Trap function.
- You need to be careful in choosing
function parameters. Hint: your Trap
function should have void return type. So, it
must have at least two reference input parameters along with other regular
parameters, through which the function will pass h
and area to the caller function (probably
main is the caller function in your program).
- Make sure to use nested (i.e. double)
for-type loop to calculate and print out your results.
- Define enumerated type in
proto.h
to reflect your three functions (i.e.
xsquaredsinx, halfcircle, testfunction), and use
it in the nested loop. Switch-type statement may also prove itself to be helpful. :)
- Make sure that your Makefile is going to properly rebuild the run image.
- You will need to build four separate source files,
one include file for prototypes, and a stub for my TestFunction
that will be supplied after submittal.
- TestFunction Stub
- Type of function is double.
- Name of function is TestFunction.
- Number of parameters is one.
- Type of parameter is double.
- Your stub will need to be called properly in your main
program. The value it returns (can be anything) should be
properly formatted as an output statement (see below).
- When we grade your program, we will overwrite your
TestFunction.c with our version of TestFunction.c and
your program will then produce an expected output that
we will check for.
- Inputs: none, the program just runs.
- Outputs: (#'s represents your calculated data):
CS201 - Lab 7 - Area Under The Curve
g(x) = XSquaredSinX
a = 0.00000, b = 3.14159
n = 2, h = #.#####, area = ######.#####
n = 4, h = #.#####, area = ######.#####
n = 8, h = #.#####, area = ######.#####
n = 16, h = #.#####, area = ######.#####
n = 32, h = #.#####, area = ######.#####
n = 64, h = #.#####, area = ######.#####
n = 128, h = #.#####, area = ######.#####
h(x) = HalfCircle
a =-2.00000, b = 2.00000
n = 2, h = #.#####, area = ######.#####
n = 4, h = #.#####, area = ######.#####
n = 8, h = #.#####, area = ######.#####
n = 16, h = #.#####, area = ######.#####
n = 32, h = #.#####, area = ######.#####
n = 64, h = #.#####, area = ######.#####
n = 128, h = #.#####, area = ######.#####
f(x) = TestFunction
a = 0.00000, b = 12.00000
n = 2, h = #.#####, area = ######.#####
n = 4, h = #.#####, area = ######.#####
n = 8, h = #.#####, area = ######.#####
n = 16, h = #.#####, area = ######.#####
n = 32, h = #.#####, area = ######.#####
n = 64, h = #.#####, area = ######.#####
n = 128, h = #.#####, area = ######.#####