Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
boxesPerMetricTon = 1 / metricTonsPerbox;
cout << \box = \
<< metricTonsPerbox << endl;
cout << \metric ton = \
<< boxesPerMetricTon << endl;
cout << \any other character ”
<< “terminates.\endl;
cin >> ans; } return 0;
6
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
}
A sample run follows:
enter the weight in ounces of your favorite cereal: 14
metric tons per box = 0.000396905 boxes to yield a metric ton = 2519.49
Y or y continues, any other characters terminates. y
enter the weight in ounces of your favorite cereal: 20
metric tons per box = 0.000567007
7
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
boxes to yield a metric ton = 1763.65
Y or y continues, any other characters terminates. n
2. Lethal Dose
Certain artificial sweeteners are poisonous at some dosage level. It is desired to know how much soda a dieter can drink without dying. The problem statement gives no information about how to scale the amount of toxicity from the dimensions of the experimental mouse to the dimensions of the dieter. Hence the student must supply this necessary assumption as basis for the calculation.
8
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
This solution supposes the lethal dose is directly proportional to the weight of the subject, hence
weightOfDieter lethalDoseDieter = lethalDoseMouse * weightOfMouse
This program accepts weight of a lethal dose for a mouse, the weight of the mouse, and the weight of the dieter, and calculates the amount of sweetener that will just kill the dieter, based on the lethal dose for a mouse in the lab. If the student has problems with grams and pounds, a pound is 454 grams.
It is interesting that the result probably wanted is a safe number of cans, while all the data can provide is the minimum lethal number! Some
9
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
students will probably realize this, but my experience is that most will not. I just weighed a can of diet pop and subtracted the weight of an empty can. The result is about 350 grams. The label claims 355 ml, which weighs very nearly 355 grams. To get the lethal number of cans from the number of grams of sweetener, you need the number of grams of sweetener in a can of pop, and the concentration of sweetener, which the problem assumes 0.1% , that is a conversion factor of 0.001.
gramsSweetenerPerCan = 350 * 0.001 = 0.35 grams/can
cans = lethalDoseDieter / (0.35 grams / can)
10
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley