C%2B%2B book

  • View
    273

  • Download
    0

Embed Size (px)

Text of C%2B%2B book

C++ for Computer Science and Engineering(4th Edition)

Vic Broquard

Broquard eBooks 103 Timberlane East Peoria, IL 61611 author@Broquard-eBooks.com ISBN: 0-9705697-2-6

C++ for Computer Science and Engineering Vic Broquard Copyright 2000, 2002, 2003, 2006 by Vic Broquard All rights reserved. No part of this book may be reproduced or transmitted in any form without written permission of Vic Broquard. Fourth Edition ISBN: 0-9705697-2-6

Brief Table of Contents1 2 3 4 5 6 7 8 9 10 11 12 13 Introduction to Programming Numerical Processing Additional Processing Details Decisions Files and Loops Writing Your Own Functions More on Functions Character Processing and Do Case Arrays Using Arrays Strings Multidimensional Arrays Structures

Appendix A: How to Use Microsofts Visual C++ .NET 2005 Compiler Appendix B: How to Use Microsofts Visual C++ .NET 2002 Compiler Appendix C: How to Use Microsofts Visual C++ Version 6.0 Compiler Index

To all of my dedicated, persevering students, and to L. Ron Hubbard, who taught me to Simplify

PrefaceThis book assumes you have no previous programming background. It uses a step-by-step building block approach to gradiently learn how to solve computer science and engineering problems in the C++ language. Each chapter has three sections. Section A presents the basic theory and principles of the current topic. Section B illustrates these basic principles by using applications that are often found in computer science. Section C illustrates these basic principles by using applications that may be found in the various engineering disciplines. You should study the basic theory Section A and then study the appropriate application section. Of course, anyone can benefit by also reviewing the other application area, since they are frequently interrelated. The book comes with a self-extracting zip file containing all of the sample programs in the book along with all of the test data required for the programming assignments. At the end of each chapter are Design Exercises, Stop Exercises and Programming Problems. Before you tackle any programming assignments, you should do both the Design and Stop exercises. The Design Exercises are paper and pencil activities that assist in solidifying the basic design principles covered in the chapter. The Stop Exercises cover the new syntax of the language, illustrating many of the more common errors beginners make in coding the language. If you dutifully do these two sets of exercises before you start in on your programming assignments, you will have a much better chance of success with drastically lower frustration level. If you find any errors or have any suggestions or comments, please email me at author@Broquard-eBooks.com

v

ContentsChapter 1 Introduction to Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Section A: Basic Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 What is a Computer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Designing Solutions the Cycle of Data Processing . . . . . . . . . . . . . . . . . . 10 Building a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 The Steps Needed to Create a Program or . . . . . . . . . . . . . . . . . . . . . 13 How to Solve a Problem on the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Early Retirement Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 The Mechanical Robot Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 The Mechanical Mouse Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Basic Computer Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 The C++ Language and the Hello World Program . . . . . . . . . . . . . . . . . . . . 23 Design Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Stop! Do These Exercises Before Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Programming Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Chapter 2 Numerical Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Section A: Basic Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Integer Versus Floating Point (Real) Numbers . . . . . . . . . . . . . . . . . . . . . . . . 40 Which Type of Data Do You Use for Which Variable? . . . . . . . . . . . 41 Definition of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 The Issue of the Case of a Variable Name . . . . . . . . . . . . . . . . . . . . . . 44 Defining More Than One Variable in the Same Statement . . . . . . . . 44 Where Are Variable Definitions Placed in a Program? . . . . . . . . . . . 46 Initializing Variables and the Assignment Operator . . . . . . . . . . . . . . . . . . . 46 Multiple Assignments Chaining the Assignment Operator . . . . . . . . . . . 48 Input of Data Values into Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chaining Extraction Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Always Prompt the User Before Inputting the Data . . . . . . . . . . . . . . 50 Output of a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 The setw() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Insertion of Floating Point Numbers into an Output Stream - setprecision and fixed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Labeling Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Math Operators Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Precedence or Priority of Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 vi

Constant Data Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Math Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 The Most Nearly Accurate Value of PI . . . . . . . . . . . . . . . . . . . . . . . . 62 Other Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Some Additional Insertion Operator Details . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Breaking a Complex Calculation Down into Smaller Portions . . . . . . . . . . . 63 Section B: Computer Science Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Cs02a Ticket Prices for a Concert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Section C: Engineering Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Engr02a Pressure Drop in a Fluid Flowing Through a Pipe (Civil Engineering) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Design Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Stop! Do These Exercises Before Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Programming Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Chapter 3 Additional Processing Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Section A: Basic Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The Complete Integer Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Which Type of Data Do I Use in My Program? . . . . . . . . . . . . . . . . . 85 How Integer Data Is Stored in Memory . . . . . . . . . . . . . . . . . . . . . . . . 86 Integer Variable Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 The Complete Floating Point Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Principles of Data Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Assigning Smaller Sized Integers to Larger Sized Integers . . . . . . . . 90 Assigning Larger Sized Integers to Smaller Sized Integer Variables (The Typecast) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Calculations Involving Multiple Floating Point Data Types . . . . . . . 93 Mixed Mode Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Constants and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Additional Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . 98 The Compound Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . 99 Section B: Computer Science Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 CS0