Table of contents for Programming and problem solving with C++ / Nell Dale, Chip Weems, Mark Headington.


Bibliographic record and links to related information available from the Library of Congress catalog. Note: Contents data are machine generated based on pre-publication information provided by the publisher. Contents may have variations from the printed book or be incomplete or contain other coding.


Counter
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
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
Exam Preparation Exercises 619
Programming Warm-Up Exercises 622
Programming Problems 624
Case Study Follow-Up 628
12 Arrays 631
12.1 One-Dimensional Arrays 632
Declaring Arrays 634
Accessing Individual Components 635
Out-of-Bounds Array Indexes 638
Initializing Arrays in Declarations 638
(Lack of) Aggregate Array Operations 639
Examples of Declaring and Accessing Arrays 640
Passing Arrays as Arguments 645
Assertions About Arrays 648
Using Typedef with Arrays 648
12.2 Arrays of Records and Class Objects 649
Arrays of Records 649
Arrays of Class Objects 651
12.3 Special Kinds of Array Processing 652
Subarray Processing 652
Indexes with Semantic Content 652
Contents | xxiii
DalePhatToc 11/4/01 2:35 PM Page xxiii
12.4 Two-Dimensional Arrays 653
12.5 Processing Two-Dimensional Arrays 656
Sum the Rows 657
Sum the Columns 659
Initialize the Array 660
Print the Array 661
12.6 Passing Two-Dimensional Arrays as Arguments 662
12.7 Another Way of Defining Two-Dimensional Arrays 664
12.8 Multidimensional Arrays 666
Problem-Solving Case Study: Comparison of Two Lists 669
Problem-Solving Case Study: City Council Election 675
Testing and Debugging 685
One-Dimensional Arrays 685
Complex Structures 686
Multidimensional Arrays 687
Testing and Debugging Hints 688
Summary 689
Quick Check 689
Answers 691
Exam Preparation Exercises 692
Programming Warm-Up Exercises 698
Programming Problems 701
Case Study Follow-Up 705
13 Array-Based Lists 707
13.1 The List as an Abstract Data Type 708
13.2 Unsorted Lists 713
Basic Operations 713
Insertion and Deletion 716
Sequential Search 718
Sorting 721
13.3 Sorted Lists 724
Basic Operations 726
Insertion 727
Sequential Search 730
Binary Search 730
Deletion 736
xxiv | Contents
DalePhatToc 11/4/01 2:35 PM Page xxiv
13.4 Understanding Character Strings 739
Initializing C Strings 742
C String Input and Output 743
C String Library Routines 746
String Class or C Strings? 747
Problem-Solving Case Study: Exam Attendance 748
Testing and Debugging 755
Testing and Debugging Hints 756
Summary 757
Quick Check 757
Answers 758
Exam Preparation Exercises 758
Programming Warm-Up Exercises 761
Programming Problems 762
Case Study Follow-Up 763
14 Object-Oriented Software Development 765
14.1 Object-Oriented Programming 766
14.2 Objects 768
14.3 Inheritance 769
Deriving One Class from Another 770
Specification of the ExtTime Class 774
Implementation of the ExtTime Class 776
Avoiding Multiple Inclusion of Header Files 780
14.4 Composition 781
Design of a TimeCard Class 782
Implementation of the TimeCard Class 783
14.5 Dynamic Binding and Virtual Functions 785
The Slicing Problem 787
Virtual Functions 788
14.6 Object-Oriented Design 790
Step 1: Identify the Objects and Operations 790
Step 2: Determine the Relationships Among Objects 792
Step 3: Design the Driver 792
14.7 Implementing the Design 793
Problem-Solving Case Study: Time Card Lookup 794
Contents | xxv
DalePhatToc 11/4/01 2:35 PM Page xxv
Testing and Debugging 814
Testing and Debugging Hints 815
Summary 816
Quick Check 816
Answers 818
Exam Preparation Exercises 819
Programming Warm-Up Exercises 822
Programming Problems 823
Case Study Follow-Up 824
15 Pointers, Dynamic Data, and Reference Types 825
15.1 Pointers 826
Pointer Variables 826
Pointer Expressions 831
15.2 Dynamic Data 836
15.3 Reference Types 842
15.4 Classes and Dynamic Data 846
Class Destructors 851
Shallow Versus Deep Copying 852
Class Copy-Constructors 854
Problem-Solving Case Study: Personnel Records 857
Problem-Solving Case Study: Dynamic Arrays 872
Testing and Debugging 882
Testing and Debugging Hints 884
Summary 885
Quick Check 886
Answers 887
Exam Preparation Exercises 888
Programming Warm-Up Exercises 892
Programming Problems 893
Case Study Follow-Up 894
16 Linked Structures 897
16.1 Sequential Versus Linked Structures 898
16.2 Array Representation of a Linked List 900
xxvi | Contents
DalePhatToc 11/4/01 2:35 PM Page xxvi
16.3 Dynamic Data Representation of a Linked List 902
Algorithms on Dynamic Linked Lists 908
Pointer Expressions 926
Classes and Dynamic Linked Lists 927
16.4 Choice of Data Representation 929
Problem-Solving Case Study: Simulated Playing Cards 930
Problem-Solving Case Study: Solitaire Simulation 938
Testing and Debugging 956
Testing and Debugging Hints 956
Summary 956
Quick Check 957
Answers 957
Exam Preparation Exercises 957
Programming Warm-Up Exercises 960
Programming Problems 961
Case Study Follow-Up 962
17 Templates and Exceptions 963
17.1 Template Functions 964
Function Overloading 964
Defining a Function Template 967
Instantiating a Function Template 968
Enhancing the Print Template 969
User-Defined Specializations 970
Organization of Program Code 971
17.2 Template Classes 974
Instantiating a Class Template 976
Organization of Program Code 978
A Caution 981
17.3 Exceptions 982
The throw Statement 983
The try-catch Statement 985
Nonlocal Exception Handlers 988
Re-Throwing an Exception 991
Standard Exceptions 992
Back to the Division-by-Zero Problem 995
Contents | xxvii
DalePhatToc 11/4/01 2:35 PM Page xxvii
Problem-Solving Case Study: The SortedList Class Revisited 996
Testing and Debugging 1007
Testing and Debugging Hints 1007
Summary 1008
Quick Check 1009
Answers 1010
Exam Preparation Exercises 1011
Programming Warm-Up Exercises 1012
Programming Problems 1014
Case Study Follow-Up 1014
18 Recursion 1017
18.1 What Is Recursion? 1018
18.2 Recursive Algorithms with Simple Variables 1022
18.3 Towers of Hanoi 1025
18.4 Recursive Algorithms with Structured Variables 1030
18.5 Recursion Using Pointer Variables 1032
Printing a Dynamic Linked List in Reverse Order 1032
Copying a Dynamic Linked List 1035
18.6 Recursion or Iteration? 1040
Problem-Solving Case Study: Converting Decimal Integers to
Binary Integers 1041
Problem-Solving Case Study: Minimum Value in an
Integer Array 1044
Testing and Debugging 1046
Testing and Debugging Hints 1046
Summary 1047
Quick Check 1047
Answers 1048
Exam Preparation Exercises 1048
Programming Warm-Up Exercises 1050
Programming Problems 1052
Case Study Follow-Up 1053
Appendix A Reserved Words 1055
Appendix B Operator Precedence 1055
xxviii | Contents
DalePhatToc 11/4/01 2:35 PM Page xxviii
Appendix C A Selection of Standard Library Routines 1057
Appendix D Using This Book with a Prestandard Version of C++ 1066
Appendix E Character Sets 1071
Appendix F Program Style, Formatting, and Documentation 1073
Glossary 1081
Answers to Selected Exercises 1091
Index 1125
Contents | xxix
DalePhatToc 11/4/01 2:35 PM Page xxix
Intentional Blank
DalePhatToc 11/4/01 2:35 PM Page xxx




Library of Congress Subject Headings for this publication: