< previous page page_117 next page >

Page 117
Variables
NameData TypeDescription
totMilesfloatTotal of rounded mileages
milesfloatAn individual rounded mileage

Now we're ready to write the program. Let's call it Walk. 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.
Here is the program:
//*****************************************************************
// Walk program
// This program computes the mileage (rounded to tenths of a mile)
// for each of four distances between points in a city, given
// the measurements on a map with a scale of one inch equal to
// one quarter of a mile
//*****************************************************************
#include <iostream.h>
#include <iomanip.h>  // For setprecision()

const float DISTANCE1 = 1.5;    // Measurement for first distance
const float DISTANCE2 = 2.3;    // Measurement for second distance
const float DISTANCE3 = 5.9;    // Measurement for third distance
const float DISTANCE4 = 4.0;    // Measurement for fourth distance
const float SCALE = 0.25;       // Map scale

int main()
{
    float totMiles;         // Total of rounded mileages
    float miles;            // An individual rounded mileage

    cout.setf(ios::fixed, ios::floatfield);   // Set up floating pt.
    cout.setf(ios::showpoint);                //   output format

totMiles = 0.0;

 
< previous page page_117 next page >