C Primer Plus第6版编程练习答案已.docx

上传人:b****7 文档编号:16537005 上传时间:2023-07-14 格式:DOCX 页数:104 大小:46.98KB
下载 相关 举报
C Primer Plus第6版编程练习答案已.docx_第1页
第1页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第2页
第2页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第3页
第3页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第4页
第4页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第5页
第5页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第6页
第6页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第7页
第7页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第8页
第8页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第9页
第9页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第10页
第10页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第11页
第11页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第12页
第12页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第13页
第13页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第14页
第14页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第15页
第15页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第16页
第16页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第17页
第17页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第18页
第18页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第19页
第19页 / 共104页
C Primer Plus第6版编程练习答案已.docx_第20页
第20页 / 共104页
亲,该文档总共104页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C Primer Plus第6版编程练习答案已.docx

《C Primer Plus第6版编程练习答案已.docx》由会员分享,可在线阅读,更多相关《C Primer Plus第6版编程练习答案已.docx(104页珍藏版)》请在冰点文库上搜索。

C Primer Plus第6版编程练习答案已.docx

CPrimerPlus第6版编程练习答案已

Chapter2ProgrammingExercises

PE2-‐1

/*ProgrammingExercise2-1*/

#includeintmain(void)

{printf("GustavMahler\n");printf("Gustav\nMahler\n");printf("Gustav");printf("Mahler\n");return0;

}

PE2-‐3

/*ProgrammingExercise2-3*/

#includeintmain(void)

{intageyears;/*ageinyears*/intagedays;/*ageindays*/

/*largeagesmayrequirethelongtype*/ageyears=101;agedays=365*ageyears;

printf("Anageof%dyearsis%ddays.\n",ageyears,agedays);return0;

}

PE2-‐4

/*ProgrammingExercise2-4*/

#includevoidjolly(void);voiddeny(void);intmain(void)

{jolly();jolly();jolly();deny();return0;}

voidjolly(void)

{

printf("Forhe'sajollygoodfellow!

\n");

}

voiddeny(void)

{

printf("Whichnobodycandeny!

\n");

}

PE2-‐6

/*ProgrammingExercise2-6*/

#includeintmain(void)

{inttoes;toes=10;

printf("toes=%d\n",toes);

printf("Twicetoes=%d\n",2*toes);printf("toessquared=%d\n",toes*toes);return0;

}

/*orcreatetwomorevariables,setthemto2*toesandtoes*toes*/

PE2-‐8

/*ProgrammingExercise2-8*/

#includevoidone_three(void);voidtwo(void);intmain(void)

{

printf("startingnow:

\n");one_three();printf("done!

\n");return0;

}

voidone_three(void)

{

printf("one\n");two();

printf("three\n");

}

voidtwo(void)

{

printf("two\n");

}

Chapter3ProgrammingExercises

PE3-‐2

/*ProgrammingExercise3-2*/

#includeintmain(void)

{

intascii;

printf("EnteranASCIIcode:

");scanf("%d",&ascii);

printf("%distheASCIIcodefor%c.\n",ascii,ascii);return0;

}

PE3-‐4

/*ProgrammingExercise3-4*/

#includeintmain(void)

{floatnum;

printf("Enterafloating-pointvalue:

");scanf("%f",&num);

printf("fixed-pointnotation:

%f\n",num);printf("exponentialnotation:

%e\n",num);printf("pnotation:

%a\n",num);return0;

}

PE3-‐6

/*ProgrammingExercise3-6*/

#includeintmain(void)

{

floatmass_mol=3.0e-23;/*massofwatermoleculeingrams*/floatmass_qt=950;/*massofquartofwateringrams*/floatquarts;floatmolecules;

printf("Enterthenumberofquartsofwater:

");scanf("%f",&quarts);

molecules=quarts*mass_qt/mass_mol;

printf("%fquartsofwatercontain%emolecules.\n",quarts,molecules);return0;

}

Chapter4ProgrammingExercises

PE4-‐1

/*ProgrammingExercise4-1*/

#includeintmain(void)

{charfname[40];charlname[40];

printf("Enteryourfirstname:

");scanf("%s",fname);printf("Enteryourlastname:

");scanf("%s",lname);printf("%s,%s\n",lname,fname);return0;

}

PE4-‐4

/*ProgrammingExercise4-4*/

#includeintmain(void)

{floatheight;charname[40];

printf("Enteryourheightininches:

");scanf("%f",&height);printf("Enteryourname:

");scanf("%s",name);

printf("%s,youare%.3ffeettall\n",name,height/12.0);

return0;

}

PE4-‐7

/*ProgrammingExercise4-7*/

#include#includeintmain(void)

{floatot_f=1.0/3.0;doubleot_d=1.0/3.0;printf("floatvalues:

");

printf("%.4f%.12f%.16f\n",ot_f,ot_f,ot_f);printf("doublevalues:

");

printf("%.4f%.12f%.16f\n",ot_d,ot_d,ot_d);printf("FLT_DIG:

%d\n",FLT_DIG);printf("DBL_DIG:

%d\n",DBL_DIG);return0;

}

Chapter5ProgrammingExercises

PE5-‐1

/*ProgrammingExercise5-1*/

#includeintmain(void)

{constintminperhour=60;intminutes,hours,mins;

printf("Enterthenumberofminutestoconvert:

");scanf("%d",&minutes);while(minutes>0)

{hours=minutes/minperhour;mins=minutes%minperhour;

printf("%dminutes=%dhours,%dminutes\n",minutes,hours,mins);printf("Enternextminutesvalue(0toquit):

");scanf("%d",&minutes);

}

printf("Bye\n");

return0;

}

PE5-‐3

/*ProgrammingExercise5-3*/

#includeintmain(void)

{constintdaysperweek=7;intdays,weeks,day_rem;

printf("Enterthenumberofdays:

");scanf("%d",&days);while(days>0)

{weeks=days/daysperweek;day_rem=days%daysperweek;

printf("%ddaysare%dweeksand%ddays.\n",days,weeks,day_rem);

printf("Enterthenumberofdays(0orlesstoend):

");scanf("%d",&days);

}

printf("Done!

\n");return0;

}

PE5-‐5

/*ProgrammingExercise5-5*/#include

intmain(void)/*findssumoffirstnintegers*/

{

intcount,sum;intn;

printf("Entertheupperlimit:

");scanf("%d",&n);count=0;sum=0;while(count++

}

PE5-‐7

/*ProgrammingExercise5-7*/#includevoidshowCube(doublex);

intmain(void)/*findscubeofenterednumber*/

{doubleval;

printf("Enterafloating-pointvalue:

");scanf("%lf",&val);showCube(val);return0;}

voidshowCube(doublex)

{

printf("Thecubeof%eis%e.\n",x,x*x*x);

}

Chapter6ProgrammingExercises

PE6-‐1

/*pe6-1.c*/

/*thisimplementationassumesthecharactercodes*/

/*aresequential,astheyareinASCII.*/

#include#defineSIZE26intmain(void){charlcase[SIZE];inti;for(i=0;i

}

PE6-‐3

/*pe6-3.c*/

/*thisimplementationassumesthecharactercodes*/

/*aresequential,astheyareinASCII.*/

#includeintmain(void)

{charlet='F';charstart;charend;

for(end=let;end>='A';end--)

{

for(start=let;start>=end;start--)printf("%c",start);printf("\n");

}return0;

}

PE6-‐6

/*pe6-6.c*/#includeintmain(void)

{intlower,upper,index;intsquare,cube;

printf("Enterstartinginteger:

");scanf("%d",&lower);printf("Enterendinginteger:

");scanf("%d",&upper);

printf("%5s%10s%15s\n","num","square","cube");for(index=lower;index<=upper;index++)

{square=index*index;cube=index*square;

printf("%5d%10d%15d\n",index,square,cube);

}return0;

}

PE6-‐8

/*pe6-8.c*/#includeintmain(void)

{doublen,m;doubleres;

printf("Enterapairofnumbers:

");

while(scanf("%lf%lf",&n,&m)==2)

{

res=(n-m)/(n*m);

printf("(%.3g-%.3g)/(%.3g*%.3g)=%.5g\n",n,m,n,m,res);printf("Enternextpair(non-numerictoquit):

");

}

return0;

}

PE6-‐11

/*pe6-11.c*/

#include#defineSIZE8intmain(void)

{intvals[SIZE];inti;

printf("Pleaseenter%dintegers.\n",SIZE);for(i=0;i

printf("Here,inreverseorder,arethevaluesyouentered:

\n");for(i=SIZE-1;i>=0;i--)printf("%d",vals[i]);printf("\n");return0;

}

PE6-‐13

/*pe6-13.c*/

/*Thisversionstartswiththe0power*/

#include#defineSIZE8intmain(void)

{

inttwopows[SIZE];inti;

intvalue=1;/*2tothe0*/

for(i=0;i

{twopows[i]=value;value*=2;

}i=0;do{

printf("%d",twopows[i]);i++;}while(i

}

PE6-‐14

/*pe-14.c*/

/*ProgrammingExercise6-14*/

#include#defineSIZE8intmain(void)

{doublearr[SIZE];doublearr_cumul[SIZE];inti;

printf("Enter%dnumbers:

\n",SIZE);

for(i=0;i

{

printf("value#%d:

",i+1);scanf("%lf",&arr[i]);/*orscanf("%lf",arr+i);*/

}

arr_cumul[0]=arr[0];/*setfirstelement*/for(i=1;i

arr_cumul[i]=arr_cumul[i-1]+arr[i];

for(i=0;i

}

PE6-‐16

/*pe6-16.c*/

#include

#defineRATE_SIMP0.10

#defineRATE_COMP0.05#defineINIT_AMT100.0intmain(void)

{

doubledaphne=INIT_AMT;doubledeidre=INIT_AMT;intyears=0;

while(deidre<=daphne)

{daphne+=RATE_SIMP*INIT_AMT;deidre+=RATE_COMP*deidre;

++years;}

printf("Investmentvaluesafter%dyears:

\n",years);printf("Daphne:

$%.2f\n",daphne);printf("Deidre:

$%.2f\n",deidre);return0;

}

Chapter7ProgrammingExercises

PE7-‐1

/*ProgrammingExercise7-1*/#includeintmain(void)

{charch;intsp_ct=0;intnl_ct=0;intother=0;while((ch=getchar())!

='#')

{

if(ch=='')sp_ct++;elseif(ch=='\n')nl_ct++;elseother++;

}

printf("spaces:

%d,newlines:

%d,others:

%d\n",sp_ct,nl_ct,other);

return0;

}

PE7-‐3

/*ProgrammingExercise7-3*/

#includeintmain(void)

{intn;doublesumeven=0.0;intct_even=0;doublesumodd=0.0;intct_odd=0;

while(scanf("%d",&n)==1&&n!

=0)

{

if(n%2==0)

{

sumeven+=n;

++ct_even;

}

else//n%2iseither1or-1

{

sumodd+=n;

++ct_odd;

}}

printf("Numberofevens:

%d",ct_even);if(ct_even>0)

printf("average:

%g",sumeven/ct_even);putchar('\n');

printf("Numberofodds:

%d",ct_odd);if(ct_odd>0)

printf("average:

%g",sumodd/ct_odd);putchar('\n');printf("\ndone\n");

return0;

}

PE7-‐5

/*ProgrammingExercise7-5*/

#includeintmain(void)

{charch;intct1=0;intct2=0;while((ch=getchar())!

='#')

{

switch(ch){

case'.':

putchar('!

');++ct1;break;case'!

':

putchar('!

');putchar('!

');++ct2;break;default:

putchar(ch);

}}

printf("%dreplacement(s)of.with!

\n",ct1);printf("%dreplacement(s)of!

with!

!

\n",ct2);

return0;

}

PE7-‐7

//ProgrammingExercise7-7

#include

#defineBASEPAY10//$10perhour

#defineBASEHRS40//hoursatbasepay

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2