Homework 14: Volume Function (and friends)
This assignments exercises your ability to define your own functions and use them.
Concepts
Focus on one main concept for this assignment: how functions are like "mini-programs" that you can define and use.
Common C++ Library Functions
Remember in 07_rockPaperScissors how you were required to convert a letter from lower case to upper case (or vice-versa)? Your implementation was a critical exercise in learning something about char
variables, but you should have asked yourself, "Surely, some genius somewhere has come across this problem before and has solved it? How can I leverage the work that they have done?"
In this case, you could have used a C++ library function called toupper
.
// ...
cin >> player1;
player1 = toupper(player1);
There are many "standard" functions in a handful of typical C++ libraries. While you won't be tested on knowing them all, you should be familiar with them since they tend to take care of common programming tasks. For example, what do the cctype
library functions tolower and isalnum do?
Defining Your Own Functions
As covered in lecture, there are multiple ways to define your own functions. For now, focus on the style of "defining functions above main." For example:
using namespace std;
// Returns the sum of x and y.
int plus(int x, int y) {
return x + y;
}
int main() {
int a = 2, b = 3;
cout << plus(4, 5);
cout << plus(a, b);
return 0;
}
Notice how we defined what "plus" is by defining the function implementation for plus
above main
. Notice how, in main
, we used the function plus
, passing it the necessary arguments it needs in order for it to do what it needs to do (sum two numbers and return the result).
Instructions
Rewrite the typical "sphere volume" assignment using library functions and a user-defined function called sphereVolume
. An example interaction will look like this:
Enter the radius of a sphere: 3
The volume of a sphere with radius 3 is 113.097 units cubed.
Whooopee!
Your program must:
- Use the
cmath
library'sacos
function to initialize the value of adouble
constant calledPI
. - Use the
cmath
library'spow
function to cube the radius. - Define a function called
sphereVolume
that accepts one integer parameter calledradius
and returns adouble
value equal to the volume of a sphere with radiusradius
.
Remember that in order to use the library functions acos
and pow
, you must include the cmath
library.
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Hel-lo. It's lonely being a cyborg. Can we be friends?
Your program must use the acos
function to initialize a value for PI
.
Your program must use the pow
function to calculate the volume of a sphere.
Your program must define a function called sphereVolume
according to the requirements.
Your main
subroutine must use your sphereVolume
function.
Your program must prompt the user for input only once.
Your program must print the line "The volume of a sphere with radius R is V units cubed" where R is an integer and V is a decimal number representing the volume of a sphere with radius R.
Your program must terminate after printing the above line.
It is a *bzzzt* pleasure serving you. Please come again.
This work is worth 50 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 2 | |
Uses acos to initialize a value for PI |
5 | |
Uses pow when calculating volume. |
5 | |
Correct implementation of sphereVolume function above main |
15 | |
Meaningful comment for sphereVolume function |
3 | |
Uses the sphereVolume function to calculate volume |
5 | |
Correct input prompt | 5 | |
Correct output prompt | 5 | |
Correctly calculates volume of sphere | 5 |
Concepts Exercised: defining functions, calling functions, libraries, declaring facts.
© 2011 Yong Joseph Bakos.