ELEMENTARY NUMERICAL METHODS AND C++ Melvin R. Corley, Ph.D., P.E. Louisiana Tech University Ruston, LA 71272
[email protected] November 2007 Edition © 2000-2007 by Melvin R. Corley All Rights Reserved.
Table of Contents Computers for Engineers and Scientists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Computers in Science and Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Purpose of Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Quick Start: Your First C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 1 1 2 3 5 5
C++ Fundamentals: Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Representing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Floating Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 C++ Fundamentals: Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Multi-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Chapter 3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Fundamentals of C++: Basic Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 The cout stream object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 The cin stream object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 The cerr stream object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Standard stream redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 User-Defined Stream Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Chapter 4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Fundamentals of C++: Programming Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Programming Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 When Additional Work Is To Be Done . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Two Mutually Exclusive, Collectively Exhaustive Alternatives . . . . . . . . . . . . 28 Three Or More Mutually Exclusive Alternatives . . . . . . . . . . . . . . . . . . . . . . . 28 Three Or More Mutually Exclusive, Collectively Exhaustive Alternatives . . . 28 Special Case: Exact Matches Of Integral Options . . . . . . . . . . . . . . . . . . . . . . . 29 Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 When The Number Of Iterations Is Known In Advance . . . . . . . . . . . . . . . . . . 30 When The Number Of Iterations Is Not Known In Advance . . . . . . . . . . . . . . 31
iv # Contents Pre-Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Post-Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32 33 34 34
Fundamentals of C++: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Eliminating Repetitive Code Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Algorithmic Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 By Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 By Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Some Numerical Programming Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Errors in Numerical Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Truncation Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Roundoff Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Calculating Machine Epsilon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chapter 7 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Zeros of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Incremental Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Bisection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Regula Falsi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Improved Regula Falsi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Newton-Raphson Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter 8 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Using C++ Classes in Numerical Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 “Thinking” Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Member Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 “Set”/“Get” Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Overloaded Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 The ENM Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Vector and Matrix classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Miscellaneous ENM classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Chapter 9 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Contents # v Simultaneous Algebraic Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Linear Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Diagonal Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Triangular Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Gauss Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Partial Pivoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Full Pivoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Tridiagonal Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 LU Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Crout’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Doolittle’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Gauss-Seidel Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Nonlinear Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Chapter 10 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Polynomial Evaluation Using Horner’s Method . . . . . . . . . . . . . . . . . . . . . . . 120 Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Least-Squares approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Chapter 11 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Numerical Quadrature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Newton-Cotes Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Simpson’s One-Third Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Simpson’s Three-Eighth’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Romberg Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Chapter 12 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Ordinary Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 First Order ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Working With Higher Order ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Initial Condition Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Taylor Series Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Euler’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Runge-Kutta Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Second Order Runge-Kutta Methods . . . . . . . . . . . . . . . . . . . . . . . . . 161 Fourth Order Runge-Kutta Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Sets of Simultaneous ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 RKSUITE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
vi # Contents Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Chapter 13 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Table of C++ Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
CHAPTER
1
Computers for Engineers and Scientists CHAPTER OUTLINE 1.1 Introduction 1.2 Computers in Science and Engineering 1.3 The Purpose of Computing
1.1
INTRODUCTION
Digital computers have reached near-commodity status in many societies around the world. While not yet viewed in exactly the same way as other common appliances such as the television and the telephone, computers have, in some ways, affected our lives in even more pervasive ways. For example, computers now run our automobiles and transportation systems, our microwave ovens and household appliances, our audio/video entertainment systems, virtually all of our medical instrumentation, as well as most factories. So, while computers may not be as visible as other appliances, they are there, making the systems around us work. These computers that surround us take on many shapes and sizes and functions. And though they may be radically different in appearance and function, they all share some similar properties. For example, they all do their required functions by executing programs stored in some type of memory device. For the most part, these programs are conceived and developed by human programmers. The effectiveness of a computer being used in a particular application is strongly dependent on the quality of the program that it is executing. And the more we, as users of these miraculous devices, are aware of their internal operation and limitations, the more effectively we will be able to harness their largely untapped power to serve us in applications yet unseen.
1.2
COMPUTERS IN SCIENCE AND ENGINEERING
Practitioners in the fields of science and engineering were among the early adopters of computer
2 # Chapter 1 Computers for Engineers and Scientists technology. For example, the pioneering work of Raimondi and Boyd[1] led to the solution of the differential equations for fluid flow in the hydrodynamic bearing. This remarkable work was conducted in the early pioneer days of computers using very primitive equipment and programming tools, yet has remained the standard solution method used in bearing design to this day. Today’s scientists and engineers have a vast array of computer tools to select from. Hardware choices range from handheld devices to desktop personal computers that are much more powerful than the largest mainframes of a generation ago, to scientific workstations that are more powerful still, to supercomputers whose computation speeds are mind-numbing. Similarly, a wide array of software tools are available to select from. Many problems today are solved using application programs developed specifically for virtually any field of practice. Other more general purpose electronic toolkits are available to handle more general purpose technical problem-solving needs. These programs allow the user to select from a large array of mathematical functions and to combine these in an unlimited number of ways to obtain numerical or, in some cases, symbolic solutions to complicated problems. Finally, there are the general purpose programming languages such as FORTRAN, C++, and JAVA. It is with these general purpose programming languages that the previous two categories of software tools are constructed. There is value in developing skill in the use of tools in each of the three classes identified above. But there are several reasons that argue for those in technical fields to have some significant experience in the use of general purpose programming languages for solving technical problems: • Modern general purpose programming languages cause programmers to give careful consideration to the various data types they employ in their programs and the inherent properties of each type. • Integrated development environments provide convenient debugging environments offering the user the opportunity to develop the skills required to detect, isolate, and correct logic flaws in programs. These skills seem to be transferable to other areas of endeavor and will serve the learner well in future experiences with debugging complicated systems. • The fundamental computer operations of sequence, selection, and repetition are very visible in general purpose programming languages. Learning these fundamental constructions early reduces the effort required to learn new computing languages and application commands in the future. • Using a general purpose programming language reduces the “fog” that often accompanies using higher level application environments. It removes one level of obfuscation in that when using a general purpose programming language, we may ask “Why did the program do that?” and the answer will invariably be “Because you told it to do that!” instead of having to wonder how the underlying software in our application environment interpreted and implemented our command.
1.3
THE PURPOSE OF COMPUTING
In one of the early standard textbooks, R.W. Hamming offered this time honored reminder to engineers and scientists who use the computer in their work, “The purpose of computing is insight, not numbers.”1 This quote can be the springboard for launching a phalanx of warnings and
1
R.W. Hamming. Numerical Methods for Scientists and Engineers, McGraw-Hill, 1962.
Elementary Numerical Methods and C++ # 3 admonitions (usually learned by personal experience) regarding the dangers of getting so involved in the numbers we are computing that we may lose sight of the problem we are actually trying to solve. Anybody can generate a lot of numbers using a computer. It takes a skilled practitioner to realize solutions and insight using a computer. Never let the computer replace your brain. Always challenge the numbers your computer programs generate. Be skeptical. Only after careful analysis and judgement should you begin to really believe the numbers your programs generate represent provide insight into the problems you are solving. Therefore, we prepare now to begin a study that seeks to provide the technical student with an introduction to the bare essentials of numerical problem solving in the context of the bare essentials of the C++ general purpose programming environment.
1.4
QUICK START: YOUR FIRST C++ PROGRAM
Traditionally, the first program that one writes in C++ is a simple program that prints the worlds Hello world on the computer console. The rationale is that to create, compile, link and execute even a simple program requires a basic knowledge of these steps which are essential to the successful creation of more substantial programs in the future. Gaining a “quick victory” in accomplishing this task will give the learner encouragement to proceed further at a quick pace. The “Hello world” program is shown in the following example. The steps required to create, compile, link and execute the program vary extensively depending on which C++ compiler, integrated development environment, and operating system is being used. O EXAMPLE 1.1 “Hello World” C++ Program “Hello World” is a very brief C++ program that simply displays the words Hello world on the computer console. The line numbers shown at the beginning of each line are not part of the program, but are used for reference in the discussion below the program listing.
1) 2) 3) 4) 5) 6)
Column Numbers 1 2 3 123456789012345678901234567890 #include using namespace std; int main(void) { cout 0); 65) Subscript dorder = n_-2; 66) Polynomial tmp(dorder); 67) for (Subscript i=0; iv_[i+1]*(i+1); 70) } 71) return tmp; 72) } 73) }; 74) template 75) Polynomial operator+(const Polynomial& A, const Polynomial& B) 76) { 77) Subscript i=0; 78) Subscript M=A.order(); 79) Subscript N=B.order(); 80) Polynomial tmp(max(M, N)); 81) Subscript L=min(M, N); 82) for (;i