CONTENTS xix 3.4 DESIGNING LOOPS 155 Loops for Sums and Products 155 Ending a Loop 157 Nested Loops 160 Debugging Loops 162 Chapter Summary 165 Answers to Self-Test Exercises 166 Practice Programs 172 Programming Projects 174 Chapter 4 Procedural Abstraction and Functions That Return a Value 181 4.1 TOP-DOWN DESIGN 182 4.2 PREDEFINED FUNCTIONS 183 Using Predefined Functions 183 Random Number Generation 188 Type Casting 190 Older Form of Type Casting 192 Pitfall:Integer Division Drops the Fractional Part 192 4.3 PROGRAMMER-DEFINED FUNCTIONS 193 Function Definitions 193 Functions That Return a Boolean Value 199 Alternate Form for Function Declarations 199 Pitfall:Arguments in the Wrong Order 200 Function Definition-Syntax Summary 201 More About Placement of Function Definitions 202 Programming Tip:Use Function Calls in Branching Statements 203 4.4 PROCEDURAL ABSTRACTION 204 The Black-Box Analogy 204 Programming Tip:Choosing Formal Parameter Names 207 Programming Tip:Nested Loops 208 Case Study:Buying Pizza 211 Programming Tip:Use Pseudocode 217 4.5 SCOPE AND LOCAL VARIABLES 218 The Small Program Analogy 218 Programming Example:Experimental Pea Patch 221
Contents xix 3.4 Designing Loops 155 Loops for Sums and Products 155 Ending a Loop 157 Nested Loops 160 Debugging Loops 162 Chapter Summary 165 Answers to Self-Test Exercises 166 Practice Programs 172 Programming Projects 174 Chapter 4 Procedural Abstraction and Functions That Return a Value 181 4.1 Top-Down Design 182 4.2 Predefined Functions 183 Using Predefined Functions 183 Random Number Generation 188 Type Casting 190 Older Form of Type Casting 192 Pitfall: Integer Division Drops the Fractional Part 192 4.3 Programmer-Defined Functions 193 Function Definitions 193 Functions That Return a Boolean Value 199 Alternate Form for Function Declarations 199 Pitfall: Arguments in the Wrong Order 200 Function Definition–Syntax Summary 201 More About Placement of Function Definitions 202 Programming Tip: Use Function Calls in Branching Statements 203 4.4 Procedural Abstraction 204 The Black-Box Analogy 204 Programming Tip: Choosing Formal Parameter Names 207 Programming Tip: Nested Loops 208 Case Study: Buying Pizza 211 Programming Tip: Use Pseudocode 217 4.5 Scope and Local Variables 218 The Small Program Analogy 218 Programming Example: Experimental Pea Patch 221
XX CONTENTS Global Constants and Global Variables 221 Call-by-Value Formal Parameters Are Local Variables 224 Block Scope 226 Namespaces Revisited 227 Programming Example:The Factorial Function 230 4.6 OVERLOADING FUNCTION NAMES 232 Introduction to Overloading 232 Programming Example:Revised Pizza-Buying Program 235 Automatic Type Conversion 238 Chapter Summary 240 Answers to Self-Test Exercises 240 Practice Programs 245 Programming Projects 247 Chapter 5 Functions for All Subtasks 251 5.1 void FUNCTIONS 252 Definitions of void Functions 252 Programming Example:Converting Temperatures 255 return Statements in void Functions 255 5.2 CALL-BY-REFERENCE PARAMETERS 259 A First View of Call-by-Reference 259 Call-by-Reference in Detail 262 Programming Example:The swap_values Function 267 Mixed Parameter Lists 268 Programming Tip:What Kind of Parameter to Use 269 Pitfall:Inadvertent Local Variables 270 5.3 USING PROCEDURAL ABSTRACTION 273 Functions Calling Functions 273 Preconditions and Postconditions 275 Case Study:Supermarket Pricing 276 5.4 TESTING AND DEBUGGING FUNCTIONS 281 Stubs and Drivers 282 5.5 GENERAL DEBUGGING TECHNIQUES 287 Keep an Open Mind 287 Check Common Errors 287
xx Contents Global Constants and Global Variables 221 Call-by-Value Formal Parameters Are Local Variables 224 Block Scope 226 Namespaces Revisited 227 Programming Example: The Factorial Function 230 4.6 Overloading Function Names 232 Introduction to Overloading 232 Programming Example: Revised Pizza-Buying Program 235 Automatic Type Conversion 238 Chapter Summary 240 Answers to Self-Test Exercises 240 Practice Programs 245 Programming Projects 247 Chapter 5 Functions for All Subtasks 251 5.1 void Functions 252 Definitions of void Functions 252 Programming Example: Converting Temperatures 255 return Statements in void Functions 255 5.2 Call-By-Reference Parameters 259 A First View of Call-by-Reference 259 Call-by-Reference in Detail 262 Programming Example: The swap_values Function 267 Mixed Parameter Lists 268 Programming Tip: What Kind of Parameter to Use 269 Pitfall: Inadvertent Local Variables 270 5.3 Using Procedural Abstraction 273 Functions Calling Functions 273 Preconditions and Postconditions 275 Case Study: Supermarket Pricing 276 5.4 Testing and Debugging Functions 281 Stubs and Drivers 282 5.5 General Debugging Techniques 287 Keep an Open Mind 287 Check Common Errors 287
CONTENTS xxi Localize the Error 288 The assert Macro 290 Chapter Summary 292 Answers to Self-Test Exercises 293 Practice Programs 296 Programming Projects 299 Chapter 6 1/0 Streams as an Introduction to Objects and Classes 305 6.1 STREAMS AND BASIC FILE I/O 306 Why Use Files for I/O?307 File 1/O 308 Introduction to Classes and Objects 312 Programming Tip:Check Whether a File Was Opened Successfully 314 Techniques for File I/O 316 Appending to a File (Optional)320 File Names as Input (Optional)321 6.2 TOOLS FOR STREAM I/O 323 Formatting Output with Stream Functions 323 Manipulators 329 Streams as Arguments to Functions 332 Programming Tip:Checking for the End of a File 332 A Note on Namespaces 335 Programming Example:Cleaning Up a File Format 336 6.3 CHARACTER I/O 338 The Member Functions get and put 338 The putback Member Function (Optional)342 Programming Example:Checking Input 343 Pitfall:Unexpected '\n'in Input 345 Programming Example:Another new_line Function 347 Default Arguments for Functions (Optional)348 The eof Member Function 353 Programming Example:Editing a Text File 355 Predefined Character Functions 356 Pitfall:toupper and tolower Return Values 358
Contents xxi Localize the Error 288 The assert Macro 290 Chapter Summary 292 Answers to Self-Test Exercises 293 Practice Programs 296 Programming Projects 299 Chapter 6 I/O Streams as an Introduction to Objects and Classes 305 6.1 Streams and Basic File I/O 306 Why Use Files for I/O? 307 File I/O 308 Introduction to Classes and Objects 312 Programming Tip: Check Whether a File Was Opened Successfully 314 Techniques for File I/O 316 Appending to a File (Optional) 320 File Names as Input (Optional) 321 6.2 Tools for Stream I/O 323 Formatting Output with Stream Functions 323 Manipulators 329 Streams as Arguments to Functions 332 Programming Tip: Checking for the End of a File 332 A Note on Namespaces 335 Programming Example: Cleaning Up a File Format 336 6.3 Character I/O 338 The Member Functions get and put 338 The putback Member Function (Optional) 342 Programming Example: Checking Input 343 Pitfall: Unexpected '\n' in Input 345 Programming Example: Another new_line Function 347 Default Arguments for Functions (Optional) 348 The eof Member Function 353 Programming Example: Editing a Text File 355 Predefined Character Functions 356 Pitfall: toupper and tolower Return Values 358
xxii CONTENTS Chapter Summary 360 Answers to Self-Test Exercises 361 Practice Programs 368 Programming Projects 370 Chapter 7 Arrays 377 7.1 INTRODUCTION TO ARRAYS 378 Declaring and Referencing Arrays 378 Programming Tip:Use for Loops with Arrays 380 Pitfall:Array Indexes Always Start with Zero 380 Programming Tip:Use a Defined Constant for the Size of an Array 380 Arrays in Memory 382 Pitfall:Array Index Out of Range 383 Initializing Arrays 386 Programming Tip:C++11 Range-Based for Statement 386 7.2 ARRAYS IN FUNCTIONS 389 Indexed Variables as Function Arguments 389 Entire Arrays as Function Arguments 391 The const Parameter Modifier 394 Pitfall:Inconsistent Use of const Parameters 397 Functions That Return an Array 397 Case Study:Production Graph 398 7.3 PROGRAMMING WITH ARRAYS 411 Partially Filled Arrays 411 Programming Tip:Do Not Skimp on Formal Parameters 414 Programming Example:Searching an Array 414 Programming Example:Sorting an Array 417 Programming Example:Bubble Sort 421 7.4 MULTIDIMENSIONAL ARRAYS 424 Multidimensional Array Basics 425 Multidimensional Array Parameters 425 Programming Example:Two-Dimensional Grading Program 427 Pitfall:Using Commas Between Array Indexes 431
xxii Contents Chapter Summary 360 Answers to Self-Test Exercises 361 Practice Programs 368 Programming Projects 370 Chapter 7 Arrays 377 7.1 Introduction to Arrays 378 Declaring and Referencing Arrays 378 Programming Tip: Use for Loops with Arrays 380 Pitfall: Array Indexes Always Start with Zero 380 Programming Tip: Use a Defined Constant for the Size of an Array 380 Arrays in Memory 382 Pitfall: Array Index Out of Range 383 Initializing Arrays 386 Programming Tip: C++11 Range-Based for Statement 386 7.2 Arrays in Functions 389 Indexed Variables as Function Arguments 389 Entire Arrays as Function Arguments 391 The const Parameter Modifier 394 Pitfall: Inconsistent Use of const Parameters 397 Functions That Return an Array 397 Case Study: Production Graph 398 7.3 Programming with Arrays 411 Partially Filled Arrays 411 Programming Tip: Do Not Skimp on Formal Parameters 414 Programming Example: Searching an Array 414 Programming Example: Sorting an Array 417 Programming Example: Bubble Sort 421 7.4 Multidimensional Arrays 424 Multidimensional Array Basics 425 Multidimensional Array Parameters 425 Programming Example: Two-Dimensional Grading Program 427 Pitfall: Using Commas Between Array Indexes 431
CONTENTS xxiii Chapter Summary 432 Answers to Self-Test Exercises 433 Practice Programs 437 Programming Projects 439 Chapter 8 Strings and Vectors 451 8.1 AN ARRAY TYPE FOR STRINGS 453 C-String Values and C-String Variables 453 Pitfall:Using and ==with C Strings 456 Other Functions in <cstring>458 Pitfall:Copying past the end of a C-string using strcpy 461 C-String Input and Output 464 C-String-to-Number Conversions and Robust Input 466 8.2 THE STANDARD string CLASS 472 Introduction to the Standard Class string 472 I/O with the Class string 475 Programming Tip:More Versions of getline 478 Pitfall:Mixing cin >variable;and getline 479 String Processing with the Class string 480 Programming Example:Palindrome Testing 484 Converting Between string Objects and C Strings 487 Converting Between Strings and Numbers 488 8.3 VECTORS 489 Vector Basics 489 Pitfall:Using Square Brackets Beyond the Vector Size 492 Programming Tip:Vector Assignment Is Well Behaved 493 Efficiency Issues 493 Chapter Summary 495 Answers to Self-Test Exercises 495 Practice Programs 497 Programming Projects 498 Chapter 9 Pointers and Dynamic Arrays 507 9.1 POINTERS 508 Pointer Variables 509 Basic Memory Management 516
Contents xxiii Chapter Summary 432 Answers to Self-Test Exercises 433 Practice Programs 437 Programming Projects 439 Chapter 8 Strings and Vectors 451 8.1 An Array Type for Strings 453 C-String Values and C-String Variables 453 Pitfall: Using = and == with C Strings 456 Other Functions in <cstring> 458 Pitfall: Copying past the end of a C-string using strcpy 461 C-String Input and Output 464 C-String-to-Number Conversions and Robust Input 466 8.2 The Standard string Class 472 Introduction to the Standard Class string 472 I/O with the Class string 475 Programming Tip: More Versions of getline 478 Pitfall: Mixing cin >> variable; and getline 479 String Processing with the Class string 480 Programming Example: Palindrome Testing 484 Converting Between string Objects and C Strings 487 Converting Between Strings and Numbers 488 8.3 Vectors 489 Vector Basics 489 Pitfall: Using Square Brackets Beyond the Vector Size 492 Programming Tip: Vector Assignment Is Well Behaved 493 Efficiency Issues 493 Chapter Summary 495 Answers to Self-Test Exercises 495 Practice Programs 497 Programming Projects 498 Chapter 9 Pointers and Dynamic Arrays 507 9.1 Pointers 508 Pointer Variables 509 Basic Memory Management 516