Table Of ContentPROGRAMMING LOGIC
AND DESIGN
COMPREHENSIVE
This page intentionally left blank
SIXTH EDITION
P R O G R A M M I N G L O G I C
A N D D E S I G N
COMPREHENSIVE
JOYCE FAR RELL
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may
be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall
learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights
restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and
alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for
materials in your areas of interest.
Programming Logic and Design, © 2011 Course Technology, Cengage Learning
Comprehensive, Sixth Edition
ALL RIGHTS RESERVED. No part of this work covered by the copyright
Joyce Farrell
herein may be reproduced, transmitted, stored or used in any form or by
any means—graphic, electronic, or mechanical, including but not limited
Executive Editor: Marie Lee
to photocopying, recording, scanning, digitizing, taping, Web distribution,
Acquisitions Editor: Amy Jollymore
information networks, or information storage and retrieval systems, except
Managing Editor: Tricia Coia as permitted under Section 107 or 108 of the 1976 United States Copyright
Developmental Editor: Dan Seiter Act—without the prior written permission of the publisher.
Content Project Manager: Jennifer Feltri
For product information and technology assistance, contact us at
Editorial Assistant: Zina Kresin Cengage Learning Customer & Sales Support, 1-800-354-9706
Marketing Manager: Bryant Chrzan For permission to use material from this text or product,
Art Director: Marissa Falco submit all requests online at cengage.com/permissions
Further permissions questions can be emailed to
Text Designer: Shawn Girsberger permissionrequest@cengage.com
Cover Designer: Cabbage Design Company
Cover Image: iStockphoto Library of Congress Control Number: 2009938501
Print Buyer: Julio Esperas
ISBN-13: 978-0-5387-4476-8
Copy Editor: Michael Beckett
ISBN-10: 0-538-74476-6
Proofreader: Vicki Zimmer
Indexer: Alexandra Nickerson
Course Technology
Compositor: Integra 20 Channel Center Street
Boston, MA 02210
USA
Some of the product names and company names used in this book have
been used for identifi cation purposes only and may be trademarks or
registered trademarks of their respective manufacturers and sellers.
Course Technology, a part of Cengage Learning, reserves the right to revise
this publication and make changes from time to time in its content without
notice.
Cengage Learning is a leading provider of customized learning solutions with
offi ce locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil, and Japan. Locate your local offi ce at:
www.cengage.com/global
Cengage Learning products are represented in Canada by
Nelson Education, Ltd.
To learn more about Course Technology, visit
www.cengage.com/coursetechnology
Purchase any of our products at your local college store or at our preferred
online store www.ichapters.com
Printed in Canada
1 2 3 4 5 6 7 14 13 12 11 10
Brief Contents
v
Preface . . . . . . . . . . . . . . . . . xvi
CHAPTER 1 An Overview of Computers and Programming . . 1
CHAPTER 2 Working with Data, Creating Modules,
and Designing High-Quality Programs . . . . 41
CHAPTER 3 Understanding Structure . . . . . . . . . . 92
CHAPTER 4 Making Decisions . . . . . . . . . . . . 133
CHAPTER 5 Looping . . . . . . . . . . . . . . . . 184
CHAPTER 6 Arrays . . . . . . . . . . . . . . . . . 228
CHAPTER 7 File Handling and Applications . . . . . . 276
CHAPTER 8 Advanced Array Concepts, Indexed Files,
and Linked Lists. . . . . . . . . . . . . 325
CHAPTER 9 Advanced Modularization Techniques . . . . 370
CHAPTER 10 Object-Oriented Programming . . . . . . . 426
CHAPTER 11 More Object-Oriented Programming
Concepts. . . . . . . . . . . . . . . . 469
CHAPTER 12 Event-Driven GUI Programming,
Multithreading, and Animation . . . . . . . 515
CHAPTER 13 System Modeling with the UML . . . . . . 550
CHAPTER 14 Using Relational Databases . . . . . . . . 585
APPENDIX A Understanding Numbering Systems
and Computer Codes. . . . . . . . . . . 637
APPENDIX B Flowchart Symbols . . . . . . . . . . . 646
BRIEF CONTENTS
APPENDIX C Structures . . . . . . . . . . . . . . . 647
APPENDIX D Solving Difficult Structuring Problems . . . 649
APPENDIX E Creating Print Charts . . . . . . . . . . 658
vi APPENDIX F Two Variations on the Basic Structures—
case and do-while . . . . . . . . . . 660
Glossary . . . . . . . . . . . . . . . . 666
Index . . . . . . . . . . . . . . . . . 681
Contents
vii
Preface . . . . . . . . . . . . . . . . . xvi
CHAPTER 1 An Overview of Computers
and Programming . . . . . . . . . . . . . 1
Understanding Computer Systems. . . . . . . . . . . . . . 2
Understanding Simple Program Logic . . . . . . . . . . . . 5
Understanding the Program Development Cycle . . . . . . . 8
Understanding the Problem . . . . . . . . . . . . . . . . 8
Planning the Logic . . . . . . . . . . . . . . . . . . . .10
Coding the Program . . . . . . . . . . . . . . . . . . .10
Using Software to Translate the Program
into Machine Language . . . . . . . . . . . . . . . . .11
Testing the Program . . . . . . . . . . . . . . . . . . .12
Putting the Program into Production . . . . . . . . . . . .14
Maintaining the Program . . . . . . . . . . . . . . . . .14
Using Pseudocode Statements and Flowchart Symbols . . . .15
Writing Pseudocode . . . . . . . . . . . . . . . . . . .15
Drawing Flowcharts . . . . . . . . . . . . . . . . . . .17
Repeating Instructions . . . . . . . . . . . . . . . . . .18
Using a Sentinel Value to End a Program. . . . . . . . . . .20
Understanding Programming and User Environments . . . . .23
Understanding Programming Environments . . . . . . . . .23
Understanding User Environments . . . . . . . . . . . . .25
Understanding the Evolution of Programming Models . . . . .27
Chapter Summary . . . . . . . . . . . . . . . . . . . . .28
Key Terms . . . . . . . . . . . . . . . . . . . . . . . .29
Review Questions . . . . . . . . . . . . . . . . . . . . .32
Exercises . . . . . . . . . . . . . . . . . . . . . . . . .37
CHAPTER 2 Working with Data, Creating Modules,
and Designing High-Quality Programs . . . . . . 41
Declaring and Using Variables and Constants. . . . . . . . .42
CONTENTS
Working with Variables . . . . . . . . . . . . . . . . . .42
Naming Variables . . . . . . . . . . . . . . . . . . . .44
Understanding Unnamed, Literal Constants
and their Data Types . . . . . . . . . . . . . . . . . .45
Understanding the Data Types of Variables . . . . . . . . .46
Declaring Named Constants. . . . . . . . . . . . . . . .47
viii
Assigning Values to Variables . . . . . . . . . . . . . . . .48
Performing Arithmetic Operations . . . . . . . . . . . . .49
Understanding the Advantages of Modularization . . . . . . .52
Modularization Provides Abstraction . . . . . . . . . . . .53
Modularization Allows Multiple Programmers
to Work on a Problem. . . . . . . . . . . . . . . . . .54
Modularization Allows You to Reuse Your Work . . . . . . .54
Modularizing a Program . . . . . . . . . . . . . . . . . .55
Declaring Variables and Constants
within Modules . . . . . . . . . . . . . . . . . . . . .59
Understanding the Most Common Configuration
for Mainline Logic . . . . . . . . . . . . . . . . . . . .61
Creating Hierarchy Charts . . . . . . . . . . . . . . . . .66
Features of Good Program Design. . . . . . . . . . . . . .68
Using Program Comments . . . . . . . . . . . . . . . .69
Choosing Identifiers . . . . . . . . . . . . . . . . . . .71
Designing Clear Statements. . . . . . . . . . . . . . . .72
Writing Clear Prompts and Echoing Input. . . . . . . . . .74
Maintaining Good Programming Habits. . . . . . . . . . .76
Chapter Summary . . . . . . . . . . . . . . . . . . . . .77
Key Terms . . . . . . . . . . . . . . . . . . . . . . . .78
Review Questions . . . . . . . . . . . . . . . . . . . . .82
Exercises . . . . . . . . . . . . . . . . . . . . . . . . .86
CHAPTER 3 Understanding Structure . . . . . . . . . . 92
Understanding Unstructured Spaghetti Code . . . . . . . . .93
Understanding the Three Basic Structures . . . . . . . . . .95
Using a Priming Input to Structure a Program . . . . . . . 103
Understanding the Reasons for Structure . . . . . . . . . 110
Recognizing Structure . . . . . . . . . . . . . . . . . . 111
Structuring and Modularizing Unstructured Logic . . . . . . 115
Chapter Summary . . . . . . . . . . . . . . . . . . . . 121
Key Terms . . . . . . . . . . . . . . . . . . . . . . . 122
Review Questions . . . . . . . . . . . . . . . . . . . . 123
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 127
Description:Programming Logic and Design,. Comprehensive, Sixth Edition. Joyce Farrell. Executive Editor: Marie Lee. Acquisitions Editor: Amy Jollymore.