< previous page page_83 next page >

Page 83
Now we're ready to write the program. Let's call it Mileage. We can take the declarations from the tables and create the executable statements from the algorithm. We must add comments and be sure to label the output.
Here is the program:
//******************************************************************
// Mileage program
// This program computes miles per gallon given four amounts
// for gallons used, and starting and ending mileage
//******************************************************************
#include <iostream.h>

const float AMT1 = 11.7;           // Number of gallons for fillup 1
const float AMT2 = 14.3;           // Number of gallons for fillup 2
const float AMT3 = 12.2;           // Number of gallons for fillup 3
const float AMT4 = 8.5;            // Number of gallons for fillup 4
const float START_MILES = 67308.0; // Starting mileage
const float END_MILES = 68750.5;   // Ending mileage

int main()
{
    float mpg;        // Computed miles per gallon

    mpg = (END_MILES - START_MILES) / (AMT1 + AMT2 + AMT3 + AMT4);

    cout < For the gallon amounts  < endl;
    cout < AMT1 <   < AMT2 <  
         < AMT3 <   < AMT4 < endl;
    cout < and a starting mileage of  < START_MILES < endl;
    cout < and an ending mileage of  < END_MILES < endl;
    cout < the mileage per gallon is  < mpg < endl;
    return 0;
}
The output from this program is
For the gallon amounts
11.7 14.3 12.2 8.5
and a starting mileage of 67308
and an ending mileage of 68750.5
the mileage per gallon is 30.888651
As the output of START_MILES shows, C++ does not display a decimal point and 0 when a floating point value is a whole number. Also, different versions of C++ may display either fewer or more decimal places (digits to

 
< previous page page_83 next page >