已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Chapter Four Selection and Iteration Exercises1. Rewrite the following if-else using a switch statement:if ( marriage_status = S )cout single ;else if ( marriage_status = M )cout married ;else if ( marriage_status = W )cout widowed ;else if ( marriage_status = E )cout separated ;else if ( marriage_status = D )cout divorced ;elsecout error: invalid code ;解答:switch (marriage_status)case S:cout single ;break;case M: cout married ;break;case W: cout widowed ;break;case E: cout separated ;break;case D:cout divorced ;break;default:cout error: invalid code ;2. The following program segment displays an appropriate message depending on the values of three integers: n1, n2, and n3.if ( n1= n2 )if (n1 = n3 )cout n1, n2 and n3 have the same value endl ;elsecout n1 and n2 have the same value endl ;else if ( n1 = n3 )cout n1 and n3 have the same value endl ;else if ( n2 = n3 )cout n2 and n3 have the same value endl ;elsecout n1, n2 and n3 have different values endl ;Use spaces to improve the readability of this code.To test the various branches in this code you will need to construct five sets of test data, each set testing one of the branches. Construct the five sets of test data for n1, n2, and n3.解答:/*测试用例为:1 1 1 ,1 1 2 ,1 2 1 ,1 2 2 ,1 2 3*/#includeusing namespace std;int main()int n1,n2,n3;cinn1n2n3;if(n1=n2)if(n1=n3)coutn1,n2 and n3 have the same valueendl;elsecoutn1 and n2 have the same valueendl;else if(n1=n3)coutn1 and n3 have the same valueendl; else if(n2=n3)coutn2 and n3 have the same valueendl; elsecoutn1,n2 and n3 have the different valueendl;return 0;3. Write a program to read in two integers and check if the first integer is evenly divisible by the second. (Hint: use the modulus operator %.)解答:#includeusing namespace std;int main()int n1,n2;coutn1n2;if(n1%n2=0)coutn1 is evenly divisible by n2endl;else coutn1 is not evenly divisible by n2endl;return 0;4. Input two numbers and find the smaller of the two using the conditional operator ?:.解答:#includeusing namespace std;int main()int n1,n2;int smaller;coutn1n2;if(n1!=n2)smaller=(n1n2)?n1:n2;coutthe smaller one is smallerendl;elsecoutn1=n2endl;return 0;5. In a triangle, the sum of any two sides must be greater than the third side. (triangle:三角形)Write a program to input three numbers and determine if they form a valid triangle.解答:#includeusing namespace std;int main()double a ,b ,c;coutabc;if(a+b)c)&(a+c)b)&(c+b)a)coutthe three number can form a valid triangle.endl;return 0;coutthe three number can not form a valid triangle.endl;return 0;6. Input a persons height in centimetres and weight in kilograms and display a message indicating that they are either underweight, overweight or normal weight. As an approximation, a person is underweight if their weight is less than their height divided by 2.5 and they are overweight if their weight is greater than their height divided by 2.3.解答:#includeusing namespace std;int main()double height,weight;coutheightweight;if(weightheight/2.5)coutunderweight!endl;else if(weightheight/2.3)coutnormal weight!endl; elsecoutoverweightendl;return 0;7. Write a program that reads a single numeral from the keyboard and displays its value as a word. For example, an input of 5 will display the word five.解答:#includeusing namespace std;int main()int num;coutnum;switch(num)case 0:coutzeroendl; break;case 1:coutoneendl; break;case 2:couttwoendl; break;case 3:coutthreeendl; break;case 4:coutfourendl; break;case 5:coutfiveendl; break;case 6:coutsixendl; break;case 7:coutsevenendl; break;case 8:couteightendl; break;case 9:coutnineendl; break;default: coutplease input a single numberalendl;return 0;8. Write a program to input a number 1 to 7 from the keyboard, where 1 represents Sunday, 2 Monday, 3 Tuesday, etc. Display the day of the week corresponding to the number typed by the user. If the user types a number outside the range 1 to 7, display an error message.解答:#includeusing namespace std;int main()int theday;cintheday;int flag=1;while(flag)switch(theday)case 1:coutsundayendl; flag=0; break;case 2:coutmondayendl;flag=0;break;case 3:couttuesdayendl;flag=0;break;case 4:coutwednesdayendl;flag=0;break;case 5:coutthursdayendl;flag=0;break;case 6:coutfridayendl;flag=0;break;case 7:coutsaturdayendl;flag=0;break;default: couttheday;return 0;9. Add the increment operator (I or i) and the decrement operator (D or d) to the simple calculator program P4D.解答:#includeusing namespace std;int main()double a,b;double result;char opt;coutplease input like :1 i 3 or 1 d 3 aoptb;if(opt=i|opt=I)result=a+b;else if(opt=d|opt=D)result=a-b;else coutinput error!endl;coutthe result is result;return 0;10. Write a program to input the time of day in Ireland and display the equivalent time in Washington (- 5 hours), Moscow (+ 3 hours), and Beijing (+ 8 hours). Input the time in the 24-hour format, e.g. 22:35 (11:35 p.m.).解答:#includeint main()int hour,minute;int h;printf(please input the time of day in Ireland in 24-hour format );scanf(%d:%d,&hour,&minute);if(hour=0&hour=0&minute=59)if(h=hour-5)0) h+=24;printf(the equivalent time in Washington is %d:%d ,in Moscow is %d:%d ,in Beijing is %d:%d .,h,minute,(hour+3)%24,minute,(hour+8)%24,minute);else printf(error input!);return 0;11. Write a program to display the effects of an earthquake based on the Richter scale value: Richter scale value EffectsLess than 4 Little.4.0 to 4.9 Windows shake.5.0 to 5.9 Walls crack; poorly built buildings are damaged.6.0 to 6.9 Chimneys tumble; ordinary buildings are damaged.7.0 to 7.9 Underground pipes break; well-built buildings are damaged.More than 7.9 Ground rises and falls in waves; most buildings are destroyed.(Richter scale value:里氏震级)解答:#includeint main()float rsvalue;cinrsvalue;if(rsvalue0)int value=rsvalue;switch(value)case 1:;case 2:;case 3:coutLittle effects!endl;break; case 4:coutWindows shake!endl;break;case 5:coutWalls crack;poorly built buidings are damaged!endl;break;case 6:coutChimneys tumble;ordinary buidings are damaged!endl;break;case 7:coutUnderground pipes break;well-buildings are damaged!endl;break;default:coutGround rises and falls in waves;most buildings are destroyed!endl;break;elsecouterror input! 0 ; i /= 2, j+ )cout i j ;解答:输出:10152231413. Modify program P4F to calculate the average along with the total of the numbers entered.解答:#includeusing namespace std;int main()double a,b;double result;char opt;int num=0;double sum=0;double ave=0.0;while(1)coutplease input like :1 i 3 or 1 d 3 aoptb;num+=2;sum+=(a+b);if(opt=i|opt=I)result=a+b;else if(opt=d|opt=D)result=a-b;else coutinput error!endl;ave=sum/num;coutthe result is resultendl;coutthe average along with the total of the numbers entered is aveendl;return 0;14. Rewrite the following using a for i = 0, total = 0 ;while ( i n ;total += n ;i+ ;解答:#includeint main()int i=0,total=0;int n;loop:cinn; total+=n; i+; if(i10) goto loop;couttotal;return 0;15. What is displayed when the following program is run and the number 1234 is entered?int num ;cout num ;docout num % 10 ;num /= 10 ;while ( num != 0 ) ;解答:当输入为1234时,输出为432116. The following program segment is intended to compute 0.1 + 0.2 + 0.3 . + 99.8 + 99.9. It contains a flaw. What is it, and how would you correct it?float sum = 0.0 ;float i = 0.1 ;while ( i != 100.0 )sum += i ;i += 0.1 ;解答:#includeint main()double sum=0.0;double i=0.1;int j=1;while(j=999)sum+=i;i+=0.1;j+;coutsum;return 0;17. What is the output from the following?(a)for (int i = 0 ; i 5 ; i+ )for ( int j = i ; j 5 ; j+ )cout i j endl ;(b)for ( int i = 0 ; i 5 ; i+ )for ( int j = 0 ; j 5-i ; j+ )cout i j endl ;解答:(a)输出为:00,01,02,03,04,11,12,13,14,22,23,24,33,34,44(b)输出为:00,01,02,03,04,10,11,12,13,20,21,22,30,31,4018. Write a for loop to(a) display the numbers 0, 5, 10, 15, ., 100(b) display the numbers 1, 2, 4, 8, 16, ., 1024.解答:(a)#includeint main()for(int i=0;i=100;i+=5)couti,;return 0;(b)#includeint main()int sum=1;for(int i=1;i=11;i+)coutsum,; sum*=2;return 0;19. Write a program to find the sum of all the odd integers in the range 1 to 99.解答:#include#includeint main()int sum=0;for(int i=1;i=99;i+=2)sum+=i;coutsum;return 0;20. Write a program that outputs all the numbers from 5 and 50 that are divisible by 3 or 5.解答:#include#includeint main()for(int i=5;i=50;i+)if(i%3=0|i%5=0)coutiendl;return 0;21. Write a program to display all the hour and
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025北京大学深圳研究生院教务处国际事务主管岗位招聘1人(广东)笔试考试参考试题及答案解析
- 2025重庆市环卫集团有限公司招聘27人(11月)笔试考试备考试题及答案解析
- 2025中煤内蒙古能源有限公司招聘43人考试笔试备考试题及答案解析
- 2025中建一局西北分局联络员招聘1人考试笔试备考题库及答案解析
- 2026江苏省招标中心有限公司校园招聘考试笔试参考题库附答案解析
- 2025四川长虹电子控股集团有限公司招聘项目管理高级经理岗位1人笔试考试参考试题及答案解析
- 2025年甘肃省嘉峪关市疾病预防控制中心(市卫生监督所)检验专业技术人员招聘补充笔试考试参考题库及答案解析
- 2025广东云浮市新兴县招聘教育人才7人(华南农业大学专场)考试笔试模拟试题及答案解析
- 2026上海市奉贤区卫生健康系统第一轮部分事业单位招聘事业单位医技人员82人考试笔试备考题库及答案解析
- 2025年安徽中医药大学第一附属医院高层次人才招聘45人笔试考试备考题库及答案解析
- 创伤急救模拟教学中的重症创伤模拟教学优化
- 加油站冬季安全培训课件
- (一诊)泸州市高2023级(2026届)高三第一次教学质量诊断性考试历史试题(含答案)
- 2026-2031中国国债市场竞争现状研究报告
- 工商企业管理毕业设计
- 新疆团员考试试卷及答案
- 2025年运输经理招聘面试参考题库及答案
- 个人述职述能报告范例
- 用友U8-ERP系统岗位操作规范手册
- 2025专职消防员聘用合同
- 【诊断学】205个必背名词解释
评论
0/150
提交评论