TY - BOOK AU - Dale,Nell B. AU - Weems,Chip TI - Programming and problem solving with C++ SN - 0763707988 (pbk.) U1 - 005.133 22 PY - 2005/// CY - Boston PB - Jones and Bartlett Publishers KW - C++ (Computer program language) N1 - Includes index; Preface v 1 Overview of Programming and Problem Solving 1 1.1 Overview of Programming 2 What Is Programming? 2 How Do We Write a Program? 3 1.2 What Is a Programming Language? 9 1.3 What Is a Computer? 15 1.4 Ethics and Responsibilities in the Computing Profession 24 Software Piracy 24 Privacy of Data 25 Use of Computer Resources 26 Software Engineering 27 1.5 Problem-Solving Techniques 27 Ask Questions 28 Look for Things That Are Familiar 28 Solve by Analogy 28 Means-Ends Analysis 29 Divide and Conquer 30 DalePhatToc 11/4/01 2:34 PM Page xiii The Building-Block Approach 30 Merging Solutions 31 Mental Blocks: The Fear of Starting 32 Algorithmic Problem Solving 33 Problem-Solving Case Study: An Algorithm for an Employee Paycheck 33 Summary 37 Quick Check 38 Answers 39 Exam Preparation Exercises 39 Programming Warm-Up Exercises 41 Case Study Follow-Up 41 2 C++ Syntax and Semantics, and the Program Development Process 43 2.1 The Elements of C++ Programs 44 C++ Program Structure 44 Syntax and Semantics 46 Syntax Templates 49 Naming Program Elements: Identifiers 52 Data and Data Types 53 Naming Elements: Declarations 56 Taking Action: Executable Statements 61 Beyond Minimalism: Adding Comments to a Program 66 2.2 Program Construction 67 Blocks (Compound Statements) 69 The C++ Preprocessor 71 An Introduction to Namespaces 73 2.3 More About Output 74 Creating Blank Lines 74 Inserting Blanks Within a Line 75 2.4 Program Entry, Correction, and Execution 76 Entering a Program 76 Compiling and Running a Program 77 Finishing Up 78 Problem-Solving Case Study: Contest Letter 79 xiv | Contents DalePhatToc 11/4/01 2:34 PM Page xiv Testing and Debugging 83 Summary 84 Quick Check 85 Answers 87 Exam Preparation Exercises 88 Programming Warm-Up Exercises 90 Programming Problems 92 Case Study Follow-Up 94 3 Numeric Types, Expressions, and Output 95 3.1 Overview of C++ Data Types 96 3.2 Numeric Data Types 97 Integral Types 97 Floating-Point Types 98 3.3 Declarations for Numeric Types 99 Named Constant Declarations 99 Variable Declarations 100 3.4 Simple Arithmetic Expressions 101 Arithmetic Operators 101 Increment and Decrement Operators 104 3.5 Compound Arithmetic Expressions 105 Precedence Rules 105 Type Coercion and Type Casting 106 3.6 Function Calls and Library Functions 111 Value-Returning Functions 111 Library Functions 113 Void Functions 114 3.7 Formatting the Output 115 Integers and Strings 115 Floating-Point Numbers 118 3.8 Additional string Operations 122 The length and size Functions 122 The find Function 124 The substr Function 125 Problem-Solving Case Study: Painting Traffic Cones 128 Contents | xv DalePhatToc 11/4/01 2:34 PM Page xv Testing and Debugging 132 Summary 133 Quick Check 133 Answers 135 Exam Preparation Exercises 136 Programming Warm-Up Exercises 140 Programming Problems 143 Case Study Follow-Up 145 4 Program Input and the Software Design Process 147 4.1 Getting Data into Programs 148 Input Streams and the Extraction Operator (>>) 149 The Reading Marker and the Newline Character 152 Reading Character Data with the get Function 153 Skipping Characters with the ignore Function 156 Reading String Data 157 4.2 Interactive Input/Output 158 4.3 Noninteractive Input/Output 160 4.4 File Input and Output 161 Files 161 Using Files 162 An Example Program Using Files 165 Run-Time Input of File Names 167 4.5 Input Failure 168 4.6 Software Design Methodologies 170 4.7 What Are Objects? 171 4.8 Object-Oriented Design 173 4.9 Functional Decomposition 174 Modules 176 Implementing the Design 177 A Perspective on Design 181 Problem-Solving Case Study: Stretching a Canvas 183 Testing and Debugging 189 Testing and Debugging Hints 191 xvi | Contents DalePhatToc 11/4/01 2:34 PM Page xvi Summary 191 Quick Check 192 Answers 193 Exam Preparation Exercises 193 Programming Warm-Up Exercises 196 Programming Problems 198 Case Study Follow-Up 199 5 Conditions, Logical Expressions, and Selection Control Structures 201 5.1 Flow of Control 202 Selection 203 5.2 Conditions and Logical Expressions 204 The bool Data Type 204 Logical Expressions 205 Precedence of Operators 214 Relational Operators with Floating-Point Types 216 5.3 The If Statement 217 The If-Then-Else Form 217 Blocks (Compound Statements) 220 The If-Then Form 222 A Common Mistake 224 5.4 Nested If Statements 224 The Dangling else 228 5.5 Testing the State of an I/O Stream 229 Problem-Solving Case Study: Warning Notices 231 Testing and Debugging 236 Testing in the Problem-Solving Phase: The Algorithm Walk-Through 236 Testing in the Implementation Phase 239 The Test Plan 244 Tests Performed Automatically During Compilation and Execution 246 Testing and Debugging Hints 247 Contents | xvii DalePhatToc 11/4/01 2:34 PM Page xvii Summary 249 Quick Check 249 Answers 250 Exam Preparation Exercises 250 Programming Warm-Up Exercises 254 Programming Problems 256 Case Study Follow-Up 259 6 Looping 261 6.1 The While Statement 262 6.2 Phases of Loop Execution 264 6.3 Loops Using the While Statement 265 Count-Controlled Loops 265 Event-Controlled Loops 267 Looping Subtasks 273 6.4 How to Design Loops 276 Designing the Flow of Control 277 Designing the Process Within the Loop 278 The Loop Exit 279 6.5 Nested Logic 280 Designing Nested Loops 284; Problem-Solving Case Study: Average Income by Gender 291 Testing and Debugging 297 Loop-Testing Strategy 297 Test Plans Involving Loops 297 Testing and Debugging Hints 299 Summary 300 Quick Check 301 Answers 301 Exam Preparation Exercises 302 Programming Warm-Up Exercises 305 Programming Problems 305 Case Study Follow-Up 308 xviii | Contents DalePhatToc 11/4/01 2:34 PM Page xviii 7 Functions 309 7.1 Functional Decomposition with Void Functions 310 When to Use Functions 311 Writing Modules as Void Functions 311 7.2 An Overview of User-Defined Functions 316 Flow of Control in Function Calls 316 Function Parameters 316 7.3 Syntax and Semantics of Void Functions 319 Function Call (Invocation) 319 Function Declarations and Definitions 320 Local Variables 322 The Return Statement 324 Header Files 325 7.4 Parameters 326 Value Parameters 327 Reference Parameters 328 An Analogy 331 Matching Arguments with Parameters 332 7.5 Designing Functions 335 Writing Assertions as Program Comments 337 Documenting the Direction of Data Flow 339 Problem-Solving Case Study: Comparison of Furniture-Store Sales 343 Testing and Debugging 352 The assert Library Function 353 Testing and Debugging Hints 354 Summary 355 Quick Check 356 Answers 357 Exam Preparation Exercises 357 Programming Warm-Up Exercises 363 Programming Problems 365 Case Study Follow-Up 369 Contents | xix DalePhatToc 11/4/01 2:34 PM Page xix 8 Scope, Lifetime, and More on Functions 371 8.1 Scope of Identifiers 372 Scope Rules 374 Variable Declarations and Definitions 378 Namespaces 379 8.2 Lifetime of a Variable 382 Initializations in Declarations 382 8.3 Interface Design 384 Side Effects 384 Global Constants 387 8.4 Value-Returning Functions 389 Boolean Functions 394 Interface Design and Side Effects 398 When to Use Value-Returning Functions 399 Problem-Solving Case Study: Reformat Dates 401 Problem-Solving Case Study: Starship Weight and Balance 412 Testing and Debugging 423 Stubs and Drivers 423 Testing and Debugging Hints 425 Summary 426 Quick Check 427 Answers 428 Exam Preparation Exercises 428 Programming Warm-Up Exercises 432 Programming Problems 433 Case Study Follow-Up 435 9 Additional Control Structures 437 9.1 The Switch Statement 438 9.2 The Do-While Statement 443 9.3 The For Statement 446 9.4 The Break and Continue Statements 450 9.5 Guidelines for Choosing a Looping Statement 453 Problem-Solving Case Study: Monthly Rainfall Averages 454 xx | Contents DalePhatToc 11/4/01 2:34 PM Page xx Testing and Debugging 459 Testing and Debugging Hints 460 Summary 460 Quick Check 461 Answers 461 Exam Preparation Exercises 462 Programming Warm-Up Exercises 463 Programming Problems 465 Case Study Follow-Up 467; Problem-Solving Case Study: Average Income by Gender 291 Testing and Debugging 297 Loop-Testing Strategy 297 Test Plans Involving Loops 297 Testing and Debugging Hints 299 Summary 300 Quick Check 301 Answers 301 Exam Preparation Exercises 302 Programming Warm-Up Exercises 305 Programming Problems 305 Case Study Follow-Up 308 xviii | Contents DalePhatToc 11/4/01 2:34 PM Page xviii 7 Functions 309 7.1 Functional Decomposition with Void Functions 310 When to Use Functions 311 Writing Modules as Void Functions 311 7.2 An Overview of User-Defined Functions 316 Flow of Control in Function Calls 316 Function Parameters 316 7.3 Syntax and Semantics of Void Functions 319 Function Call (Invocation) 319 Function Declarations and Definitions 320 Local Variables 322 The Return Statement 324 Header Files 325 7.4 Parameters 326 Value Parameters 327 Reference Parameters 328 An Analogy 331 Matching Arguments with Parameters 332 7.5 Designing Functions 335 Writing Assertions as Program Comments 337 Documenting the Direction of Data Flow 339 Problem-Solving Case Study: Comparison of Furniture-Store Sales 343 Testing and Debugging 352 The assert Library Function 353 Testing and Debugging Hints 354 Summary 355 Quick Check 356 Answers 357 Exam Preparation Exercises 357 Programming Warm-Up Exercises 363 Programming Problems 365 Case Study Follow-Up 369 Contents | xix DalePhatToc 11/4/01 2:34 PM Page xix 8 Scope, Lifetime, and More on Functions 371 8.1 Scope of Identifiers 372 Scope Rules 374 Variable Declarations and Definitions 378 Namespaces 379 8.2 Lifetime of a Variable 382 Initializations in Declarations 382 8.3 Interface Design 384 Side Effects 384 Global Constants 387 8.4 Value-Returning Functions 389 Boolean Functions 394 Interface Design and Side Effects 398 When to Use Value-Returning Functions 399 Problem-Solving Case Study: Reformat Dates 401 Problem-Solving Case Study: Starship Weight and Balance 412 Testing and Debugging 423 Stubs and Drivers 423 Testing and Debugging Hints 425 Summary 426 Quick Check 427 Answers 428 Exam Preparation Exercises 428 Programming Warm-Up Exercises 432 Programming Problems 433 Case Study Follow-Up 435 9 Additional Control Structures 437 9.1 The Switch Statement 438 9.2 The Do-While Statement 443 9.3 The For Statement 446 9.4 The Break and Continue Statements 450 9.5 Guidelines for Choosing a Looping Statement 453 Problem-Solving Case Study: Monthly Rainfall Averages 454 xx | Contents DalePhatToc 11/4/01 2:34 PM Page xx Testing and Debugging 459 Testing and Debugging Hints 460 Summary 460 Quick Check 461 Answers 461 Exam Preparation Exercises 462 Programming Warm-Up Exercises 463 Programming Problems 465 Case Study Follow-Up 467; 10 Simple Data Types: Built-In and User-Defined 469 10.1 Built-In Simple Types 470 Integral Types 472 Floating-Point Types 475 10.2 Additional C++ Operators 476 Assignment Operators and Assignment Expressions 478 Increment and Decrement Operators 479 Bitwise Operators 480 The Cast Operation 480 The sizeof Operator 481 The ?: Operator 481 Operator Precedence 482 10.3 Working with Character Data 484 Character Sets 485 C++ char Constants 487 Programming Techniques 488 10.4 More on Floating-Point Numbers 495 Representation of Floating-Point Numbers 495 Arithmetic with Floating-Point Numbers 498 Implementation of Floating-Point Numbers in the Computer 499 10.5 User-Defined Simple Types 505 The Typedef Statement 506 Enumeration Types 506 Named and Anonymous Data Types 513 User-Written Header Files 514 Contents | xxi DalePhatToc 11/4/01 2:34 PM Page xxi 10.6 More on Type Coercion 515 Type Coercion in Arithmetic and Relational Expressions 516 Type Coercion in Assignments, Argument Passing, and Return of a Function Value 517 Problem-Solving Case Study: Finding the Area Under a Curve 519 Problem-Solving Case Study: Rock, Paper, Scissors 527 Testing and Debugging 536 Floating-Point Data 536 Coping with Input Errors 536 Testing and Debugging Hints 537 Summary 539 Quick Check 539 Answers 540 Exam Preparation Exercises 540 Programming Warm-Up Exercises 543 Programming Problems 544 Case Study Follow-Up 545 11 Structured Types, Data Abstraction, and Classes 547 11.1 Simple Versus Structured Data Types 548 11.2 Records (C++ Structs) 549 Accessing Individual Components 551 Aggregate Operations on Structs 553 More About Struct Declarations 554 Hierarchical Records 555 11.3 Unions 557 11.4 Data Abstraction 559 11.5 Abstract Data Types 561 11.6 C++ Classes 564 Classes, Class Objects, and Class Members 568 Built-in Operations on Class Objects 569 Class Scope 571 Information Hiding 571 11.7 Specification and Implementation Files 573 The Specification File 573 xxii | Contents DalePhatToc 11/4/01 2:34 PM Page xxii The Implementation File 575 Compiling and Linking a Multifile Program 580 11.8 Guaranteed Initialization with Class Constructors 582 Invoking a Constructor 584 Revised Specification and Implementation Files for TimeType 585 Guidelines for Using Class Constructors 588 Problem-Solving Case Study: Manipulating Dates 590 Problem-Solving Case Study: Birthday Calls 602 Testing and Debugging 610 Testing and Debugging Hints 614 Summary 615 Quick Check 615 Answers 617 ER -