Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
//Ch2Prob5.cc
//Input: lethal dose of sweetener for a lab mouse, weights // of mouse and dieter, and concentration of sweetener in a // soda.
//Output: lethal dose of soda in number of cans.
//Assumption: lethal dose
proportional to weight of subject // Concentration of sweetener in the soda is 1/10 percent #include
11
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
const double concentration = .001; // 1/10 of 1 percent const double canWeight = 350; const double gramsSweetnerPerCan = canWeight ??concentration; //units of grams/can int main() {
double lethalDoseMouse, lethalDoseDieter, weightMouse,
weightDieter; //units: grams double cans; char ans; do
12
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
{
cout << \the mouse in grams\
<< endl;
cin >> weightMouse; cout << \dose for the mouse in“
<< ”grams \ cin >> lethalDoseMouse; cout << \weight of the dieter in”
<<“ grams \ cin >> weightDieter; lethalDoseDieter = lethalDoseMouse
??weightDieter/weightMouse;
13
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
cout << \parameters:\\nmouse weight: \
<< weightMouse << \ << \mouse: \
<< lethalDoseMouse << \ << \weightDieter
<< \ << \grams of sweetener is: \
<< lethalDoseDieter << endl;
cans = lethalDoseDieter / gramsSweetnerPerCan;
14
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
cout << \cans of pop: \
<< cans << endl; cout << \any other character quits\
<< endl; cin >> ans;
} while ( 'y' == ans || 'Y' == ans ); return 0; }
A typical run follows: 17:23:09:~/AW$ a.out
Enter the weight of the mouse in grams 15
15
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley