CS201
Lab 7 - Function Parameters
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 as parameter.
Readings:
- Read Chapter 7 in the Hanly/Koffman text. Read "Case Study: Bisection Method for Finding Roots" in page 348-356 for ideas and better understanding.
Deliverables: (DUE AFTER THE END OF YOUR LAB IN THE WEEK STARTING AT 9TH APRIL)
- Submittal of the following files:
- Makefile
- Proto.h
- Lab7.c
- Trap.c
- XSquaredSinX.c
- HalfCircle.c
- TestFunction.c
TO DO
- Design and Write a program to solve Programming Project #6
on page 363-364 of the Hanly/Koffman text.
- Your program should find the area under a curve y = f(x) between the lines x=a and x=b. Approximate this area by summing trapezoids (Formed by a line from x0 vertical up to the function, to y0 = f(x0), then straight line to y1 = f(x1), back down to the x-axis, and left to original.)
- We assume that function is positive over the interval [a,b].
- For n subintervals of length h, whereh=(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
passing by value is enough.
- Make sure to use nested (i.e. double)
for-type loop to calculate and print out your results.
- Define enumerated type to reflect your three functions (i.e.
xsquaredsinx, halfcircle, testfunction), and use
it in the nested loop. Switch-type statement may also be helpful.
- Make sure that your Makefile is going to properly rebuild the executable.
- 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 = ######.#####
Grading Scheme
Program compile and execute 20%
Program meets all specifications 60% (e.g. makefile, pass by value/reference, proto.h, enumerated types, nested for loop, function parameter etc. Also ask your TA specifically about what preciously he is looking for)
Program follow code conventions and contain proper documentation. 20%