Transcription of Programming Design - Syl9.com
1 Starting Out withThirdEditionProgrammingLogic &DesignThis page intentionally left blank ThirdEditionProgrammingLogic &DesignTony GaddisHaywood Community CollegeStarting Out withBoston Columbus Indianapolis New York San Francisco Upper Saddle RiverAmsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal TorontoDelhi Mexico City S o Paulo Sydney Hong Kong Seoul Singapore Taipei TokyoEditorial Director:Marcia HortonEditor in Chief:Michael HirschAcquisitions Editor:Matt GoldsteinEditorial Assistant:Chelsea KharakozovaDirector of Marketing:Patrice JonesMarketing Manager:Yez AlayanMarketing Coordinator:Kathryn FerrantiMarketing Assistant:Emma SniderDirector of Production:Vince O BrienManaging Editor:Jeff HolcombProduction Editor:Pat BrownCopyright 2013, 2010, 2008 by Pearson Education, Inc.
2 , publishing as Addison-Wesley. Allrights reserved. Manufactured in the United States of America. This publication is protected byCopyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic,mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material from thiswork, please submit a written request to Pearson Education, Inc., Permissions Department, OneLake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to of the designations by manufacturers and sellers to distinguish their products are claimed astrademarks.
3 Where those designations appear in this book, and the publisher was aware of a trade-mark claim, the designations have been printed in initial caps or all of Congress Cataloging-in-Publication DataGaddis, out with Programming logic and Design /Tony Gaddis. 3rd : 978-0-13-280545-2 ISBN-10: 0-13-280545-61. Computer Programming . I. Title. II. Title: Starting out with Programming logic and dc23201104425010 9 8 7 6 5 4 3 2 1 ISBN 10: 0-13-280545-6 ISBN 13: 978-0-13-280545-2 Manufacturing Buyer:Pat BrownArt Director:Anthony GemmellaroCover Designer:Joyce Cosentino WellsCover Art: iStockphotoMedia Project Manager:John CassarFull-Service Project Management:Jogender Taneja/Aptara , :Aptara , :Edwards BrothersCover Printer.
4 Lehigh-Phoenix Color/HagerstownBrief ContentsvPrefacexiiiAcknowledgmentsxxiAb out the AuthorxxiiiChapter 1 Introduction to Computers and Programming1 Chapter 2 Input, Processing, and Output27 Chapter 3 Modules79 Chapter 4 Decision Structures and Boolean Logic121 Chapter 5 Repetition Structures169 Chapter 6 Functions225 Chapter 7 Input Validation267 Chapter 8 Arrays281 Chapter 9 Sorting and Searching Arrays337 Chapter 10 Files375 Chapter 11 Menu-Driven Programs429 Chapter 12 Text Processing475 Chapter 13 Recursion497 Chapter 14 Object-Oriented Programming519 Chapter 15 GUI Applications and Event-Driven Programming565 Appendix AASCII/Unicode Characters585 Appendix BFlowchart Symbols587 Appendix CPseudocode Reference589 Appendix DAnswers to Checkpoint Questions(located on the CD that accompanies this book)
5 Index601 This page intentionally left blank ContentsviiPreface xiiiAcknowledgmentsxxiAbout the AuthorxxiiiChapter 1 Introduction to Computers and Programming Introduction .. Hardware .. How Computers Store Data .. How a Program Works .. Types of Software ..20 Review Questions .. 22 Chapter 2 Input, Processing, and Output Designing a Program .. Output, Input, and Variables .. Variable Assignment and Calculations ..41IN THE SPOTLIGHT: Calculating Cell Phone Overage Fees .. 45IN THE SPOTLIGHT:Calculating a Percentage .. 47IN THE SPOTLIGHT:Calculating an Average .. 50IN THE SPOTLIGHT: Converting a Math Formula to a Programming Statement.
6 Variable Declarations and Data Types .. Named Constants .. Hand Tracing a Program .. Documenting a Program .. 64IN THE SPOTLIGHT:Using Named Constants, Style Conventions, and Comments .. Your First Program ..68 Review Questions .. 71 Debugging Exercises .. 76 Programming Exercises .. 77 Chapter 3 Modules Introduction to Modules .. Defining and Calling a Module .. 82viiiContentsIN THE SPOTLIGHT: Defining and Calling Modules .. Local Variables .. Passing Arguments to Modules .. 94IN THE SPOTLIGHT:Passing an Argument to a Module .. 99IN THE SPOTLIGHT:Passing an Argument by Reference .. Global Variables and Global Constants.
7 108IN THE SPOTLIGHT:Using Global Constants .. 109 Review Questions .. 113 Debugging Exercises .. 117 Programming Exercises .. 117 Chapter 4 Decision Structures and Boolean Logic Introduction to Decision Structures .. 121IN THE SPOTLIGHT:Using the If-ThenStatement .. Dual Alternative Decision Structures .. 131IN THE SPOTLIGHT:Using the If-Then-ElseStatement .. Comparing Strings .. Nested Decision Structures .. 141IN THE SPOTLIGHT: Multiple Nested Decision Structures .. The Case Structure .. 148IN THE SPOTLIGHT:Using a Case Structure .. Logical Operators .. Boolean Variables .. 160 Review Questions .. 161 Debugging Exercises.
8 165 Programming Exercises .. 166 Chapter 5 Repetition Structures Introduction to Repetition Structures .. Condition-Controlled Loops: While, Do-While, and Do-Until .. 170IN THE SPOTLIGHT: Designing a WhileLoop .. 175IN THE SPOTLIGHT: Designing a Do-WhileLoop .. Count-Controlled Loops and the ForStatement .. 189IN THE SPOTLIGHT:Designing a Count-Controlled Loop with the ForStatement .. Calculating a Running Total .. Sentinels .. 211IN THE SPOTLIGHT: Using a Sentinel .. Nested Loops .. 215 Review Questions .. 218 Debugging Exercises .. 222 Programming Exercises .. 222 ContentsixChapter 6 Functions Introduction to Functions: Generating Random Numbers.
9 225IN THE SPOTLIGHT: Using Random Numbers .. 229IN THE SPOTLIGHT: Using Random Numbers to Represent Other Values .. Writing Your Own Functions .. 233IN THE SPOTLIGHT: Modularizing with Functions .. More Library Functions .. 248 Review Questions .. 259 Debugging Exercises .. 261 Programming Exercises .. 262 Chapter 7 Input Validation Garbage In, Garbage Out .. The Input Validation Loop .. 268IN THE SPOTLIGHT:Designing an Input Validation Loop .. Defensive Programming .. 275 Review Questions .. 276 Debugging Exercises .. 278 Programming Exercises .. 279 Chapter 8 Arrays Array Basics .. 281IN THE SPOTLIGHT:Using Array Elements in a Math Expression.
10 Sequentially Searching an Array .. Processing the Contents of an Array .. 301IN THE SPOTLIGHT:Processing an Array .. Parallel Arrays .. 315IN THE SPOTLIGHT:Using Parallel Arrays .. Two-Dimensional Arrays .. 319IN THE SPOTLIGHT:Using a Two-Dimensional Array .. Arrays of Three or More Dimensions .. 328 Review Questions .. 329 Debugging Exercises .. 332 Programming Exercises .. 333 Chapter 9 Sorting and Searching Arrays The Bubble Sort Algorithm .. 337IN THE SPOTLIGHT: Using the Bubble Sort Algorithm .. The Selection Sort Algorithm .. The Insertion Sort Algorithm .. The Binary Search Algorithm .. 363xContentsIN THE SPOTLIGHT: Using the Binary Search Algorithm.