Starting Out with Programming Logic and Design by Tony Gaddis
The book is ideal for a programming logic course taught as a precursor to a language-specific introductory programming course, or for the first part of an introductory programming course.
Get this product faster from our US warehouse
Preface xiii
Chapter 1 Input, Processing, and Output 29
2.1 Designing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2 Output, Input, and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3 Variable Assignment and Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . 43
IN THE SPOTLIGHT: Calculating a Percentage . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
IN THE SPOTLIGHT: Calculating an Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
IN THE SPOTLIGHT: Converting a Math Formula to a
Programming Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.4 Variable Declarations and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.5 Named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.6 Hand Tracing a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.7 Documenting a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
IN THE SPOTLIGHT: Using Named Constants, Style Conventions,
and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Chapter 2 Modules 75
3.1 Introduction to Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.2 Defining and Calling a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
IN THE SPOTLIGHT: Defining and Calling Modules . . . . . . . . . . . . . . . . . . . . . . . 84
3.3 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.4 Passing Arguments to Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
IN THE SPOTLIGHT: Passing an Argument to a Module . . . . . . . . . . . . . . . . . . . . 95
IN THE SPOTLIGHT: Passing an Argument by Reference . . . . . . . . . . . . . . . . . . . 100
3.5 Global Variables and Global Constants . . . . . . . . . . . . . . . . . . . . . . . . . 104
IN THE SPOTLIGHT: Using Global Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Chapter 3 Decision Structures and Boolean Logic 115
4.1 Introduction to Decision Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
IN THE SPOTLIGHT: Using the If-Then Statement . . . . . . . . . . . . . . . . . . . . . . . 122
4.2 Dual Alternative Decision Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
IN THE SPOTLIGHT: Using the If-Then-Else Statement . . . . . . . . . . . . . . . . . . 126
4.3 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.4 Nested Decision Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
IN THE SPOTLIGHT: Multiple Nested Decision Structures . . . . . . . . . . . . . . . . . 138
4.5 The Case Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
IN THE SPOTLIGHT: Using a Case Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
4.6 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.7 Boolean Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Chapter 4 Repetition Structures 163
5.1 Introduction to Repetition Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 163
5.2 Condition-Controlled Loops: While, Do-While, and Do-Until . . . . . . 164
IN THE SPOTLIGHT: Designing a While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . 169
IN THE SPOTLIGHT: Designing a Do-While Loop . . . . . . . . . . . . . . . . . . . . . . . . 178
5.3 Count-Controlled Loops and the For Statement . . . . . . . . . . . . . . . . . 183
IN THE SPOTLIGHT: Designing a Count-Controlled Loop
with the For Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.4 Calculating a Running Total . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
5.5 Sentinels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
IN THE SPOTLIGHT: Using a Sentinel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
5.6 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Chapter 5 Functions 217
6.1 Introduction to Functions: Generating Random Numbers . . . . . . . . . . 217
IN THE SPOTLIGHT: Using Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 221
IN THE SPOTLIGHT: Using Random Numbers to Represent Other Values . . . . . 223
6.2 Writing Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
IN THE SPOTLIGHT: Modularizing with Functions . . . . . . . . . . . . . . . . . . . . . . . 232
6.3 More Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Chapter 6 Input Validation 257
7.1 Garbage In, Garbage Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
7.2 The Input Validation Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
IN THE SPOTLIGHT: Designing an Input Validation Loop . . . . . . . . . . . . . . . . . . 260
7.3 Defensive Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Chapter 7 Arrays 269
8.1 Array Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
IN THE SPOTLIGHT: Using Array Elements in a Math Expression . . . . . . . . . . . 276
8.2 Sequentially Searching an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
8.3 Processing the Contents of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
IN THE SPOTLIGHT: Processing an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
8.4 Parallel Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
IN THE SPOTLIGHT: Using Parallel Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
8.5 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
IN THE SPOTLIGHT: Using a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . 311
8.6 Arrays of Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Chapter 8 Sorting and Searching Arrays 323
9.1 The Bubble Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
IN THE SPOTLIGHT: Using the Bubble Sort Algorithm . . . . . . . . . . . . . . . . . . . . 330
9.2 The Selection Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
9.3 The Insertion Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
9.4 The Binary Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
IN THE SPOTLIGHT: Using the Binary Search Algorithm . . . . . . . . . . . . . . . . . . . 353
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Chapter 9 Files 361
10.1 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . 361
10.2 Using Loops to Process Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
IN THE SPOTLIGHT: Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
10.3 Using Files and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
10.4 Processing Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
IN THE SPOTLIGHT: Adding and Displaying Records . . . . . . . . . . . . . . . . . . . . . 388
IN THE SPOTLIGHT: Searching for a Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
IN THE SPOTLIGHT: Modifying Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
IN THE SPOTLIGHT: Deleting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
10.5 Control Break Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
IN THE SPOTLIGHT: Using Control Break Logic . . . . . . . . . . . . . . . . . . . . . . . . .403
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Chapter 10 GUI Applications and Event-Driven Programming 549
15.1 Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
15.2 Designing the User Interface for a GUI Program . . . . . . . . . . . . . . . . . . 552
IN THE SPOTLIGHT: Designing a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
15.3 Writing Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
IN THE SPOTLIGHT: Designing an Event Handler . . . . . . . . . . . . . . . . . . . . . . . . 562
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Index 573