C++ Programming for Computer Science and Engineering – Long Table of Contents

C++ Programming for Computer Science and Engineering – Long Table of Contents

Back to previous book promo page(With page numbers)

Chapter 1 Introduction to Programming. . . . . . . . . . . . .1
Section A: Basic Theory . . . . . . . . . . . . . . . . . .1
Introduction . . . . . . . . . . . . . . . . . . . . .1
What is a Computer?. . . . . . . . . . . . . . . . . .1
Designing Solutions the Cycle of Data Processing . .4
Building a Program . . . . . . . . . . . . . . . . . .5
The Steps Needed to Create a Program or . . . . . .7
How to Solve a Problem on the Computer . . . . . . . .7
The Early Retirement Program . . . . . . . . . . . . 11
The Mechanical Robot Problem . . . . . . . . . . . . 13
The Mechanical Mouse Problem . . . . . . . . . . . . 14
Basic Computer Architecture. . . . . . . . . . . . . 15
The C++ Language and the Hello World Program . . . . 17
Design Exercises. . . . . . . . . . . . . . . . . . . . . 26
Stop! Do These Exercises Before Programming . . . . . . . 27
Programming Problems. . . . . . . . . . . . . . . . . . . 31

Chapter 2 Numerical Processing . . . . . . . . . . . . . . . 34
Section A: Basic Theory . . . . . . . . . . . . . . . . . 34
Introduction . . . . . . . . . . . . . . . . . . . . 34
Variables and Constants. . . . . . . . . . . . . . . 34
Integer Versus Floating Point (Real) Numbers . . . . 34
Which Type of Data Do You Use for Which Variable? 35
Definition of Variables. . . . . . . . . . . . . . . 36
The Issue of the Case of a Variable Name. . . . 38
Defining More Than One Variable in the Same Statement 38
Where Are Variable Definitions Placed in a Program? 40
Initializing Variables and the Assignment Operator . 40
Multiple Assignments Chaining the Assignment Operator 42
Input of Data Values into Variables. . . . . . . . . 43
Chaining Extraction Operators . . . . . . . . . 43
Always Prompt the User Before Inputting the Data 44
Output of a Variable . . . . . . . . . . . . . . . . 45
The setw() Function . . . . . . . . . . . . . . 47
Labeling Output. . . . . . . . . . . . . . . . . . . 47
Math Operators Calculations. . . . . . . . . . . . 48
Precedence or Priority of Operators. . . . . . . . . 49
Constant Data Objects. . . . . . . . . . . . . . . . 50
Math Library Functions . . . . . . . . . . . . . . . 52
The Most Accurate Value of PI . . . . . . . . . 54
Other Math Functions. . . . . . . . . . . . . . 54
Some Additional Insertion Operator Details . . . . . 54
Breaking a Complex Calculation Down into Smaller Portions 55
Insertion of Floating Point Numbers into an Output Stream 56
Section B: Computer Science Example . . . . . . . . . . . 59
Cs02a Ticket Prices for a Concert. . . . . . . . . 59
Section C: Engineering Example. . . . . . . . . . . . . . 63
Engr02a Pressure Drop in a Fluid Flowing Through a Pipe(Civil
Engineering) 63
New Syntax Summary. . . . . . . . . . . . . . . . . . . . 67
Design Exercises. . . . . . . . . . . . . . . . . . . . . 69
Stop! Do These Exercises Before Programming . . . . . . . 70
Programming Problems. . . . . . . . . . . . . . . . . . . 74

Chapter 3 Additional Processing Details. . . . . . . . . . . 75
Section A: Basic Theory . . . . . . . . . . . . . . . . . 75
Introduction . . . . . . . . . . . . . . . . . . . . 75
The Complete Integer Data Types. . . . . . . . . . . 75
Which Type of Data Do I Use in My Program?. . . 76
How Integer Data Is Stored in Memory. . . . . . 77
Integer Variable Overflow . . . . . . . . . . . 78
The Complete Floating Point Data Types . . . . . . . 79
Principles of Data Conversion. . . . . . . . . . . . 80
Assigning Smaller Sized Integers to Larger Sized Integers 81
Assigning Larger Sized Integers to Smaller Sized Integer Variables
(The Typecast). . . . . . . . . . . . . . . . . . . . . . . 82
Calculations Involving Multiple Floating Point Data Types 84
Mixed Mode Math. . . . . . . . . . . . . . . . . . . 85
Constants and Data Types . . . . . . . . . . . . . . 86
Additional Operators . . . . . . . . . . . . . . . . 88
The Increment and Decrement Operators . . . . . 88
The Compound Assignment Operators . . . . . . . 89
Section B: Computer Science Examples. . . . . . . . . . . 91
Cs03a Vote Tally Program . . . . . . . . . . . . . 91
Section C: An Engineering Example . . . . . . . . . . . . 93
Engr03a Calculating the Power Supplied to a Load Electrica
Engineering . . . . . . . . . . . . . . . . . . . . . . . . 93
New Syntax Summary. . . . . . . . . . . . . . . . . . . . 98
Design Exercises. . . . . . . . . . . . . . . . . . . . . 99
Stop! Do These Exercises Before Programming . . . . . . . 99
Programming Problems. . . . . . . . . . . . . . . . . . .101

Chapter 4 Decisions. . . . . . . . . . . . . . . . . . . . .105
Section A: Basic Theory . . . . . . . . . . . . . . . . .105
Introduction . . . . . . . . . . . . . . . . . . . .105
The Components of an If-Then-Else Decision Structure 105
The If-Then-Else Syntax . . . . . . . . . . . .105
The Test Condition. . . . . . . . . . . . . . .107
Nested Decisions. . . . . . . . . . . . . . . .109
Compound Test Conditions. . . . . . . . . . . .113
The Logical Not Operator !. . . . . . . . . .115
Data Type and Value of Relational Expressions The bool Data Type 116
The bool Data Type. . . . . . . . . . . . . . .116
The Most Common Test Condition Blunder Explained . .118
The Conditional Expression . . . . . . . . . . . . .119
The Precedence of Operators. . . . . . . . . . . . .120
Testing of Real Numbers. . . . . . . . . . . . . . .121
Section B: Computer Science Example . . . . . . . . . . .122
Cs04a Finding the Sales Tax Rate . . . . . . . . .122
Section C: An Engineering Example . . . . . . . . . . . .127
Engr04a Quadratic Root Solver. . . . . . . . . . .127
New Syntax Summary. . . . . . . . . . . . . . . . . . . .131
Design Exercises. . . . . . . . . . . . . . . . . . . . .133
Stop! Do These Exercises Before Programming . . . . . . .134
Programming Problems. . . . . . . . . . . . . . . . . . .137

Chapter 5 Files and Loops. . . . . . . . . . . . . . . . . .141
Section A: Basic Theory . . . . . . . . . . . . . . . . .141
Introduction . . . . . . . . . . . . . . . . . . . .141
Input Files. . . . . . . . . . . . . . . . . . . . .142
I/O Stream States . . . . . . . . . . . . . . .144
Testing for Goodness. . . . . . . . . . . . . .144
Testing for Bad Data Entry. . . . . . . . . . .145
The End of File . . . . . . . . . . . . . . . .147
Closing a File. . . . . . . . . . . . . . . . .147
The Iterative Instructions . . . . . . . . . . . . .149
Loops That Are to Be Executed a Known Number of Times 150
Loops to Input All Data in a File. . . . . . . . . .151
Sentinel Controlled Input Loops . . . . . . . .151
Keyboard Data Entry Sentinel Controlled Loops .153
Menus as Sentinel Controlled Loops. . . . . . .153
Primed Input Loops that Detect End of File. . .154
A More Compact Loop That Detects End of File. .155
Applications of Loops. . . . . . . . . . . . . . . .156
Application: The Summation of a Series. . . . .156
Counters and Totals Grand Totals. . . . . . .157
Finding the Maximum and Minimum Values. . . . .158
Bulletproofing Programs . . . . . . . . . . . .163
Creating Output Files. . . . . . . . . . . . . . . .164
The Do Until Instruction An Alternative to the Do While 168
The Do Loop or for Statement . . . . . . . . . . . .169
Efficient Loops. . . . . . . . . . . . . . . . . . .173
Nesting of Loops . . . . . . . . . . . . . . . . . .173
An Example of Nested Loops. . . . . . . . . . .175
Section B: Computer Science Examples. . . . . . . . . . .176
Cs05a Acme Ticket Sales Summary Program. . . . . .176
Cs05b Calculating N! (N factorial) . . . . . . . .181
Section C: Engineering Examples . . . . . . . . . . . . .184
Engr05a Summation of Infinite Polynomials. . . . .184
Engr05b Artillery Shell Trajectory . . . . . . . .188
New Syntax Summary. . . . . . . . . . . . . . . . . . . .193
Design Exercises. . . . . . . . . . . . . . . . . . . . .196
Stop! Do These Exercises Before Programming . . . . . . .197
Programming Problems. . . . . . . . . . . . . . . . . . .203

Chapter 6 Writing Your Own Functions . . . . . . . . . . . .207
Section A: Basic Theory . . . . . . . . . . . . . . . . .207
Introduction . . . . . . . . . . . . . . . . . . . .207
Principles of Top-Down Design. . . . . . . . . . . .208
Writing your Own Functions . . . . . . . . . . . . .211
Step A. Define the Function’s Prototype . . . .212
Step B. Define the Function Header. . . . . . .214
Step C. Code the Function’s Body. . . . . . . .216
Step D. Invoke or Call the Function.. . . . . .217
A Second Example, calcTax() . . . . . . . . . .219
How Parameters Are Passed to Functions . . . . . . .222
The Types, Scope and Storage Classes of Variables. .224
Registers and the Stack a Bit of Computer Architecture 227
How a Function Returns a Value. . . . . . . . .227
More on the bool Data Type and Functions that Return a bool 231
Functions that Return No Values. . . . . . . . . . .232
Where Should Error Messages Be Displayed?. . . . . .233
Controlling Leading 0’s on Output The setfill() Function 234
Inputting Integers That Have Leading Zeros The dec Manipulator
Function . . . . . . . . . . . . . . . . . . . . . . . . . .234
Section B: Computer Science Example . . . . . . . . . . .236
Cs06a Employee Payroll Program . . . . . . . . . .236
Section C: An Engineering Example . . . . . . . . . . . .241
Introduction to Numerical Analysis . . . . . . . . .241
Numerical Analysis: Root Solving, the Bisection Method 243
Engr06a Root Solving, the Bisection Method . . . .246
New Syntax Summary. . . . . . . . . . . . . . . . . . . .252
Design Exercises. . . . . . . . . . . . . . . . . . . . .253
Stop! Do These Exercises Before Programming . . . . . . .254
Programming Problems. . . . . . . . . . . . . . . . . . .258

Chapter 7 More on Functions. . . . . . . . . . . . . . . . .264
Section A: Basic Theory . . . . . . . . . . . . . . . . .264
Introduction . . . . . . . . . . . . . . . . . . . .264
Reference Variables. . . . . . . . . . . . . . . . .264
The Need for Reference Variables. . . . . . . .264
The Reference Variable Solution . . . . . . . .269
The Static Storage Class . . . . . . . . . . . . . .273
The Global/External Storage Class. . . . . . . . . .276
Using Global Variables in Other Cpp Files – the extern Keyword 279
Where are Global and Static Variables Actually Stored? 280
Philosophy on the Use of Global Variables. . . . . .281
How to Pass iostreams to Functions . . . . . . . . .282
Section B: Computer Science Examples. . . . . . . . . . .285
Cs07c Acme Ticket Sales Report a Multi-page Report 285
Cs07a Multiple Level Control Break Processing. . .292
Cs07b Summary Reports Based upon Control Break Processing 300
Section C: Engineering Examples . . . . . . . . . . . . .303
Bisection Revisited Writing a Generic Bisection Function 303
Engr07a Using a Generic bisect() Function. . . . .305
Engr07b Molar Volume of Non-Ideal Gases Chemical Engineering 308
Faster Alternative Root Solving Methods. . . . . . .312
The Regula Falsi Root Solving Method . . . . . . . .313
Engr07c Molar Volume of Non-Ideal Gases Using Regula Falsi
Method. . . . . . . . . . . . . . . . . . . . . . . . .313
Newton’s Method of Root Solving. . . . . . . . . . .317
Engr07d Molar Volume of Non-Ideal Gases Using Newton’s Method 319
The Secant Method of Root Solving. . . . . . . . . .324
Engr07e Molar Volume of Non-Ideal Gases Using the Secant
Method . . . . . . . . . . . . . . . . . . . . . . . .325
Summary of Root Solving Techniques . . . . . . . . .329
New Syntax Summary. . . . . . . . . . . . . . . . . . . .330
Design Exercises. . . . . . . . . . . . . . . . . . . . .331
Stop! Do These Exercises Before Programming . . . . . . .331
Programming Problems. . . . . . . . . . . . . . . . . . .337

Chapter 8 Character Processing and Do Case . . . . . . . . .344
Section A: Basic Theory . . . . . . . . . . . . . . . . .344
Introduction . . . . . . . . . . . . . . . . . . . .344
The Processing of Character Data . . . . . . . . . .344
Defining Variables to Hold a Character of Data. 344
Inputting Character Data. . . . . . . . . . . .345
Using the Extraction Operator to Input a Character 345
Hexadecimal Numbers . . . . . . . . . . . . . .346
Using the get() Function. . . . . . . . . . . .347
Output of Character Data The put() Function .348
How Are Character Data Stored?. . . . . . . . .349
The Escape Sequences. . . . . . . . . . . . . .351
Numbers and Letters . . . . . . . . . . . . . .352
The Character Processing Functions. . . . . . .354
Basic08a A Word Counter Program. . . . . . . . . .354
The Do Case Structure. . . . . . . . . . . . . . . .358
More on the break Statement and the continue Statement 363
Enumerated Data Types. . . . . . . . . . . . . . . .364
Aborting the Program . . . . . . . . . . . . . . . .370
Section B: Computer Science Examples. . . . . . . . . . .371
Cs08a Inventory on Hand Program. . . . . . . . . .371
Cs08b Inventory on Hand Program Using a Generic processFile()378
Section C: Engineering Examples Numerical Integration . 384
The Trapezoid Method of Numerical Integration. . . .384
Engr08a Numerical Integration with the Trapezoid Rule 387
Integration Using Simpson’s Rule . . . . . . . . . .389
Engr08b Numerical Integration with Simpson’s Rule. 390
Engr08c Using Menus to Control Program Operation . 392
New Syntax Summary. . . . . . . . . . . . . . . . . . . .396
Design Exercises. . . . . . . . . . . . . . . . . . . . .398
Stop! Do These Exercises Before Programming . . . . . . .399
Programming Problems. . . . . . . . . . . . . . . . . . .402

Chapter 9 Arrays . . . . . . . . . . . . . . . . . . . . . .408
Section A: Basic Theory . . . . . . . . . . . . . . . . .408
Definitions and Need for Arrays. . . . . . . . . . .408
Defining Arrays. . . . . . . . . . . . . . . . . . .408
Accessing Array Elements . . . . . . . . . . . . . .409
Methods of Inputting Data into an Array. . . . . . .411
Method A: Inputting a Known Number of Elements. 411
Method B: Inputting the Number of Array Elements To Be
Input . . . . . . . . . . . . . . . . . . . . . . . . . .412
Method C: Inputting an Unknown Number of Elements Until EOF Is
Reached. . . . . . . . . . . . . . . . . . . . . . . . . . . .413
Working with Arrays the Calculations . . . . . . .414
Working with Arrays: the Output Process. . . . . . .416
Initializing an Array. . . . . . . . . . . . . . . .416
Passing Arrays to Functions. . . . . . . . . . . . .416
Section B: Computer Science Examples. . . . . . . . . . .423
Cs09a Sales Data Analysis. . . . . . . . . . . . .423
Section C: Engineering Examples . . . . . . . . . . . . .431
Engr09a Vector Coordinate Conversions. . . . . . .431
Engr09b Plotting Graphs. . . . . . . . . . . . . .434
Design Exercises. . . . . . . . . . . . . . . . . . . . .441
Stop! Do These Exercises Before Programming . . . . . . .442
Programming Problems. . . . . . . . . . . . . . . . . . .446

Chapter 10 Using Arrays. . . . . . . . . . . . . . . . . . .451
Section A: Basic Theory . . . . . . . . . . . . . . . . .451
Introduction . . . . . . . . . . . . . . . . . . . .451
Using an Array for Direct Lookup Operations. . . . .451
Parallel Arrays and Sequential Searches Inquiry Programs 452
Inserting Another Element into an Unsorted Array . .454
Ordered (Sorted) Lists . . . . . . . . . . . . . . .455
Inserting New Data into a Sorted List. . . . . . . .457
Sorting an Array . . . . . . . . . . . . . . . . . .458
Section B: A Computer Science Example . . . . . . . . . .460
Cs10b Account Processing . . . . . . . . . . . . .460
Cs10a Merging Arrays . . . . . . . . . . . . . . .469
Section C: An Engineering Example . . . . . . . . . . . .481
Engr10a Statistical Computations . . . . . . . . .481
Least Squares Curve Fitting. . . . . . . . . . . . .485
Design Exercises. . . . . . . . . . . . . . . . . . . . .487
Stop! Do These Exercises Before Programming . . . . . . .489
Programming Problems. . . . . . . . . . . . . . . . . . .491

Chapter 11 Strings . . . . . . . . . . . . . . . . . . . . .499
Section A: Basic Theory . . . . . . . . . . . . . . . . .499
Defining Character Strings . . . . . . . . . . . . .499
Inputting Character Strings. . . . . . . . . . . . .500
Using the Extraction Operator . . . . . . . . .500
Method A All Strings Have the Same Length . .501
Method B String Contains Only the Needed Characters, But Is the Last
Field on a Line. . . . . . . . . . . . . . . . . . . . . . . .504
Method C All strings Are Delimited. . . . . .504
Outputting Character Strings . . . . . . . . . . . .505
Passing a String to a Function . . . . . . . . . . .506
Working with Strings . . . . . . . . . . . . . . . .506
The String Functions. . . . . . . . . . . . . .509
How Could String Functions Be Implemented in the Standard
Library? . . . . . . . . . . . . . . . . . . . . . . . . . . .511
Section B: A Computer Science Example . . . . . . . . . .512
Cs11a Character String Manipulation – Customer Names 512
Section C: An Engineering Example . . . . . . . . . . . .521
Engr11a Weather Statistics Revisited . . . . . . .521
Design Exercises. . . . . . . . . . . . . . . . . . . . .525
Stop! Do These Exercises Before Programming . . . . . . .526
Programming Problems. . . . . . . . . . . . . . . . . . .528

Chapter 12 Multidimensional Arrays . . . . . . . . . . . . .533
Section A: Basic Theory . . . . . . . . . . . . . . . . .533
Introduction . . . . . . . . . . . . . . . . . . . .533
Defining Multidimensional Arrays . . . . . . . . . .533
Physical Memory Layout Versus Logical Layout . . . .535
Initialization of Multidimensional Arrays. . . . . .536
Passing Multidimensional Arrays to Functions . . . .537
Loading a Multidimensional Array from an Input File.537
Working with Multidimensional Arrays . . . . . . . .539
Some More Examples of Array Processing . . . . . . .544
Section B: A Computer Science Example . . . . . . . . . .544
Cs12a Arrays of Strings. . . . . . . . . . . . . .544
Section C: Engineering Examples . . . . . . . . . . . . .552
Matrix Algebra . . . . . . . . . . . . . . . . . . .552
Matrix Math Operations Summary. . . . . . . . .553
Mathematical Theorems of Determinants . . . . .556
The Gauss Method for Solving a System of Linear Equations 556
Gauss-Jordan Method of Solving Simultaneous Linear Equations 558
Engr12a Aligning the Mirrors of a Telescope Astronomy 563
Design Exercises. . . . . . . . . . . . . . . . . . . . .565
Stop! Do These Exercises Before Programming . . . . . . .566
Programming Problems. . . . . . . . . . . . . . . . . . .569

Chapter 13 Structures. . . . . . . . . . . . . . . . . . . .576
Section A: Basic Theory . . . . . . . . . . . . . . . . .576
Introduction . . . . . . . . . . . . . . . . . . . .576
Structures . . . . . . . . . . . . . . . . . . . . .576
Defining Structures . . . . . . . . . . . . . .577
Creating Instances of a Structure . . . . . . .578
How are Structures Initialized? . . . . . . . .580
How are Structure Members Accessed? . . . . . .580
Rules of Use for Structure Variables. . . . . .581
User Written Header Files. . . . . . . . . . . . . .584
Binary Files and Structures. . . . . . . . . . . . .585
Mechanics of Binary Files . . . . . . . . . . .586
Section B: Computer Science Examples. . . . . . . . . . .588
Cs13a Credit Card Application with Sorting . . . .588
Cs13b Writing a Binary File. . . . . . . . . . . .597
Cs13c Reading a Binary File -. . . . . . . . . . .600
Section C: An Engineering Example . . . . . . . . . . . .604
Engr13a Weather Statistics Revisited . . . . . . .604
Design Exercises. . . . . . . . . . . . . . . . . . . . .609
Stop! Do These Exercises Before Programming . . . . . . .610
Programming Problems. . . . . . . . . . . . . . . . . . .612

Appendix A: How to Use Microsoft’s (.NET) Visual C++ 7.0
Compiler 618
Appendix B: How to Use Microsoft’s Visual C++ 6.0 Compiler . .641
Back to previous book promo page

Share Button