Tải bản đầy đủ - 0 (trang)
Appendix A. Answers to the Review Quesions

Appendix A. Answers to the Review Quesions

Tải bản đầy đủ - 0trang

AnswerstoReviewQuestionsforChapter1



A1:



Aperfectlyportableprogramisonewhosesourcecodecan,withoutmodification,

becompiledtoasuccessfulprogramonavarietyofdifferentcomputersystems.



Asourcecodefilecontainscodeaswritteninwhateverlanguagetheprogrammer

isusing.Anobjectcodefilecontainsmachinelanguagecode;itneednotbethe

A2:

codeforacompleteprogram.Anexecutablefilecontainsthecompletecode,in

machinelanguage,constitutinganexecutableprogram.



a. Definingprogramobjectives.

b. Designingtheprogram.

c. Codingtheprogram.

A3:



d. Compilingtheprogram.

e. Runningtheprogram.

f. Testinganddebuggingtheprogram.

g. Maintainingandmodifyingtheprogram.



A4:



Acompilertranslatessourcecode(forexample,codewritteninC)tothe

equivalentmachinelanguagecode,alsotermedobjectcode.



A5:



Thelinkercombinestranslatedsourcecodewithlibrarycodeandstart-upcodeto

produceanexecutableprogram.



AnswerstoReviewQuestionsforChapter2

A1: Theyarecalledfunctions.



Asyntaxerrorisaviolationoftherulesgoverninghowsentencesorprogramsare

puttogether.Here'sanexampleinEnglish:"MespeakEnglishgood."Here'san

exampleinC:



A2:



printf"Wherearetheparentheses?";.



Asemanticerrorisoneofmeaning.Here'sanexampleinEnglish:"Thissentence

isexcellentItalian."Here'saCexample:



A3:

thrice_n=3+n;



Line1:Beginthelinewitha#;spellthefilestdio.h;placethefilenamewithin

anglebrackets.

Line2:Use(),not{};endthecommentwith*/,not/*.

Line3:Use{,not(.

Line4:Completethestatementwithasemicolon.

Line5:Indianagotthisone(theblankline)right!

Line6:Use=,not:=forassignment.(Apparently,IndianaknowsalittlePascal.)

Use52,not56,weeksperyear.



Line7:Shouldbe



printf("Thereare%dweeksinayear.\n",s);



Line9:Thereisn'taline9,butthereshouldbe,anditshouldconsistoftheclosing

A4: brace,}.

Here'showthecodelooksafterthesechanges:



#include

intmain(void)/*thisprintsthenumberofweeksinayear*/

{

ints;



s=52;

printf("Thereare%dweeksinayear.\n",s);

return0;

}



a. BaaBaaBlackSheep.Haveyouanywool?

(Notethatthereisnospaceaftertheperiod.Youcouldhavehadaspaceby

using"Haveinsteadof"Have.)

b.



Begone!



Ocreatureoflard!



(Notethatthecursorisleftattheendofthesecondline.)

A5:



c.



What?

No/nBonzo?



(Notethattheslash[/]doesnothavethesameeffectasthebackslash[\];

itsimplyprintsasaslash.)

d. 2+2=4

(Notehoweach%disreplacedbythecorrespondingvariablevaluefromthe

list.Note,too,that+meansadditionandthatcalculationcanbedoneinside

aprintf()statement.)



A6:



intandchar(mainisafunctionname,functionisatechnicaltermfordescribing

C,and=isanoperator).



A7: printf("Therewere%dwordsand%dlines.\n",words,lines);



Afterline7,ais5andbis2.Afterline8,bothaandbare5.Afterline9,botha

A8: andbarestill5.(Notethatacan'tbe2becausebythetimeyousaya=b;,b

hasalreadybeenchangedto5.)



AnswerstoReviewQuestionsforChapter3

a. int,possiblyshortorunsignedorunsignedshort;populationisawhole

number.

A1:



b. float;it'sunlikelythecostwillbeanexactinteger.(Youcouldusedoublebut

don'treallyneedtheextraprecision.)

c. char.

d. int,possiblyunsigned.



Onereasonisthatlongmayaccommodatelargernumbersthanintonyoursystem;

A2: anotherreasonisthatifyoudoneedtohandlelargervalues,youimproveportabilityby

usingatypeguaranteedtobeatleast32bitsonallsystems.



Togetexactly32bits,youcoulduseint32_t,provideditwasdefinedforyoursystem.

A3: Togetthesmallesttypethatcouldstoreatleast32bits,useint_least32_t.Andtoget

thetypethatwouldprovidethefastestcomputationsfor32bits,chooseint_fast32_t



a. charconstant(butstoredastypeint)

b. intconstant

A4:



c. doubleconstant

d. unsignedintconstant,hexadecimalformat

e. doubleconstant



Line1:Shouldbe#include.

Line2:Shouldbeintmain(void).

Line3:Use{,not(.

Line4:Shouldbeacomma,notasemicolon,betweengandh.

Line5:Fine.

Line6(theblankline):Fine.



Line7:Thereshouldbeatleastonedigitbeforethee.Either1e21or1.0e21isokay,

althoughratherlarge.

Line8:Fine,atleastintermsofsyntax.

Line9:Use},not).

Missinglines:First,rateisneverassignedavalue.Second,thevariablehisneverused.

Also,theprogramneverinformsyouoftheresultsofitscalculation.Noneofthese

errorswillstoptheprogramfromrunning(althoughyoumightbegivenawarningabout

theunusedvariable),buttheydodetractfromitsalreadylimitedusefulness.Also,there

shouldbeareturnstatementattheend.

Hereisonepossiblecorrectversion:



A5:

#include

intmain(void)

{

floatg,h;

floattax,rate;



rate=0.08;

g=1.0e5;

tax=rate*g;

h=g+tax;

printf("Youowe$%fplus$%fintaxesforatotalof$%f.\n",g,tax,h);

return0;

}







Constant



Type



Specifier



a.



12



int



%d



b.



0X3



unsignedint



%#X



c.



'C'



char(reallyint)



%c



2.34E07



double



%e



e.



'\040'



char(reallyint)



%c



f.



7.0



double



%f



g.



6L



long



%ld



h.



6.0f



float



%f







Constant



Type



Specifier



a.



012



unsignedint



%#o



b.



2.9e05L



longdouble



%Le



c.



's'



char(reallyint)



%c



d.



100000



long



%ld



e.



'\n'



char(reallyint)



%c



f.



20.0f



float



%f



g.



0x44



unsignedint



%x



A6: d.



A7:



A8: printf("Theoddsagainstthe%dwere%ldto1.\n",imate,shot);

printf("Ascoreof%fisnotan%cgrade.\n",log,grade);



ch='\r';

A9: ch=13;

ch='\015'

ch='\xd'



Line0:Shouldhave#include.

Line1:Use/*and*/orelse//

Line3:intcows,legs;

Line4:count?\n");

Line5:%d,not%c;replacelegswith&legs

Line7:%d,not%f

Also,addareturnstatement.

Here'sonecorrectversion:



A10: #include

intmain(void)/*thisprogramisperfect*/



{

intcows,legs;

printf("Howmanycowlegsdidyoucount?\n");

scanf("%d",&legs);

cows=legs/4;

printf("Thatimpliesthereare%dcows.\n",cows);

return0;

}



a. Anewlinecharacter

b. Abackslashcharacter

A11:

c. Adoublequotationmark

d. Atabcharacter



AnswerstoReviewQuestionsforChapter4

Theprogrammalfunctions.Thefirstscanf()statementreadsjustyourfirstname,

leavingyourlastnameuntouchedbutstillstoredintheinput"buffer."(Thisbufferis

justatemporarystorageareausedtostoretheinput.)Whenthenextscanf()

statementcomesalonglookingforyourweight,itpicksupwherethelastreading

A1:

attemptended,andittriestoreadyourlastnameasyourweight.Thisfrustrates

scanf().Ontheotherhand,ifyourespondtothenamerequestwithsomethingsuch

asLasha144,ituses144asyourweight,eventhoughyoutypeditbeforeyour

weightwasrequested.



a.



Hesoldthepaintingfor$234.50.



b.



Hi!



(Note:Thefirstcharacterisacharacterconstant,thesecondisadecimal

integerconvertedtoacharacter,andthethirdisanASCIIrepresentation,in

octal,ofacharacterconstant.)



A2:

c.



HisHamletwasfunnywithoutbeingvulgar.

has42characters.



d.



Is1.20e+003thesameas1201.00?



Use\",asinthefollowing:



A3:

printf("\"%s\"\nhas%dcharacters.\n",Q,strlen(Q));



Hereisacorrectedversion:



#include/*don'tforgetthis*/

#defineB"booboo"/*add#,quotes*/

#defineX10/*add#*/

intmain(void)/*insteadofmain(int)*/

{

intage;

intxp;/*declareallvariables*/

A4: charname[40];/*makeintoanarray*/



printf("Pleaseenteryourfirstname.\n");/*\nforreadability*/

scanf("%s",name);

printf("Allright,%s,what'syourage?\n",name);/*%sforstring*/



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Appendix A. Answers to the Review Quesions

Tải bản đầy đủ ngay(0 tr)

×