C语言程序设计现代方法第二版习题答案.docx
- 文档编号:9652686
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:107
- 大小:41.96KB
C语言程序设计现代方法第二版习题答案.docx
《C语言程序设计现代方法第二版习题答案.docx》由会员分享,可在线阅读,更多相关《C语言程序设计现代方法第二版习题答案.docx(107页珍藏版)》请在冰豆网上搜索。
C语言程序设计现代方法第二版习题答案
Chapter2
AnswerstoSelectedExercises
2.[was#2](a)Theprogramcontainsonedirective(#include)andfourstatements(threecallsofprintfandonereturn).
(b)
Parkinson'sLaw:
Workexpandssoastofillthetime
availableforitscompletion.
3.[was#4]
#include
intmain(void)
{
intheight=8,length=12,width=10,volume;
volume=height*length*width;
printf("Dimensions:
%dx%dx%d\n",length,width,height);
printf("Volume(cubicinches):
%d\n",volume);
printf("Dimensionalweight(pounds):
%d\n",(volume+165)/166);
return0;
}
4.[was#6]Here'sonepossibleprogram:
#include
intmain(void)
{
inti,j,k;
floatx,y,z;
printf("Valueofi:
%d\n",i);
printf("Valueofj:
%d\n",j);
printf("Valueofk:
%d\n",k);
printf("Valueofx:
%g\n",x);
printf("Valueofy:
%g\n",y);
printf("Valueofz:
%g\n",z);
return0;
}
WhencompiledusingGCCandthenexecuted,thisprogramproducedthefollowingoutput:
Valueofi:
5618848
Valueofj:
0
Valueofk:
6844404
Valueofx:
3.98979e-34
Valueofy:
9.59105e-39
Valueofz:
9.59105e-39
Thevaluesprinteddependonmanyfactors,sothechancethatyou'llgetexactlythesenumbersissmall.
5.[was#10](a)isnotlegalbecause100_bottlesbeginswithadigit.
8.[was#12]Thereare14tokens:
a,=,(,3,*,q,-,p,*,p,),/,3,and;.
AnswerstoSelectedProgrammingProjects
4.[was#8;modified]
#include
intmain(void)
{
floatoriginal_amount,amount_with_tax;
printf("Enteranamount:
");
scanf("%f",&original_amount);
amount_with_tax=original_amount*1.05f;
printf("Withtaxadded:
$%.2f\n",amount_with_tax);
return0;
}
Theamount_with_taxvariableisunnecessary.Ifweremoveit,theprogramisslightlyshorter:
#include
intmain(void)
{
floatoriginal_amount;
printf("Enteranamount:
");
scanf("%f",&original_amount);
printf("Withtaxadded:
$%.2f\n",original_amount*1.05f);
return0;
}
Chapter3
AnswerstoSelectedExercises
2.[was#2]
(a)printf("%-8.1e",x);
(b)printf("%10.6e",x);
(c)printf("%-8.3f",x);
(d)printf("%6.0f",x);
5.[was#8]Thevaluesofx,i,andywillbe12.3,45,and.6,respectively.
AnswerstoSelectedProgrammingProjects
1.[was#4;modified]
#include
intmain(void)
{
intmonth,day,year;
printf("Enteradate(mm/dd/yyyy):
");
scanf("%d/%d/%d",&month,&day,&year);
printf("Youenteredthedate%d%.2d%.2d\n",year,month,day);
return0;
}
3.[was#6;modified]
#include
intmain(void)
{
intprefix,group,publisher,item,check_digit;
printf("EnterISBN:
");
scanf("%d-%d-%d-%d-%d",&prefix,&group,&publisher,&item,&check_digit);
printf("GS1prefix:
%d\n",prefix);
printf("Groupidentifier:
%d\n",group);
printf("Publishercode:
%d\n",publisher);
printf("Itemnumber:
%d\n",item);
printf("Checkdigit:
%d\n",check_digit);
/*Thefiveprintfcallscanbecombinedasfollows:
printf("GS1prefix:
%d\nGroupidentifier:
%d\nPublishercode:
%d\nItemnumber:
%d\nCheckdigit:
%d\n",
prefix,group,publisher,item,check_digit);
*/
return0;
}
Chapter4
AnswerstoSelectedExercises
2.[was#2]NotinC89.Supposethatiis9andjis7.Thevalueof(-i)/jcouldbeeither–1or–2,dependingontheimplementation.Ontheotherhand,thevalueof-(i/j)isalways–1,regardlessoftheimplementation.InC99,ontheotherhand,thevalueof(-i)/jmustbeequaltothevalueof-(i/j).
9.[was#6]
(a)638
(b)321
(c)2-13
(d)000
13.[was#8]Theexpression++iisequivalentto(i+=1).Thevalueofbothexpressionsisiaftertheincrementhasbeenperformed.
AnswerstoSelectedProgrammingProjects
2.[was#4]
#include
intmain(void)
{
intn;
printf("Enterathree-digitnumber:
");
scanf("%d",&n);
printf("Thereversalis:
%d%d%d\n",n%10,(n/10)%10,n/100);
return0;
}
Chapter5
AnswerstoSelectedExercises
2.[was#2]
(a)1
(b)1
(c)1
(d)1
4.[was#4](i>j)-(i 6.[was#12]Yes,thestatementislegal.Whennisequalto5,itdoesnothing,since5isnotequalto–9. 10.[was#16]Theoutputis onetwo sincetherearenobreakstatementsafterthecases. AnswerstoSelectedProgrammingProjects 2.[was#6] #include intmain(void) { inthours,minutes; printf("Entera24-hourtime: "); scanf("%d: %d",&hours,&minutes); printf("Equivalent12-hourtime: "); if(hours==0) printf("12: %.2dAM\n",minutes); elseif(hours<12) printf("%d: %.2dAM\n",hours,minutes); elseif(hours==12) printf("%d: %.2dPM\n",hours,minutes); else printf("%d: %.2dPM\n",hours-12,minutes); return0; } 4.[was#8;modified] #include intmain(void) { intspeed; printf("Enterawindspeedinknots: "); scanf("%d",&speed); if(speed<1) printf("Calm\n"); elseif(speed<=3) printf("Lightair\n"); elseif(speed<=27) printf("Breeze\n"); elseif(speed<=47) printf("Gale\n"); elseif(speed<=63) printf("Storm\n"); else printf("Hurricane\n"); return0; } 6.[was#10] #include intmain(void) { intcheck_digit,d,i1,i2,i3,i4,i5,j1,j2,j3,j4,j5, first_sum,second_sum,total; printf("Enterthefirst(single)digit: "); scanf("%1d",&d); printf("Enterfirstgroupoffivedigits: "); scanf("%1d%1d%1d%1d%1d",&i1,&i2,&i3,&i4,&i5); printf("Entersecondgroupoffivedigits: "); scanf("%1d%1d%1d%1d%1d",&j1,&j2,&j3,&j4,&j5); printf("Enterthelast(single)digit: "); scanf("%1d",&check_digit); first_sum=d+i2+i4+j1+j3+j5; second_sum=i1+i3+i5+j2+j4; total=3*first_sum+second_sum; if(check_digit==9-((total-1)%10)) printf("VALID\n"); else printf("NOTVALID\n"); return0; } 10.[was#14] #include intmain(void) { intgrade; printf("Enternumericalgrade: "); scanf("%d",&grade); if(grade<0||grade>100){ printf("Illegalgrade\n"); return0; } switch(grade/10){ case10: case9: printf("Lettergrade: A\n"); break; case8: printf("Lettergrade: B\n"); break; case7: printf("Lettergrade: C\n"); break; case6: printf("Lettergrade: D\n"); break; case5: case4: case3: case2: case1: case0: printf("Lettergrade: F\n"); break; } return0; } Chapter6 AnswerstoSelectedExercises 4.[was#10](c)isnotequivalentto(a)and(b),becauseiisincrementedbeforetheloopbodyisexecuted. 10.[was#12]Considerthefollowingwhileloop: while(…){ … continue; … } Theequivalentcodeusinggotowouldhavethefollowingappearance: while(…){ … gotoloop_end; … loop_end: ;/*nullstatement*/ } 12.[was#14] for(d=2;d*d<=n;d++) if(n%d==0) break; Theifstatementthatfollowstheloopwillneedtobemodifiedaswell: if(d*d<=n) printf("%disdivisibleby%d\n",n,d); else printf("%disprime\n",n); 14.[was#16]Theproblemisthesemicolonattheendofthefirstline.Ifweremoveit,thestatementisnowcorrect: if(n%2==0) printf("niseven\n"); AnswerstoSelectedProgrammingProjects 2.[was#2] #include intmain(void) { intm,n,remainder; printf("Entertwointegers: "); scanf("%d%d",&m,&n); while(n! =0){ remainder=m%n; m=n; n=remainder; } printf("Greatestcommondivisor: %d\n",m); return0; } 4.[was#4] #include intmain(void) { floatcommission,value; printf("Entervalueoftrade: "); scanf("%f",&value); while(value! =0.0f){ if(value<2500.00f) commission=30.00f+.017f*value; elseif(value<6250.00f) commission=56.00f+.0066f*value; elseif(value<20000.00f) commission=76.00f+.0034f*value; elseif(value<50000.00f) commission=100.00f+.0022f*value; elseif(value<500000.00f) commission=155.00f+.0011f*value; else commission=255.00f+.0009f*value; if(commission<39.00f) commission=39.00f; printf("Commission: $%.2f\n\n",commission); printf("Entervalueoftrade: "); scanf("%f",&value); } return0; } 6.[was#6] #include intmain(void) { inti,n; printf("Enterlimitonmaximumsquare: "); scanf("%d",&n); for(i=2;i*i<=n;i+=2) printf("%d\n",i*i); return0; } 8.[was#8] #include intmain(void) { inti,n,start_day; printf("Enternumberofdaysinmonth: "); scanf("%d",&n); printf("Enterstartingdayoftheweek(1=Sun,7=Sat): "); scanf("%d",&start_day); /*printanyleading"blankdates"*/ for(i=1;i printf(""); /*nowprintthecalendar*/ for(i=1;i<=n;i++){ printf("%3d",i); if((start_day+i-1)%7==0) printf("\n"); } return0; } Chapter7 AnswerstoSelectedExercises 3.[was#4](b)isnotlegal. 4.[was#6](d)isillegal,sinceprintfrequiresastring,notacharacter,asitsfirstargument. 10.[was#14]unsignedint,becausethe(int)castappliesonlytoj,notj*k. 12.[was#16]Thevalueofiisconvertedtofloatandaddedtof,thentheresultisconvertedtodoubleandstoredind. 14.[was#18]No.Convertingftointwillfailifthevaluestoredinfexceedsthelargestvalueoftypeint. AnswerstoSelectedProgrammingProjects 1.[was#2]shortintvaluesareusuallystoredin16bits,causingfailureat182.intandlongintvaluesareusuallystoredin32bits,withfailureoccurringat46341. 2.[was#8] #include intmain(void) { inti,n; charch; printf("Thisprogramprintsatableofsquares.\n"); printf("Enternumberofentriesintable: "); scanf("%d",&n); ch=getchar(); /*disposeofnew-linecharacterfollowingnumber
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言程序设计 现代 方法 第二 习题 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)