A Beginner’s Introduction to Computer Programming You Can Do It! Francis Glassborow with Roberta Allen A Beginner’s Introduction to Computer Programming Francis Glassborow with Roberta Allen A Beginner’s Introduction to Computer Programming You Can Do It! Francis Glassborow with Roberta Allen Copyright2004 JohnWiley&SonsLtd,TheAtrium,SouthernGate,Chichester, WestSussexPO198SQ,England Telephone(+44)1243779777 Email(forordersandcustomerserviceenquiries):[email protected] VisitourHomePageonwww.wileyeurope.comorwww.wiley.com AllRightsReserved.Nopartofthispublicationmaybereproduced,storedinaretrieval systemortransmittedinanyformorbyanymeans,electronic,mechanical,photocopying, recording,scanningorotherwise,exceptunderthetermsoftheCopyright,Designsand PatentsAct1988orunderthetermsofalicenceissuedbytheCopyrightLicensingAgency Ltd,90TottenhamCourtRoad,LondonW1T4LP,UK,withoutthepermissioninwritingof thePublisher,withtheexceptionofanymaterialsuppliedspecificallyforthepurposeof beingenteredandexecutedonacomputersystemforexclusiveusebythepurchaseofthe publication.RequeststothePublishershouldbeaddressedtothePermissionsDepartment, JohnWiley&SonsLtd,TheAtrium,SouthernGate,Chichester,WestSussexPO198SQ, England,[email protected],orfaxedto(+44)1243770620. Thispublicationisdesignedtoprovideaccurateandauthoritativeinformationinregardto thesubjectmattercovered.ItissoldontheunderstandingthatthePublisherisnotengaged inrenderingprofessionalservices.Ifprofessionaladviceorotherexpertassistanceis required,theservicesofacompetentprofessionalshouldbesought. OtherWileyEditorialOffices JohnWiley&SonsInc.,111RiverStreet,Hoboken,NJ07030,USA Jossey-Bass,989MarketStreet,SanFrancisco,CA94103-1741,USA Wiley-VCHVerlagGmbH,Boschstr.12,D-69469Weinheim,Germany JohnWiley&SonsAustraliaLtd,33ParkRoad,Milton,Queensland4064,Australia JohnWiley&Sons(Asia)PteLtd,2ClementiLoop#02-01,JinXingDistripark,Singapore129809 JohnWiley&SonsCanadaLtd,22WorcesterRoad,Etobicoke,Ontario,CanadaM9W1L1 Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappears inprintmaynotbeavailableinelectronicbooks. LibraryofCongressCataloging-in-PublicationData Glassborow,Francis. Abeginner’sintroductiontocomputerprogramming:youcandoit!/ FrancisGlassborow. p.cm. Includesbibliographicalreferencesandindex. ISBN0-470-86398-6(Paper:alk.paper) 1.Computerprogramming.I.Title. QA76.6.G5752003 005.1–dc22 2003020686 BritishLibraryCataloguinginPublicationData AcataloguerecordforthisbookisavailablefromtheBritishLibrary ISBN0-470-86398-6 Typesetin10/11ptJoannabyLaserwordsPrivateLimited,Chennai,India PrintedandboundinGreatBritainbyBiddlesLtd,King’sLynn Thisbookisprintedonacid-freepaperresponsiblymanufacturedfromsustainableforestry inwhichatleasttwotreesareplantedforeachoneusedforpaperproduction. Dedication Thisbookisdedicatedtothemanycolleaguesandnumerousstudentswhomouldedmeintoabetterteacher. InparticularmyfirstHeadofDepartment,GerryAstell,whotaughtmenevertoteachsomethingthatIknew Iwouldlaterretract.Falsesimplicityisneverhelpfultothestudentinthelongrun;itjustmakesapoor teacher’slifeeasierforamoment. Contents HowtoUseThisBook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii StudyElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv EndofChapterElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv EndoftheBook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv TheCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Whyfgw? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii BeforePurchase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii WhyC++? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii GettingtheBestfromThisBook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii WhatYouWillAchieve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx NotesforStudents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx NotesforInstructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx PersonalIntroductions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii 1 YouCanProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 WhatIsProgramming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 IntroductiontoYourProgrammingTools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 OurFirstProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 ElementsofC++Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 APlaypenDoesn’tHaveToBeWhite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 PlottingaPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 MixingColors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 ModernArt? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Tasks,ExercisesandFun. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Roberta’sComments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Description: