您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 数据结构与程序设计C++描述(Kruse著)高等教育出版社_课后答案
ProgrammingPrinciples11.2THEGAMEOFLIFEExercises1.2DeterminebyhandcalculationwhatwillhappentoeachoftheconfigurationsshowninFigure1.1overthecourseoffivegenerations.[Suggestion:SetuptheLifeconfigurationonacheckerboard.Useonecolorofcheckersforlivingcellsinthecurrentgenerationandasecondcolortomarkthosethatwillbebornordieinthenextgeneration.]Answer(a)Figureremainsstable.(b)(c)(d)Figureisstable.12Chapter1_ProgrammingPrinciples(e)(f)Figurerepeatsitself.(g)(h)(i)Figurerepeatsitself.(j)(k)(l)Figurerepeatsitself.Section1.3_ProgrammingStyle31.3PROGRAMMINGSTYLEExercises1.3E1.Whatclasseswouldyoudefineinimplementingthefollowingprojects?Whatmethodswouldyourclassespossess?(a)Aprogramtostoretelephonenumbers.AnswerTheprogramcoulduseclassescalledPhone_bookandPerson.ThemethodsforaPhone_bookobjectwouldincludelook_up_name,add_person,remove_person.ThemethodsforaPersonobjectwouldincludeLook_up_number.Additionalmethodstoinitializeandprintobjectsofbothclasseswouldalsobeuseful.(b)AprogramtoplayMonopoly.AnswerTheprogramcoulduseclassescalledGame_board,Property,Bank,Player,andDice.Inadditiontoinitializationandprintingmethodsforallclasses,thefollowingmethodswouldbeuseful.TheclassGame_boardneedsmethodsnext_cardandoperate_jail.TheclassPropertyneedsmethodschange_owner,look_up_owner,rent,build,mortgage,andunmortgage.TheclassBankneedsmethodspayandcollect.TheclassPlayerneedsmethodsroll_dice,move_location,buy_propertyandpay_rent.TheclassDiceneedsamethodroll.(c)Aprogramtoplaytic-tac-toe.AnswerTheprogramcoulduseclassescalledGame_boardandSquare.Theclassesneedinitializationandprintingmethods.TheclassGame_boardwouldalsoneedmethodsmake_moveandis_game_over.TheclassSquarewouldneedmethodsis_occupied,occupied_by,andoccupy.(d)Aprogramtomodelthebuildupofqueuesofcarswaitingatabusyintersectionwithatrafficlight.AnswerTheprogramcoulduseclassesCar,Traffic_light,andQueue.Theclasseswouldallneedinitializationandprintingmethods.TheclassTraffic_lightwouldneedadditionalmethodschange_statusandstatus.TheclassQueuewouldneedadditionalmethodsadd_carandremove_car.E2.Rewritethefollowingclassdefinition,whichissupposedtomodeladeckofplayingcards,sothatitconformstoourprinciplesofstyle.classa{//adeckofcardsintX;thingY1[52];/*Xisthelocationofthetopcardinthedeck.Y1liststhecards.*/public:a();voidShuffle();//Shufflerandomlyarrangesthecards.thingd();//dealsthetopcardoffthedeck};AnswerclassCard_deck{Carddeck[52];inttop_card;public:Card_deck();voidShuffle();Carddeal();};4Chapter1_ProgrammingPrinciplesE3.Giventhedeclarationsinta[n][n],i,j;wherenisaconstant,determinewhatthefollowingstatementdoes,andrewritethestatementtoaccomplishthesameeffectinalesstrickyway.for(i=0;in;i..)for(j=0;jn;j..)a[i][j]=((i.1)/(j.1))*((j.1)/(i.1));AnswerThisstatementinitializesthearrayawithall0’sexceptfor1’sdownthemaindiagonal.Alesstrickywaytoaccomplishthisinitializationis:for(i=0;in;i..)for(j=0;jn;j..)if(i==j)a[i][j]=1;elsea[i][j]=0;E4.Rewritethefollowingfunctionsothatitaccomplishesthesameresultinalesstrickyway.voiddoes_something(int&first,int&second){first=second−first;second=second−first;first=second.first;}AnswerThefunctioninterchangesthevaluesofitsparameters:voidswap(int&first,int&second)/*Pre:Theintegersfirstandsecondhavebeeninitialized.Post:Thevaluesoffirstandsecondhavebeenswitched.*/{inttemp=first;first=second;second=temp;}E5.Determinewhateachofthefollowingfunctionsdoes.Rewriteeachfunctionwithmeaningfulvariablenames,withbetterformat,andwithoutunnecessaryvariablesandstatements.(a)intcalculate(intapple,intorange){intpeach,lemon;peach=0;lemon=0;if(appleorange)peach=orange;elseif(orange=apple)peach=apple;else{peach=17;lemon=19;}return(peach);}AnswerThefunctioncalculatereturnsthelargerofitstwoparameters.intlarger(inta,intb)/*Pre:Theintegersaandbhavebeeninitialized.Post:Thevalueofthelargerofaandbisreturned.*/{if(ab)returnb;returna;}Section1.3_ProgrammingStyle5(b)Forthispartassumethedeclarationtypedeffloatvector[max];floatfigure(vectorvector1){intloop1,loop4;floatloop2,loop3;loop1=0;loop2=vector1[loop1];loop3=0.0;loop4=loop1;for(loop4=0;loop4max;loop4..){loop1=loop1.1;loop2=vector1[loop1−1];loop3=loop2.loop3;}loop1=loop1−1;loop2=loop1.1;return(loop2=loop3/loop2);}AnswerThefunctionfigureobtainsthemeanofanarrayoffloatingpointnumbers.floatmean(vectorv)/*Pre:Thevectorvcontainsmaxfloatingpointvalues.Post:Themeanofthevaluesinvisreturned.*/{floattotal=0.0;for(inti=0;imax;i..)total+=v[i];returntotal/((float)max);}(c)intquestion(int&a17,int&stuff){intanother,yetanother,stillonemore;another=yetanother;stillonemore=a17;yetanother=stuff;another=stillonemore;a17=yetanother;stillonemore=yetanother;stuff=another;another=yetanother;yetanother=stuff;}AnswerThefunctionquestioninterchangesthevaluesofitsparameters.voidswap(int&first,int&second)/*Pre:Theintegersfirstandsecondhavebeeninitialized.Post:Thevaluesoffirstandsecondhavebeenswitched.*/{inttemp=first;first=second;second=temp;}(d)intmystery(intapple,intorange,intpeach){if(appleorange)if(applepeach)if(peachorange)return(peach);elseif(appleorange)return(apple);elser
本文标题:数据结构与程序设计C++描述(Kruse著)高等教育出版社_课后答案
链接地址:https://www.777doc.com/doc-3871861 .html