Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:cube-functions [2014/10/02 15:17] (current)
kseppi created
Line 1: Line 1:
 +<code cpp>
 +#include <​iostream>​
 +#include <​iomanip>​
 +#include <​cmath>​
 +using namespace std;
 +
 +/*
 +Computes the volume of a cube.
 +@param sideLength the side length of the cube
 +@return the volume of the cube
 +*/
 +double cubeVolume(double sideLength) {
 + if(sideLength <= 0) {
 + return 0;
 + }
 + return pow(sideLength,​ 3); // functions can call functions
 + //return sideLength * sideLength * sideLength; // this also works
 +}
 +
 +/*
 +Handy function for testing the cubeVolume() function. Prints
 +useful information to the console.
 +@param arg the argument to pass to cubeVolume()
 +*/
 +void cubeVolume_test(double testArg, double expected) {
 + double result = cubeVolume(testArg);​
 + cout << "​sidelength:​ " << testArg << endl;
 + cout << "​volume:​ " << result << endl;
 + cout << "​expected:​ " << expected << endl;
 + const double EPSILON = 1E-10;
 + if( abs(result-expected) > EPSILON ) {
 + cout << "Big Massive ERROR!"​ << endl;
 + }
 + cout << endl;
 +}
 +
 +/*
 +Functions compartmentalize related chuncks of code. This makes
 +testing easier. It also enables code reuse, which reduces code
 +duplication.
 +*/
 +int main() {
 +
 + /*
 + functions make code easier to test; we can run as many
 + of these tests as we want, very easily; this is easier ​
 + than copying and pasting the cube volume code many times 
 + to test it, and it's much easier than running the 
 + program many times by hand with different input values
 + */
 + cout << fixed << setprecision(10);​
 + cubeVolume_test(4,​ 64);
 + cubeVolume_test(0,​ 0);
 + cubeVolume_test(10,​ 1000);
 + cubeVolume_test(3.456,​ 41.278242816);​
 +
 + /*
 + functions are also reusable; now if I need to calculate
 + the volume of a cube multiple times in different places
 + in my program, I can just call the function each time, 
 + rather than copying and pasting the code; what's more, 
 + if I had copyied the code to reuse it, but later found
 + a bug in the code, I would have to fix the bug in many 
 + places; on the other hand, if I use functions, I can 
 + test and fix the code in only one place, which is 
 + significnatly easier to do
 + */
 +
 + // somewhere in your program, calculate shipping costs
 + const double PRICE_PER_POUND = 0.5; // dollars
 + const double PRICE_PER_CUBIC_IN = 0.25; // dollars
 + double boxWeight_lbs = 14.5; // input from user
 + double boxLength_in = 12.36; // input from user
 + double shippingCost = boxWeight_lbs*PRICE_PER_POUND + cubeVolume(boxLength_in)*PRICE_PER_CUBIC_IN;​
 +
 + // somewhere else in your program, calculate maximum shipping cost estimates
 + double objectLongestSide_in = 16.4; // input from user
 + double objectWeight_lbs = 37.0; // input from user
 + double maxiumPossibleBoxVolume = cubeVolume(objectLongestSide_in);​
 + double estMaxiumShippingCost = objectWeight_lbs*PRICE_PER_POUND + cubeVolume(boxLength_in)*PRICE_PER_CUBIC_IN;​
 +
 + /*
 + But wait! looks like the shipping cost calculation can 
 + also be extracted out into a function...
 + Anytime you see duplicated code, it's a sign that you
 + probably need to create a new function to encapsulate it
 + */
 +
 + cin.get();
 + return 0;
 +
 +}
 +</​code>​
  
cs-142/cube-functions.txt ยท Last modified: 2014/10/02 15:17 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0