Chapter1:SolutionstoSampleExamQuestionsShortAnswerQuestions1.1.Useaprecondition/postconditioncontract.Forexample:doublenew_balance(doublebalance,doubleinterest_rate);//Precondition:Theparameterinterest_rateistheannualbank//accountinterestrate,forexample5.0for5%.Theparameter//balanceisanaccountbalancebeforeinterestisposted.//Postcondition:Thevaluereturnedbythefunctionisthenew//accountbalanceafterpostingoneyearofsimpleinterestat//therategivenbyinterest_rate.1.2.Apreconditionisastatementgivingtheconditionthatissupposedtobetruewhenafunctioniscalled.Apostconditionisastatementdescribingwhatwillbetruewhenafunctioncalliscompleted.1.3.Ifthepreconditionfails,thefunctioncanprintanerrormessageandhalttheprogram.1.4.Preconditionscannotalwaysbechecked.1.5.Ifyoucallafunctionandthepreconditionisfalse,thenthereisnotellingwhatmighthappen.Anythingcouldhappen,includingerasingtheentireharddrive.Ofcourse,thefunctionsthatwewritetrytoavoidsuchunpleasantbehavior,butthereisnoguarantee.1.6.voidexam(inti)//Precondition:iisnotequalto42.//Libraryfacilitiesused:assert.h{assert(i!=42);...1.7.lognistheapproximatenumberofdigitsinn(usingbase10logs).(Theexactformulaisfloor(logn)+1,usingbase10logsandthefloorfunctionwhichroundsarealnumberdowntothenextlowestinteger.)1.8.Evenaslowprocessorwillbeatafastprocessoriftheslowerprocessorusesafasteralgorithmandtheinputsizeislargeenough.1.9.ThefirstformulaisO(n).ThesecondandfourthformulasareO(n^2)(using^forexponentiation).ThethirdformulaisO(logn).1.10.SeetheMethod2analysisonpage17ofthetext.1.11.Mydebuggerletsmerunaprogramandstopitataspecifiedpointtoexaminethecurrentvaluesofvariables.Afterthis,Icanstepthroughtheprogramonelineatatime,continuingtokeepawatchonvariablevalues.OrIcancontinueexecutioninanormalway,specifyingalaterpointtostoptheprogram.MultipleChoiceQuestions:Preview Mode
This document has 26 pages. Sign in to access the full document!
