ebook img

Python Programming: An Introduction to Computer Science PDF

261 Pages·2012·1.2 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science JohnM.Zelle, Ph.D. Version 1.0rc2 Fall 2002 Copyright(cid:0)c 2002byJohnM.Zelle Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted, in anyform orbyanymeans, electronic, mechanical, photocopying,recording, or otherwise, withoutprior writtenpermissionoftheauthor. ThisdocumentwaspreparedwithLATEX2e andreproducedbyWartburgCollegePrintingServices. Contents 1 ComputersandPrograms 1 1.1 TheUniversalMachine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 ProgramPower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 WhatisComputerScience? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 HardwareBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.5 ProgrammingLanguages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.6 TheMagicofPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.7 InsideaPythonProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.8 ChaosandComputers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 WritingSimplePrograms 13 2.1 TheSoftwareDevelopmentProcess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2 ExampleProgram: TemperatureConverter . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.3 ElementsofPrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.3.1 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.3.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.4 OutputStatements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.5 AssignmentStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.5.1 SimpleAssignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.5.2 AssigningInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5.3 SimultaneousAssignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.6 DefiniteLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.7 ExampleProgram: FutureValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3 ComputingwithNumbers 25 3.1 NumericDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.2 UsingtheMathLibrary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.3 AccumulatingResults: Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.4 TheLimitsofInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.5 HandlingLargeNumbers: LongInts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.6 TypeConversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4 ComputingwithStrings 39 4.1 TheStringDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2 SimpleStringProcessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3 StringsandSecretCodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.3.1 StringRepresentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.3.2 ProgramminganEncoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4.3.3 ProgrammingaDecoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.3.4 OtherStringOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 i ii CONTENTS 4.3.5 FromEncodingtoEncryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.4 OutputasStringManipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4.4.1 ConvertingNumberstoStrings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4.4.2 StringFormatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.4.3 BetterChangeCounter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.5 FileProcessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.5.1 Multi-LineStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4.5.2 FileProcessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.5.3 ExampleProgram: BatchUsernames . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.5.4 ComingAttraction: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5 ObjectsandGraphics 61 5.1 TheObjectofObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.2 GraphicsProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 5.3 UsingGraphicalObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 5.4 GraphingFutureValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 5.5 ChoosingCoordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5.6 InteractiveGraphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 5.6.1 GettingMouseClicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 5.6.2 HandlingTextualInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.7 GraphicsModuleReference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.7.1 GraphWinObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.7.2 GraphicsObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.7.3 EntryObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.7.4 DisplayingImages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.7.5 GeneratingColors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 6 DefiningFunctions 85 6.1 TheFunctionofFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 6.2 Functions,Informally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 6.3 FutureValuewithaFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 6.4 FunctionsandParameters: TheGoryDetails . . . . . . . . . . . . . . . . . . . . . . . . . . 90 6.5 FunctionsthatReturnValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 6.6 FunctionsandProgramStructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 7 ControlStructures,Part1 101 7.1 SimpleDecisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 7.1.1 Example: TemperatureWarnings. . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 7.1.2 FormingSimpleConditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 7.1.3 Example: ConditionalProgramExecution . . . . . . . . . . . . . . . . . . . . . . . 104 7.2 Two-WayDecisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 7.3 Multi-WayDecisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.4 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 7.5 StudyinDesign: MaxofThree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.5.1 Strategy1: CompareEachtoAll . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.5.2 Strategy2: DecisionTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 7.5.3 Strategy3: SequentialProcessing . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 7.5.4 Strategy4: UsePython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 7.5.5 SomeLessons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 7.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 CONTENTS iii 8 ControlStructures,Part2 119 8.1 ForLoops: AQuickReview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 8.2 IndefiniteLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 8.3 CommonLoopPatterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.3.1 InteractiveLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.3.2 SentinelLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 8.3.3 FileLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 8.3.4 NestedLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 8.4 ComputingwithBooleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 8.4.1 BooleanOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 8.4.2 BooleanAlgebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 8.5 OtherCommonStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8.5.1 Post-TestLoop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8.5.2 LoopandaHalf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 8.5.3 BooleanExpressionsasDecisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 8.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 9 SimulationandDesign 137 9.1 SimulatingRacquetball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 9.1.1 ASimulationProblem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 9.1.2 ProgramSpecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 9.2 RandomNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 9.3 Top-DownDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 9.3.1 Top-LevelDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 9.3.2 SeparationofConcerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 9.3.3 Second-LevelDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 9.3.4 DesigningsimNGames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.3.5 Third-LevelDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 9.3.6 FinishingUp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 9.3.7 SummaryoftheDesignProcess . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.4 Bottom-UpImplementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.4.1 UnitTesting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.4.2 SimulationResults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 9.5 OtherDesignTechniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 9.5.1 PrototypingandSpiralDevelopment . . . . . . . . . . . . . . . . . . . . . . . . . . 150 9.5.2 TheArtofDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 10 DefiningClasses 155 10.1 QuickReviewofObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 10.2 ExampleProgram: Cannonball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 10.2.1 ProgramSpecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 10.2.2 DesigningtheProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 10.2.3 ModularizingtheProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.3 DefiningNewClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.3.1 Example: Multi-SidedDice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 10.3.2 Example: TheProjectileClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 10.4 ObjectsandEncapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 10.4.1 EncapsulatingUsefulAbstractions . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 10.4.2 PuttingClassesinModules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 10.5 WidgetObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 10.5.1 ExampleProgram: DiceRoller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 10.5.2 BuildingButtons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 10.5.3 BuildingDice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 iv CONTENTS 10.5.4 TheMainProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 10.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 11 DataCollections 177 11.1 ExampleProblem: SimpleStatistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 11.2 ApplyingLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 11.2.1 ListsareSequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 11.2.2 Listsvs. Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 11.2.3 ListOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 11.3 StatisticswithLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 11.4 CombiningListsandClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 11.5 CaseStudy:PythonCalculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 11.5.1 ACalculatorasanObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 11.5.2 ConstructingtheInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 11.5.3 ProcessingButtons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 11.6 Non-SequentialCollections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 11.6.1 DictionaryBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 11.6.2 DictionaryOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 11.6.3 ExampleProgram: WordFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . 194 11.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 12 Object-OrientedDesign 201 12.1 TheProcessofOOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 12.2 CaseStudy:RacquetballSimulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 12.2.1 CandidateObjectsandMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 12.2.2 ImplementingSimStats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 12.2.3 ImplementingRBallGame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 12.2.4 ImplementingPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 12.2.5 TheCompleteProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 12.3 CaseStudy:DicePoker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 12.3.1 ProgramSpecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 12.3.2 IdentifyingCandidateObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 12.3.3 ImplementingtheModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 12.3.4 AText-BasedUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 12.3.5 DevelopingaGUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 12.4 OOConcepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 12.4.1 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 12.4.2 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 12.4.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 12.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 13 AlgorithmAnalysisandDesign 225 13.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 13.1.1 ASimpleSearchingProblem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 13.1.2 Strategy1: LinearSearch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 13.1.3 Strategy2: BinarySearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 13.1.4 ComparingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 13.2 RecursiveProblem-Solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 13.2.1 RecursiveDefinitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 13.2.2 RecursiveFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 13.2.3 RecursiveSearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 13.3 SortingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 13.3.1 NaiveSorting: SelectionSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 13.3.2 DivideandConquer: MergeSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 CONTENTS v 13.3.3 ComparingSorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 13.4 HardProblems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 13.4.1 TowersofHanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 13.4.2 TheHaltingProblem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 13.4.3 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 vi CONTENTS Chapter 1 Computers and Programs Almosteveryonehasusedacomputeratonetimeoranother. Perhapsyouhaveplayedcomputergamesor used a computer to write a paper or balance your checkbook. Computers are used to predict the weather, designairplanes,makemovies,runbusinesses,performfinancialtransactions,andcontrolfactories. Have you ever stopped to wonder what exactly a computer is? How can one device perform so many different tasks? These basic questions are the starting point for learning about computers and computer programming. 1.1 The Universal Machine Amoderncomputermightbedefinedas“amachinethatstoresandmanipulatesinformationunderthecon- trol of a changeable program.” There are two key elements to this definition. The first is that computers are devices for manipulating information. This means that we can put information into a computer, and it can transform the information into new, useful forms, and then output or display the information for our interpretation. Computersarenottheonlymachinesthatmanipulateinformation. Whenyouuseasimplecalculatorto addupacolumnofnumbers,youareenteringinformation(thenumbers)andthecalculatorisprocessingthe informationtocomputearunningsumwhichisthendisplayed. Anothersimpleexampleisagaspump. As youfillyourtank,thepumpusescertaininputs: thecurrentpriceofgaspergallonandsignalsfromasensor thatreadstherateofgasflowingintoyourcar. Thepumptransformsthisinputintoinformationabouthow muchgasyoutookandhowmuchmoneyyouowe. Wewouldnotconsidereitherthecalculatororthegaspumpasfull-fledgedcomputers,althoughmodern versionsofthesedevicesmayactuallycontainembeddedcomputers. Theyaredifferentfromcomputersin thattheyarebuilttoperformasingle,specifictask.Thisiswherethesecondpartofourdefinitioncomesinto thepicture: computersoperateunderthecontrolofachangeableprogram. Whatexactlydoesthismean? Acomputerprogramisadetailed,step-by-stepsetofinstructionstellingacomputerexactlywhattodo. Ifwechangetheprogram,thenthecomputerperformsadifferentsequenceofactions,andhence,performs a differenttask. Itis thisflexibilitythat allowsyourPC to beatonemoment a word processor,atthenext moment a financial planner, and later on, an arcade game. The machine stays the same, but the program controllingthemachinechanges. Everycomputerisjustamachineforexecuting(carryingout)programs. Therearemanydifferentkinds of computers. You might be familiar with Macintoshes and PCs, but there are literally thousands of other kinds of computers both real and theoretical. One of the remarkable discoveries of computer science is the realization that allof these differentcomputers havethe same power; with suitableprogramming, each computercanbasicallydoallthethingsthatanyothercomputercando. Inthissense,thePCthatyoumight havesittingonyourdeskisreallyauniversalmachine. Itcandoanythingyouwantitto,providedyoucan describethetasktobeaccomplishedinsufficientdetail. Nowthat’sapowerfulmachine! 1 2 CHAPTER1. COMPUTERSANDPROGRAMS 1.2 Program Power You have already learned an important lesson of computing: Software (programs) rules the hardware (the physicalmachine).Itisthesoftwarethatdetermineswhatanycomputercando. Withoutprograms,comput- erswouldjustbeexpensivepaperweights. Theprocessofcreatingsoftwareiscalledprogramming,andthat isthemainfocusofthisbook. Computerprogramming is a challenging activity. Good programming requires an ability to see the big picturewhilepayingattentiontominutedetail. Noteveryonehasthetalenttobecomeafirst-classprogram- mer,justasnoteveryonehastheskillstobeaprofessionalathlete. However,virtuallyanyonecanlearnhow to program computers. With some patience and effort on your part, this book will help you to become a programmer. There are lotsof goodreasons to learnprogramming. Programmingis a fundamentalpart ofcomputer scienceandis,therefore,importanttoanyoneinterestedinbecomingacomputerprofessional.Butotherscan alsobenefitfromtheexperience.Computershavebecomeacommonplacetoolinoursociety.Understanding thestrengthsandlimitationsofthistoolrequiresanunderstandingofprogramming.Non-programmersoften feeltheyareslavesoftheircomputers. Programmers,however,aretrulythemasters. Ifyouwanttobecome amoreintelligentuserofcomputers,thenthisbookisforyou. Programming can also be loads of fun. It is an intellectually engaging activity that allows people to express themselves through useful and sometimes remarkably beautiful creations. Believe it or not, many peopleactuallywritecomputerprogramsasahobby. Programmingalsodevelopsvaluableproblem-solving skills,especiallytheabilitytoanalyzecomplexsystemsbyreducingthemtointeractionsofunderstandable subsystems. Asyouprobablyknow,programmersareingreatdemand.Morethanafewliberalartsmajorshaveturned acouplecomputerprogrammingclassesintoalucrativecareeroption.Computersaresocommonplaceinthe businessworldtodaythattheabilitytounderstandandprogramcomputersmightjustgiveyoutheedgeover yourcompetition,regardlessofyouroccupation. 1.3 What is Computer Science? You might be surprised to learn that computer science is not the study of computers. A famous computer scientistnamedEdsgarDijkstraoncequippedthatcomputersaretocomputersciencewhattelescopesareto astronomy. The computer is an important tool in computer science, but it is not itself the object of study. Sinceacomputercancarryoutanyprocessthatwecandescribe,therealquestionisWhatprocessescanwe describe? Putanotherway,thefundamentalquestionofcomputerscienceissimplyWhatcanbecomputed? Computerscientistsusenumeroustechniquesofinvestigationtoanswerthisquestion. Thethreemainones aredesign,analysis,andexperimentation. Onewaytodemonstratethataparticularproblemcanbesolvedistoactuallydesignasolution. Thatis, wedevelopastep-by-stepprocessforachievingthedesiredresult.Computerscientistscallthisanalgorithm. That’s a fancy word that basically means “recipe.” The design of algorithms is one of the most important facetsofcomputerscience.Inthisbookyouwillfindtechniquesfordesigningandimplementingalgorithms. OneweaknessofdesignisthatitcanonlyanswerthequestionWhatiscomputable? inthepositive. IfI candeviseanalgorithm,thentheproblemissolvable. However,failingtofindanalgorithmdoesnotmean thataproblemisunsolvable.ItmaymeanthatI’mjustnotsmartenough,orIhaven’thitupontherightidea yet. Thisiswhereanalysiscomesin. Analysisistheprocessofexaminingalgorithmsandproblemsmathematically.Computerscientistshave shown that some seemingly simple problems are not solvable by any algorithm. Other problems are in- tractable. The algorithms that solve these problems take too long or require too much memory to be of practical value. Analysis of algorithms is an important part of computer science; throughout this book we will touch on some of the fundamental principles. Chapter 13 has examples of unsolvable and intractable problems. Some problems are too complexor ill-defined to lend themselvesto analysis. In such cases, computer scientists rely on experimentation; they actually implement systems and then study the resulting behavior. Even when theoretical analysis is done, experimentation is often needed in order to verify and refine the

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.