|
|
|
|
|
|
|
From the algorithm we can create tables of constants and variables to help us write the program declarations. |
|
|
|
|
| Constants | | Name | Value | Description | | INCH_HEIGHT | 30.0 | Height of a typical cone | | INCH_DIAMETER | 8.0 | Diameter of the base of the cone | | RED_PRICE | 0.10 | Price per square foot of red paint | | BLUE_PRICE | 0.15 | Price per square foot of blue paint | | GREEN_PRICE | 0.18 | Price per square foot of green paint | | INCHES_PER_FOOT | 12.0 | Inches in 1 foot | | PI | 3.14159265 | Ratio of circumference to diameter |
|
|
| Variables | | Name | Data Type | Description | | heightInFeet | float | Height of the cone in feet | | diameterInFeet | float | Diameter of the cone in feet | | radius | float | Radius of the cone in feet | | surfaceArea | float | Surface area in square feet | | redCost | float | Cost to paint a cone red | | blueCost | float | Cost to paint a cone blue | | greenCost | float | Cost 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()
|
|
|
|
|
|