资源描述:
《C语言程序设计_现代方法_答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、AnswerstoSelectedExercisesandProgrammingProjectsThefollowinglinksleadtoanswerstoselectedexercisesandprogrammingprojectsfromthesecondeditionofCProgramming:AModernApproach.Exercisesandprojectsforwhichanswersareprovidedaremarkedwithaiconinthebook.Note:Theseexercisesandprojectswereorigina
2、llytheeven-numberedexercisesinthefirstedition.(Thefirsteditiondidnotdistinguishbetweenexercisesandprogrammingprojects.)Forthebenefitofreaderswhohavethefirstedition,theoriginalexercisenumberisgiveninsquarebrackets.Forexample,thenotation[was#4]indicatesthatthenumberoftheexercise(orprogr
3、ammingproject)was4inthefirstedition.Ifananswerisdifferentbecauseofsecond-editionchanges,theword"modified"willappearinsidethebrackets:[was#4;modified].Pleaseemailmewithcommentsanderrorreports.Chapter2AnswerstoSelectedExercises2.[was#2](a)Theprogramcontainsonedirective(#include)andfours
4、tatements(threecallsofprintfandonereturn).(b)Parkinson'sLaw:Workexpandssoastofillthetimeavailableforitscompletion.3.[was#4]#includeintmain(void){intheight=8,length=12,width=10,volume;volume=height*length*width;printf("Dimensions:%dx%dx%d",length,width,height);printf("Volume
5、(cubicinches):%d",volume);printf("Dimensionalweight(pounds):%d",(volume+165)/166);return0;}4.[was#6]Here'sonepossibleprogram:#includeintmain(void){inti,j,k;floatx,y,z;printf("Valueofi:%d",i);printf("Valueofj:%d",j);printf("Valueofk:%d",k);printf("Valueofx:%g",x);p
6、rintf("Valueofy:%g",y);printf("Valueofz:%g",z);return0;}WhencompiledusingGCCandthenexecuted,thisprogramproducedthefollowingoutput:Valueofi:5618848Valueofj:0Valueofk:6844404Valueofx:3.98979e-34Valueofy:9.59105e-39Valueofz:9.59105e-39Thevaluesprinteddependonmanyfactors,sothechanceth
7、atyou'llgetexactlythesenumbersissmall.5.[was#10](a)isnotlegalbecause100_bottlesbeginswithadigit.8.[was#12]Thereare14tokens:a,=,(,3,*,q,-,p,*,p,),/,3,and;.AnswerstoSelectedProgrammingProjects4.[was#8;modified]#includeintmain(void){floatoriginal_amount,amount_with_tax;printf("E
8、ntera