IRM - Chapter - 6e - 02(3)

2019-04-23 19:55

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 using namespace std;

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


IRM - Chapter - 6e - 02(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:李春城案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: