Classwork 8: Functions

Objectives

To practice using functions in the math library and to write a simple function of your own.

The Assignment

Version 1


Write a program that calculates the cube root of a number that the user types in. A sample run of your program might look like this:

PT[193]% ./a.out
This program calculates the cube root.
x = ? 27
The cube root of 27.000000 is 3.000000
PT[194]% 

PT[194]% ./a.out
This program calculates the cube root.
x = ? 64
The cube root of 64.000000 is 4.000000
PT[195]% 

PT[195]% ./a.out
This program calculates the cube root.
x = ? 100
The cube root of 100.000000 is 4.641589
PT[196]% 

PT[196]% ./a.out
This program calculates the cube root.
x = ? 125
The cube root of 125.000000 is 5.000000
PT[197]% 

To calculate the cube root, use the pow() function from the math library. To do so, you must have the following preprocessor directive at the top of your program:

#include <math.h>

The pow() function has the following prototype:

   double pow(double x, double y) ;

DO NOT ADD THIS PROTOTYPE TO YOUR PROGRAM.

This means that when you call pow() you must supply it with two double values for input (a.k.a. the actual parameters) and it will produce a double value as output (a.k.a. the return value). (Note: you should not have the function prototype of pow() in your program. It is already declared in the math.h header file.) For example, the following assignment should store the value 8.0 in the variable r.

    r = pow(2.0, 3.0) ;

Note: the expression 1/3 evaluates to 0 because it is an integer divided by an integer, so the division is integer division. Use 1.0/3.0 or 0.3333333333333.

Name your program cuberoot1.c, compile it using the -Wall and -lm options:

gcc -Wall cuberoot1.c -lm

You need the -lm flag to tell gcc to use the math library. Make sure your main() program has return type int and has as its last statement:

   return 0 ;

Then, gcc -Wall should give you no warnings. (If it does, then you have an actual error in your program.)

Use the Unix script command to record the compilation of your program with the -Wall option and several sample runs of your program. Then submit both your program and the typescript file. (Remember to exit from script, first.)

submit cs104_chang cw08 cuberoot1.c
submit cs104_chang cw08 typescript


Version 2


Make a copy of your cuberoot1.c file and call the new file cuberoot2.c.

For version 2 of your assignment, create a function that computes the cuberoot of a number. Call this function cbrt cuberoot. Your cuberoot function should not print anything to the screen or ask the user for input. (See example functions from class notes.) It should take one double value for input (its "formal parameter") and return the cube root of that value.

Modify your main program so it uses your own cuberoot() function instead of calling pow() from the math library. The sample runs of your program should look the same as before.

Use the script command to record yourself compiling and running the second version of your program, except use the option -a typescript2 so you don't overwrite your previous sample runs:

script -a typescript2

Finally, submit the second version of your program using:

submit cs104_chang cw08 cuberoot2.c
submit cs104_chang cw08 typescript2


Be sure to logout completely when you have finished!