Laboratory Explorations: Functions
Collaborate with your assigned partner in completing this activity.
1. Download this file: checkit.h
Save this file in the same folder as your other programs.
Using a text editor, create a new file and enter the source code from
this file: geometry.c
This program is based on Fig 3.21 in your text. If you haven't
already read Section 3.5, do so now.
2. Compile the geometry.c program. You should see several errors.
To correct the errors, perform these steps:
- Add #include for the math library.
- Add a constant for kPi:
#define kPi 3.14
Now compile the program again and make sure it compiles successfully.
Execute the program. The output should display "Test
passed on line 34." (The line number might vary if you added
extra blank lines.) This message is displayed by the checkit_double()
function. It confirms that call find_circum()
with a value of zero will return zero.
3. Add a the following statement to the test_cases() function of
the program.
checkit_double(find_circum(3), 18.84);
4. Predict: Using a calculator, predict what will returned from calling
find_circum() with a value of three. Execute
the program and observe what is displayed by checkit.
5. Introduce an error in find_circum(). Change
"2.0" to "1.0". Execute the program and observe what is
displayed by checkit. We use checkit to see if the actual
result from a function matches the expected result.
Checkit provides an automated mechanism for verifying function
results that is fast and reliable. We will use it extensively
throughout the course.
6. Repair the error you introduced in the previous step.
Next we want to determine if the find_area()
function is working correctly. Invent a value for the radius, and
use a calculator to compute the area of a circle with that
radius. In the test_cases() method add
another call to checkit to see if find_area()
produces the expected result for the radius you invented. Your
statement will look like this one, but you must fill in the blanks:
checkit_double( find_area( ____ ), ____
);
7. When you want to find the square of a number, the best way is to
multiply the number by itself, not use the "pow" function.
Change the body of the find_area function so that it
multiplies r times itself instead of using the "pow"
function.
8. Move the declaration of the find_area function BELOW the main
function. When you compile, this should give an error. Write down the
error message that is reported. Leave the declaration below the
main, and add a function prototype to correct the error.
9. Remove the brace on the line after the return in the find_circum
function. When you compile, this should give an error. Write down
the error message that is reported. Now repair the error by replacing
the brace.
10. Create a local variable in the find_area function
called "result" and assign it the result of the area
computation. Then change the return statement so that it returns
the value of result.
11. Write a new function, find_volume, that returns the
volume of a cylinder with a given height and radius. The function
will require two arguments; you can make them both be of type double.
Use the formula
12. Write a testcase for find_volume. Predict the volume for
a known height and radius using a calculator. Now write a call to
checkit that evaluates find_volume() to see if it works
correctly. Your statement will look like this one, but you must fill in
the blanks:
checkit_double( find_volume( ____, ____ ),
____ );
12. Change the body of find_volume so that it invokes find_area
to perform the area computation.
13. (Optional - Uses selection from Chapter 4.) Add a
decision so that the computations are performed only if the radius and
height are both positive non-zero numbers, otherwise display an error
message.
Show your executing program to your instructor. You do not need
to use handin to submit your work.