Figure 3.1 |
/* mass2energy.cpp computes energy from a given mass using
* Einstein’s mass-to-energy conversion equation.
*
* Input: The mass (in kilograms) being converted to energy
* Precondition: mass >= 0
* Output: The amount of energy (in kilojoules) corresponding
* to mass
*******************************************************************/
#include <iostream> // cin, cout, <<, >>
#include <cassert> // assert()
#include <cmath> // pow()
using namespace std;
int main()
{
const double SPEED_OF_LIGHT = 2.997925e8; // meters/sec
cout << "To find the amount of energy obtained from a given mass,\n"
<< "enter a mass (in kilograms): ";
double mass;
cin >> mass; // get mass
assert(mass >= 0); // make sure it's nonnegative
// compute energy
double energy = mass * pow(SPEED_OF_LIGHT, 2);
// display energy
cout << mass << " kilograms of matter will release "
<< energy << " kilojoules of energy.\n";
}
|
Sample Runs for Figure 3.1 |
To find the amount of energy obtained from a given mass, enter a mass (in kilograms): 1 1 kilograms of matter will release 8.98755e+16 kilojoules of energy. To find the amount of energy obtained from a given mass, enter a mass (in kilograms): 2 2 kilograms of matter will release 1.79751e+17 kilojoules of energy. To find the amount of energy obtained from a given mass, enter a mass (in kilograms): 125.5 125.5 kilograms of matter will release 1.12794e+19 kilojoules of energy. |
Figure 3.2 |
/* tripCost.cpp calculates the total cost and miles per gallon
* of a vehicle, based on the miles traveled, fuel consumed,
* cost per gallon of fuel, and operating cost per mile.
*
* Input: The total miles traveled, total fuel consumed,
* unit cost of the fuel, and operating cost per mile.
* Precondition: miles, gallonsOfFuel, unitFuelCost and
* unitOperatingCost are all positive.
* Output: The miles per gallon, total cost of the trip
* and the cost per mile.
***************************************************************/
#include <iostream> // cin, cout, <<, >>, fixed, showpoint
#include <iomanip> // setw(), setprecision()
#include <cassert>
using namespace std;
int main()
{
const int WIDTH = 7; // width of output field
cout << "Enter:\n\ttotal miles traveled,"
<< "\n\tgallons of fuel used,"
<< "\n\ttotal cost per gallon of the fuel, and"
<< "\n\toperating cost per mile."
<< "\n\t---> ";
double miles, // total miles traveled
gallonsOfFuel, // total gallons used
unitFuelCost, // fuel cost per gallon
unitOperatingCost; // operating cost per mile
cin >> miles >> gallonsOfFuel
>> unitFuelCost >> unitOperatingCost;
assert(miles > 0 && gallonsOfFuel > 0 &&
unitFuelCost > 0 && unitOperatingCost > 0);
double milesPerGallon = miles / gallonsOfFuel,
fuelCost = unitFuelCost * gallonsOfFuel,
operatingCost = unitOperatingCost * miles,
totalTripCost = fuelCost + operatingCost,
costPerMile = totalTripCost / miles;
cout << showpoint << fixed << setprecision(2)
<< "\n\tMiles per gallon: " << setw(WIDTH) << milesPerGallon
<< "\n\tTotal cost: $" << setw(WIDTH) << totalTripCost
<< "\n\tCost per mile: $" << setw(WIDTH) << costPerMile
<< endl << endl;
}
|
Sample Runs for Figure 3.2 |
Enter: total miles traveled, gallons of fuel used, total cost per gallon of the fuel, and operating cost per mile. ---> 10 1 1.50 3.50 Miles per gallon: 10.00 Total cost: $ 36.50 Cost per mile: $ 3.50 Enter: the total miles traveled, the gallons of fuel used, the total cost per gallon of the fuel, and the operating cost per mile. ---> 100 10 15 10 Miles per gallon: 10.00 Total cost: $1150.00 Cost per mile: $ 11.50
|