ENGINEERING PROBLEM SOLVING WITH C++ Second Edition Delores M. Etter Electrical Engineering Department United States Naval Academy Jeanine A. Ingber Training Consultant Sandia National Laboratories Upper Saddle River, New Jersey 07458
Contents Preface xi Introduction to Computing and Engineering Problem Solving 2 1.1 Historical Perspective 3 1.2 Recent Engineering Achievements 7 Changing Engineering Environment 9 1.3 Computing Systems 11 Computer Hardware 11 Computer Software 12 1.4 Data Representation and Storage 16 Number Systems 17 Data Types and Storage 23 1.5 An Engineering Problem-Solving Methodology 26 Summary 29 Simple C++ Programs 34 ENGINEERING CHALLENGE: Global Change 2.1 Program Structure 35 2.2 Constants and Variables 39 Scientific Notation 41 Numeric Data Types 42 Boolean Data Type 44 Character Data Type 44 String Data 46 Symbolic Constants 47 2.3 C++ Operators 47 Assignment Operator 47 Arithmetic Operators 49 Precedence of Operators 51 Overflow and Underflow 54 Increment and Decrement Operators 55 Abbreviated Assignment Operators 55 2.4 Standard Input and Output 57 The с out Object 57 Stream Objects 58 Manipulators 60 The ein Object 63
vi Contents 2.5 Numerical Technique: Linear Interpolation 65 2.6 Problem Solving Applied: Freezing Temperature of Seawater 69 2.7 Basic Functions Included in the C++ Standard Library 73 Elementary Math Functions 74 Trigonometrie Functions 75 Hyperbolic Functions* 76 Character Functions 78 2.8 Problem Solving Applied: Velocity Computation 78 2.9 System Limitations 82 Summary 83 Control Structures 92 ENGINEERING CHALLENGE: Data Collection 3.1 Algorithm Development 93 Тор-Down Design 93 Structured Programming 94 Refinement in Pseudocode 95 Evaluation of Alternative Solutions 98 3.2 Conditional Expressions 98 Relational Operators 98 Logical Operators 99 Precedence and Associativity 101 3.3 Selection Statements 102 Simple if Statements 102 i f / e 1 s e Statement 104 switch Statement 108 3.4 Loop Structures 111 while Loop 111 do/while Loop 114 for Loop 116 break and continue Statements 120 Structuring Input Loops 121 3.5 Problem Solving Applied: Weather Balloons 125 Summary 130 Working with Data Files 138 ENGINEERING CHALLENGE: Weather Prediction 4.1 Defining File Streams 139 Stream Class Hierarchy 139 if stream Class 142 of stream Class 144 4.2 Reading Data Files 145 Specified Number of Records 146
Contents vii Trailer or Sentinel Signals 148 End-of-File 150 4.3 Generating a Data File 153 4.4 Problem Solving Applied: Data Filters Modifying an HTML File 156 4.5 Error Checking 159 The Stream State 161 4.6 Numerical Technique: Linear Modeling* 166 4.7 Problem Solving Applied: Ozone Measurements* 169 Summary 175 Modular Programming with Functions 182 ENGINEERING CHALLENGE: Enhanced Oil and Gas Recovery 5.1 Modularity 183 5.2 Programmer-Defined Functions 185 Function Definition 186 Solution 1 190 Solution 2 191 Function Prototype 194 5.3 Parameter Passing 195 Pass by Value 196 Pass by Reference 198 Storage Class and Scope 204 5.4 Problem Solving Applied: Calculating a Center of Gravity 206 5.5 Random Numbers 210 Integer Sequences 210 Floating-Point Sequences 214 5.6 Problem Solving Applied: Instrumentation Reliability 215 5.7 Numerical Technique: Roots of Polynomials* 222 Polynomial Roots 222 Incremental-Search Technique 224 5.8 Problem Solving Applied: System Stability* 226 Newton-Raphson Method* 232 5.9 Numerical Technique: Integration* 236 Integration Using the Trapezoidal Rule 236 Summary 240 One-Dimensional Arrays 250 ENGINEERING CHALLENGE: Hurricane Tracking 6.1 Arrays 251 Definition and Initialization 252 Computation and Output 256 Function Arguments 260
VIII Contents 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 Problem Solving Applied: Hurricane Categories Statistical Measurements Simple Analysis Variance and Standard Deviation Custom Header Files Problem Solving Applied: Speech Signal Analysis Sorting Algorithms Selection Sort Search Algorithms Unordered Lists Ordered Lists Character Strings С Style String Definition and I/O String Functions Problem Solving Applied: Palindromes The string Class Summary 263 269 269 271 274 274 280 281 282 283 283 285 285 288 289 293 295 Two-Dimensional Arrays and Matrices 300 ENGINEERING CHALLENGE: Terrain Navigation 7.1 Two-Dimensional Arrays 301 Declaration and Initialization 302 Computations and Output 306 Function Arguments 308 7.2 Problem Solving Applied: Terrain Navigation 311 7.3 Matrices* 316 Determinant 316 Transpose 317 Matrix Addition and Subtraction 318 Matrix Multiplication 318 7.4 Numerical Technique: Solution to Simultaneous Equations* 321 Graphical Interpretation 321 Gauss Elimination 323 7.5 Problem Solving Applied: Electrical Circuit Analysis* 327 7.6 Higher Dimensional Arrays* 333 Summary 335 An Introduction to Classes 342 ENGINEERING CHALLENGE: Simulation 8.1 Programmer Defined Types 343 8.2 Design and Implementation of Classes 345 Class Declaration 345 Class Implementation 347
Contents ix 8.3 Class Composition 353 8.4 Constructors 357 Initialization Lists 361 8.5 The vector class 364 Parameter Passing 366 8.6 Problem Solving Applied: Calculating probabilities 368 8.7 private Methods 381 Class Objects as Arguments to Member Functions 384 8.8 Problem Solving Applied: Design of Composite Materials 385 Summary 392 An Introduction to Pointers 396 ENGINEERING CHALLENGE: Weather Patterns 9.1 Addresses and Pointers 397 Address Operator 398 Pointer Assignment 400 Pointer Arithmetic 403 9.2 Pointers to Array Elements 406 One-Dimensional Arrays 407 Character Strings 409 Pointers as Function Arguments 410 9.3 Problem Solving Applied: El Nino-Southern Oscillation Data 415 9.4 Dynamic Memory Allocation 418 The new Operator 418 Dynamically Allocated Arrays 420 The delete Operator 420 9.5 Problem Solving Applied: Seismic Event Detection 422 9.6 Common Errors Using new and delete 428 9.7 Data Structures and the C++ Standard Template Library (STL) 430 The list class 430 The stack class 434 The queue class 436 9.8 Problem Solving Applied: Concordance of a Text File* 438 Summary 444 Additional Topics in Programming with Classes 450 ENGINEERING CHALLENGE: Artificial Intelligence 10.1 Introduction to Generic Programming 451 Function Templates 452 Overloading Operators 456 The pixel class 456 Arithmetic Operators 458 friend Functions 463
10.2 Problem Solving Applied: Color Image Processing 468 10.3 Recursion 475 Factorial Function 475 Fibonacci Sequence 477 Binary Trees 479 10.4 Class Templates 490 10.5 Inheritance 496 The Square class 497 The Cube class 502 10.6 virtual Methods 504 10.7 Problem Solving Applied: Iterated Prisoner's Dilemma 507 Summary 516 C++ Standard Library 521 ASCII Character Codes 529 Using MATLAB to Plot Data from ASCII Files 533 C++ Program to Generate a Data File 533 ASCII Data File Generated by the C++ Program 534 Generating a Plot with MATLAB 534 References 536 PRACTICE! Solutions 537 Index 552