IRM - Chapter - 6e - 02
更新时间:2024-05-25 03:26:01 阅读量: 综合文库 文档下载
- irm是什么意思推荐度:
- 相关推荐
Chapter 2 C++ Basics
1. Solutions to the Programming Projects:
1. Metric - English units Conversion A metric ton is 35,273.92 ounces. Write a C++ program to read the weight of a box of cereal in ounces then output this weight in metric tons, along with the number of boxes to yield a metric ton of cereal.
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
1
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
Design: To convert 14 ounces (of cereal) to metric tons, we use the 'ratio of units' to tell us whether to divide or multiply:
1 metric tons 14 ounces * * = 0.000397 metric tons 35,273 ounces
The use of units will simplify the determination of whether to divide or to multiply in making a conversion. Notice that ounces/ounce becomes unit-less, so that we are left with metric ton units. The number of ounces will be very, very much larger than the number of metric tons. It is then reasonable to divide the number of ounces by the number of ounces in a metric ton to get the number of metric tons.
2
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
Now let metricTonsPerBox be the weight of the cereal box in metric tons. Let ouncesPerBox the be the weight of the cereal box in ounces. Then in C++ the formula becomes:
const double ouncesPerMetric_ton = 35272.92;
metricTonsPerBox = ouncesPerBox / ouncesPerMetricTon;
This is metric tons PER BOX, whence the number of BOX(es) PER metric ton should be the reciprocal:
boxesPerMetricTon = 1 / metricTonsPerBox;
3
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
Once this analysis is made, the code proceeds quickly:
//Purpose: To convert cereal box weight from ounces to
// metric tons to compute number of boxes to make up a // metric ton of cereal.
#include
const double ouncesPerMetricTon = 35272.92;
4
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
int main() {
double ouncesPerBox, metricTonsPerbox,
boxesPerMetricTon; char ans = 'y';
while( 'y' == ans || 'Y' == ans ) {
cout << “enter the weight in ounces of your”
<< “favorite cereal:” < cin >> ouncesPerBox; metricTonsPerbox = ouncesPerBox / ouncesPerMetricTon; 5 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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 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 Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 Enter the lethal dose for the mouse in grams 100 Enter the desired weight of the dieter, in grams 45400 For these parameters: mouse weight: 15 grams lethal dose for the mouse: 100 grams Dieter weight: 45400 grams The lethal dose in grams of sweetener is: 302667 Lethal number of cans of pop: 864762 Y or y continues, any other character quits y 16 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 Enter the weight of the mouse in grams 30 Enter the lethal dose for the mouse in grams 100 Enter the desired weight of the dieter, in grams 45400 For these parameters: mouse weight: 30 grams lethal dose for the mouse: 100 grams Dieter weight: 45400 grams The lethal dose in grams of sweetener is: 151333 Lethal number of cans of pop: 432381 17 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 Y or y continues, any other character quits q 17:23:56:~/AW$ 3. Pay Increase The workers have won a 7.6% pay increase, effective 6 months retroactively. This program is to accept the previous annual salary, then outputs the retroactive pay due the employee, the new annual salary, and the new monthly salary. Allow user to repeat as desired. The appropriate formulae are: const double INCREASE = 0.076; 18 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 newSalary = salary * (1 + INCREASE); monthly = salary / 12; retroactive = (salary – oldSalary)/2; The code follows: //Ch2Prob3.cc //Given 6 mos retroactive 7.6% pay increase, //input salary //Output new annual and monthly salaries, retroactive pay #include const double INCREASE = 0.076; 19 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 int main() { double oldSalary, salary, monthly, retroactive; char ans; cout << \salary.\ << \salary, monthly ” << “salary, and retroactive pay.\ cin >> oldSalary;//old annual salary salary = oldSalary*(1+INCREASE);//new annual salary monthly = salary/12; 20 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 retroactive = (salary – oldSalary)/2; cout << \salary << endl; cout << \<< monthly << endl; cout << \due: \ << retroactive << endl; return 0; } 17:50:12:~/AW$ a.out Enter current annual salary. 100000 I'll return new annual salary, monthly salary, and retroactive pay. new annual salary 107600 21 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 new monthly salary 8966.67 retroactive salary due: 3800 4. Retroactive Salary // File: Ch2.4.cpp // Modify program from Problem #3 so that it calculates retroactive // salary for a worker for a number of months entered by the user. //Given a 7.6% pay increase, //input salary //input number of months to compute retroactive salary //Output new annual and monthly salaries, retroactive pay #include const double INCREASE = 0.076; 22 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 int main() { using std::cout; using std::cin; using std::endl; double oldSalary, salary, monthly, oldMonthly, retroactive; int numberOfMonths; // number of months to pay retroactive increase char ans; cout << \salary and a number of months\\n\ << \compute retroactive pay.\\n\ << \salary, monthly \ << \pay.\ cin >> oldSalary;//old annual salary cin >> numberOfMonths; 23 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 salary = oldSalary * (1+INCREASE); //new annual salary oldMonthly = oldSalary/12; monthly = salary/12; retroactive = (monthly - oldMonthly) * numberOfMonths; // retroactive = (salary - oldSalary)/2; // six months retroactive pay increase. cout << \salary << endl; cout << \<< monthly << endl; cout << \\ << retroactive << endl; return 0; } /* Typical run 24 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 Enter current annual salary and a number of months for which you wish to compute retroactive pay. I'll return new annual salary, monthly salary, and retroactive pay. 12000 9 new annual salary 12912 new monthly salary 1076 retroactive salary due: 684 Press any key to continue */ 5. No solution provided. 6. No solution provided. 7. Payroll 25 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 This problem involves payroll and uses the selection construct. A possible restatement: An hourly employee's regular payRate is $16.78/hour for hoursWorked <= 40 hours. If hoursWorked > 40 hours, then (hoursWorked -40) is paid at an overtime premium rate of 1.5 * payRate. FICA (social security) tax is 6% and Federal income tax is 14%. Union dues of $10/week are withheld. If there are 3 or more covered dependents, $15 more is withheld for dependent health insurance. a) Write a program that, on a weekly basis, accepts hours worked then outputs gross pay, 26 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 each withholding amount, and net (take-home) pay. b) Add 'repeat at user discretion' feature. I was unpleasantly surprised to find that with early GNU g++ , you cannot use a leading 0 (such as an SSN 034 56 7891) in a sequence of integer inputs. The gnu iostreams library took the integer to be zero and went directly to the next input! You either have to either use an array of char, or 9 char variables to avoid this restriction. Otherwise, the code is fairly straight forward. //file Ch2Prob7.cc 27 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 //pay roll problem: //Inputs: hoursWorked, number of dependents //Outputs: gross pay, each deduction, net pay // //This is the 'repeat at user discretion' version //Outline: //In a real payroll program, each of these values would be //stored in a file after the payroll calculation was printed //to a report. // //regular payRate = $10.78/hour for hoursWorked <= 40 //hours. //If hoursWorked > 40 hours, 28 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 // overtimePay = (hoursWorked - 40) * 1.5 * PAY_RATE. //FICA (social security) tax rate is 6% //Federal income tax rate is 14%. //Union dues = $10/week . //If number of dependents >= 3 // $15 more is withheld for dependent health insurance. // #include const double PAY_RATE = 16.78; const double SS_TAX_RATE = 0.06; const double FedIRS_RATE = 0.14; const double STATE_TAX_RATE = 0.05; 29 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 const double UNION_DUES = 10.0; const double OVERTIME_FACTOR = 1.5; const double HEALTH_INSURANCE = 15.0; int main() { double hoursWorked, grossPay, overTime, fica, incomeTax, stateTax, union_dues, netPay; int numberDependents, employeeNumber; char ans; //set the output to two places, and force .00 for cents 30 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2); // compute payroll do { cout << \(digits only,” << “ no spaces or dashes) \\n”; cin >> employeeNumber ; cout << “Please the enter hours worked and number “ << “of employees.” << endl; cin >> hoursWorked ; cin >> numberDependents; 31 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 cout << endl; if (hoursWorked <= 40 ) grossPay = hoursWorked * PAY_RATE; else { overTime = (hoursWorked - 40) * PAY_RATE * OVERTIME_FACTOR; grossPay = 40 * PAY_RATE + overTime; } fica = grossPay * SS_TAX_RATE; 32 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 incomeTax = grossPay * FedIRS_RATE; stateTax = grossPay * STATE_TAX_RATE; netPay = grossPay - fica - incomeTax - UNION_DUES - stateTax; if ( numberDependents >= 3 ) netPay = netPay - HEALTH_INSURANCE; //now print report for this employee: cout << \ 33 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 << employeeNumber << endl; cout << \hoursWorked << endl; cout << \<< PAY_RATE << endl; if (hoursWorked > 40 ) { cout << \worked: \ << hoursWorked - 40 << endl; cout << \premium: \ << OVERTIME_FACTOR << endl; 34 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 } cout << \grossPay << endl; cout << \<< fica << endl; cout << \withheld: \ << incomeTax << endl; cout << \\ if (numberDependents >= 3 ) cout << \Premium withheld: \ << HEALTH_INSURANCE << endl; 35 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 cout << \Union Dues withheld: \ << UNION_DUES << endl; cout << \<< endl << endl; cout << \another employee?” << “ Y/y repeats, any other ends\ cin >> ans; } while( 'y' == ans || 'Y' == ans ); return 0; } //A typical run: 36 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 14:26:48:~/AW $ a.out Enter employee SSN (digits only, no spaces or dashes) 234567890 Please the enter hours worked and number of employees. 10 1 Employee number: 234567890 hours worked: 10.00 regular pay rate: 16.78 gross pay: 167.80 FICA tax withheld: 10.07 Federal Income Tax withheld: 23.49 State Tax withheld: 8.39 Flabbergaster's Union Dues withheld: 10.00 37 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 Net Pay: 115.85 Compute pay for another employee? Y/y repeats, any other ends y Enter employee SSN (digits only, no spaces or dashes) 987654321 Please the enter hours worked and number of employees. 10 3 Employee number: 987654321 hours worked: 10.00 regular pay rate: 16.78 gross pay: 167.80 FICA tax withheld: 10.07 Federal Income Tax withheld: 23.49 38 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 State Tax withheld: 8.39 Health Insurance Premium withheld: 35.00 Flabbergaster's Union Dues withheld: 10.00 Net Pay: 80.85 Compute pay for another employee? Y/y repeats, any other ends y Enter employee SSN (digits only, no spaces or dashes) 123456789 Please the enter hours worked and number of employees. 45 3 Employee number: 123456789 hours worked: 45.00 39 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Savitch Problem Solving w/ C++, 6e Instructor’s Resource Guide Chapter 2 regular pay rate: 16.78 overtime hours worked: 5.00 with overtime premium: 1.50 gross pay: 797.05 FICA tax withheld: 47.82 Federal Income Tax withheld: 111.59 State Tax withheld: 39.85 Health Insurance Premium withheld: 35.00 Flabbergaster's Union Dues withheld: 10.00 Net Pay: 552.79 Compute pay for another employee? Y/y repeats, any other ends n 14:28:12:~/AW $ 40 Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
正在阅读:
飘扬的红领巾作文600字07-01
拔牙记作文800字07-16
最高院案例(2004)民一终字第46号(关于城市房地产管理法第39条的04-25
调研报告怎么写03-15
钢筋滚扎直螺纹连接质量控制要点04-11
06第05章 - 材料与截面 - 沈万湘04-25
选修八unit5知识点02-27
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- Chapter
- IRM
- 6e
- 02
- 2010学年七年级( 上)语文期末卷(五)
- 完整打印版人教版小学一年级下册第、七八单元语文教案及反思
- 新版北师大版小学数学一年级下册教材分析教学计划及第一单元教案
- 五年级上学期人文与社会3,4课 - 图文
- 小学教师资格考试《教育学》模拟试卷
- 高速公路隧道消防设计技术规程-广西交通运输标准化技术
- 甲级单位编制鱼腥草注射液项目可行性报告(立项可研+贷款+用地+2
- 消防实习报告
- 专题14 动植物生命活动调节试题精练-2018年高考生物黄金20天黄金
- 对输变电设备的状态检修进行研究
- 柬埔寨金边十一郎区块链共享单车营业所财务日报表
- 六年级数学易错题
- 粉末冶金
- 计数原理学案(孙平公开课)
- 第03章 1NT开叫及以后的叫牌
- 同志的入党政审调查笔录
- 新建铁路贵阳至南宁客运专线环境影响报告书(简本)
- Dhcddem深圳出入境检验局行政执法证考试复习题
- 住宅楼施工组织设计
- 2015年全国各地高考物理模拟试题专题汇编专题6 物理实验 第2讲