< previous page page_120 next page >

Page 120
From the algorithm we can create tables of constants and variables to help us write the program declarations.
Constants
NameValueDescription
INCH_HEIGHT30.0Height of a typical cone
INCH_DIAMETER8.0Diameter of the base of the cone
RED_PRICE0.10Price per square foot of red paint
BLUE_PRICE0.15Price per square foot of blue paint
GREEN_PRICE0.18Price per square foot of green paint
INCHES_PER_FOOT12.0Inches in 1 foot
PI3.14159265Ratio of circumference to diameter

Variables
NameData TypeDescription
heightInFeetfloatHeight of the cone in feet
diameterInFeetfloatDiameter of the cone in feet
radiusfloatRadius of the cone in feet
surfaceAreafloatSurface area in square feet
redCostfloatCost to paint a cone red
blueCostfloatCost to paint a cone blue
greenCostfloatCost to paint a cone green

Now we can write the program, which we'll call ConePaint. We take the declarations from the tables and the executable statements from the algorithm. We have labeled the output with explanatory messages and formatted it with fieldwidth specifications. We've also added comments where needed.
//*****************************************************************
// ConePaint program
// This program computes the cost of painting traffic cones in
// each of three different colors, given the height and diameter
// of a cone in inches, and the cost per square foot of each of
// the paints
//******************************************************************
#include <iostream.h>
#include <iomanip.h>    // For setw() and setprecision()
#include <math.h>       // For sqrt()

 
< previous page page_120 next page >