Please send questions to
st10@humboldt.edu .
CIS 130 - Week 8, Monday, March 8, 2010
* more practice writing basic C++ functions
* consider: we'd like to write a C++ function to
compute the area of a circle
* you can type the following directly in using funct_play2
on nrs-labs -- I'm going to figure out the pieces
first, and then paste them in
* its contract:
// contract: circ_area: double -> double
* its purpose statement:
expects a circle's radius, and produces the
area of that circle
* its header:
double circ_area(double radius)
* its examples -- and since double values (floating
point numbers, double-precision) are involved,
exact equality is dicey...
...so, I'd like the absolute value of the
difference between the value produced by an
example call, and the expected value of the
example call -- and I'd like that absolute
value to be less than... well, 0.001
abs( circ_area(10) - 314.159 ) < 0.001
* we would like a named constant here;
(NOTE: REMEMBER, named constants should be
written in all-uppercase...)
const double PI = 3.14159;
* so, a body to this function could be:
{
return PI * radius * radius;
}
* I want a function to tell me if a temperature
is in the "safe" range of [50, 100)
* contract:
// contract: is_safe: double -> bool
* purpose:
purpose:
expects a temperature (in Celsius) and
produces whether it is in the safe range
of [50, 100)
* header:
bool is_safe(double temp)
* examples:
I need 5 specific examples/test cases here...
(to left of interval, to right of interval,
in the interval, and both boundaries...)
is_safe(47) == false
is_safe(50) == true
is_safe(90) == true
is_safe(100) == false
is_safe(200) == false
* any named constants? yes, ought to have, here;
const int MIN_SAFE = 50;
const int MAX_SAFE = 100;
* and the body?
{
return (temp >= MIN_SAFE) && (temp < MAX_SAFE);
}
* REMEMBER:
* ...if you make an error in the contract or purpose
statement, edit the .cpp file (using the nano text editor,
or your favorite text editor on nrs-labs)
* ...if you make an error in the header of your function,
edit it in BOTH the .cpp and the .h file!
* ...if you make an error in an example/test, edit it
in BOTH the .cpp file and the _ck_expect.cpp file
(but if the _ck_expect.cpp file editing is too much
at this point, ask me for help!)
* ...if you make an error in a named constant declaration,
edit it in the .h file;
* ...if you make an error in the function body,
edit it in the .cpp file;
* ...and after such editing, you can re-compile your
function using the funct_compile tool
(or the funct_play2 tool, if you are careful to
note that a file for your function already exists,
so it won't ask you to create it all over...)