C++ for Engineering and Science Developed from the author’s many years of teaching computing courses, Programming in C++ for Engineering and Science guides readers in designing programs to solve real problems encountered in engineering and scientific applications These problems include radioactive decay, pollution indexes, digital circuits, differential equations, Internet addresses, data analysis, simulation, quality control, electrical networks, data encryption, beam deflection, and many other areas Nyhoff Features • Uses standard C++ throughout • Explains key concepts, such as functions and classes, through a “use it first, build it later” approach • Shows how to develop programs to solve real problems, emphasizing the proper techniques of design and style • Introduces the very powerful and useful Standard Template Library along with important class and function templates • Develops numeric techniques and programs for some engineering and science example problems • Highlights key terms, important points, design and style suggestions, and common programming pitfalls in the chapter summaries • Includes self-study questions and programming projects in each chapter • Provides ancillary materials on the book’s website Programming in C++ for Engineering and Science C++ To make it easier for novices to develop programs, the author uses an object-centered design approach that helps readers identify the objects in a problem and the operations needed; develop an algorithm for processing; implement the objects, operations, and algorithm in a program; and test, correct, and revise the program He also revisits topics in greater detail as the text progresses By the end of the book, readers will have a solid understanding of how C++ can be used to process complex objects, including how classes can be built to model objects Programming in Programming in for Engineering and Science Computer Science Larry Nyhoff K11207 K11207_Cover.indd 6/19/12 9:01 AM Programming in C++ for Engineering and Science K11207.indb 6/15/12 10:06 AM This page intentionally left blank Programming in C++ for Engineering and Science Larry Nyhoff K11207.indb 6/15/12 10:06 AM CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2012 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S Government works Version Date: 20120409 International Standard Book Number-13: 978-1-4398-2535-8 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint Except as permitted under U.S Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400 CCC is a not-for-profit organization that provides licenses and registration for a variety of users For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents Preface, vii Acknowledgments, xi About the Author, xiii Chapter ■ Introduction to Computing Chapter ■ Programming and Problem Solving— Software Engineering 21 Chapter ■ Types in C++ 39 Chapter ■ Getting Started with Expressions 63 Chapter ■ Control Structures 107 Chapter ■ Functions and Libraries 149 Chapter ■ Using Classes 207 Chapter ■ More Selection Control Structures 261 Chapter ■ More Repetition Control Structures 295 Chapter 10 ■ Functions in Depth 351 Chapter 11 ■ Files and Streams 409 Chapter 12 ■ Arrays and the vector Class Template 451 Chapter 13 ■ Multidimensional Arrays and Vectors 503 v vi ◾ Contents Chapter 14 ■ B uilding Classes 553 Chapter 15 ■ Pointers and Linked Structures 593 Chapter 16 ■ D ata Structures 637 Answers to Test Yourself Questions, 677 Appendix A: ASCII Character Codes, 693 Appendix B: C++ Keywords, 697 Appendix C: C++ Operators, 699 Appendix D: Other C++ Features, 701 Index, 715 K11207.indb 6/15/12 10:06 AM Preface C ++ is a general-purpose programming language that has both high-level and low-level language features Bjarne Stroustrup developed it in 1979 at Bell Labs as a series of enhancements to the C programming language, which, although developed for system programming, has been used increasingly in engineering and scientific applications Because the first enhancement was the addition of classes, the resulting language was originally named Bjarne Stroustrup “C with Classes,” but was renamed C++ in 1983 Along with overcoming some of the dangers and disadvantages of C, these and subsequent enhancements have resulted in a very powerful language in which very efficient programs can be written and developed using the object-oriented paradigm A programming language standard for C++ (ISO/IEC148821998) was adopted in 1998 and revised in 2003 and is the basis for this text Background and Content This text grew out of many years of teaching courses in computing, including programming courses intended for students majoring in engineering and science Although the Fortran language was first used, these courses are now taught using C++ However, most C++ textbooks are written for the general college student and thus include examples and some content that is not aimed at or especially relevant to science and engineering students In this text, nearly all of the examples and exercises involve engineering and scientific applications, including the following (and many more): • Temperature conversion • Radioactive decay • Einstein’s equation • Pollution indexes • Digital circuits • Root finding, integration, differential equations vii viii ◾ Preface • Internet addresses • A-C circuits • Simulation • Quality control • Street networks • Environmental data analysis • Searching a chemistry database • Oceanographic data analysis • Electrical networks • Coordinate transformations • Data encryption • Beam deflection • Weather data analysis • Oceanographic data analysis Some examples are described and solved in detail, while for others the presentation in the text outlines the solution and the complete development is available on the text’s website maintained by the author: http://cs.calvin.edu/books/c++/engr-sci This text also focuses on those features of C++ that are most important in engineering and science applications, with other features described in optional sections, appendices, or on the text’s website This makes it useable in a variety of courses ranging from a regular full-credit course to one with reduced credit such as a two-credit course that the author has taught many times, where the class lectures are supplemented by lab exercises—tutorial in nature—in which the students develop a program to solve some problem using the new language features presented in class Presentation The basic approach of the text is a spiral approach that revisits topics in increasingly more detail For example, the basic C++ operations used to build expressions are presented first, and then predefined functions provided in C++ libraries are added Once students have experience with functions, they learn how to define their own simple functions and then more complicated ones Later they learn how to incorporate these into libraries of their own, thus extending the C++ language with custom-designed libraries K11207.indb 6/15/12 10:06 AM Preface ◾ ix Learning how to develop a program from scratch, however, can be a difficult and challenging task for novice programmers A methodology used in this text for designing programs to solve problems, developed over years of teaching C++ to computer science, engineering, and science students and coauthoring texts in C++, is called object-centered design (OCD): • Identify the objects in the problem that need to be processed • Identify the operations needed to this processing • Develop an algorithm for this processing • Implement these objects, operations, and algorithm in a program • Test, correct, and revise the program Although this approach cannot technically be called object-oriented design (OOD), it does focus on the objects and operations on these objects in a problem As new language constructs are learned, they are incorporated into the design process For example, simple types of objects are used in early chapters, but Chapter introduces students to some of the standard classes provided in C++ for processing more complex objects—those that have multiple attributes In subsequent chapters, more classes are introduced and explained, and students gain more practice in using them and understanding the structure of a class Once they have a good understanding of these predefined standard classes, in Chapter 14 they learn how to build their own classes to model objects, thus extending the C++ language to include a new custom-built type Important Features • Standard C++ is used throughout • A “use it first—build it later” approach is used for key concepts such as functions (use predefined functions first, build functions later) and classes (use predefined classes first, build classes later) Various other topics are similarly introduced early and used, and are expanded later—a spiral kind of approach • The very powerful and useful Standard Template Library (STL) is introduced and some of the important class templates (e.g., vector) and function templates (e.g., sort()) are presented in detail • C++’s language features that are not provided in C are noted • Engineering and science examples, including numeric techniques, are emphasized • Programs for some examples are developed in detail; for others, the design of a program is outlined and a complete development is available on the text’s website • Object-centered design (OCD) helps students develop programs to solve problems • Proper techniques of design and style are emphasized and used throughout K11207.indb 6/15/12 10:06 AM .. .Programming in C++ for Engineering and Science K11207.indb 6/15/12 10:06 AM This page intentionally left blank Programming in C++ for Engineering and Science Larry Nyhoff K11207.indb 6/15/12... simpler and more intuitive interface between humans and computers K11207.indb 11 6/15/12 10:06 AM 12 ◾ Programming in C++ for Engineering and Science? ?? As noted in the introduction to this... ◾ Programming in C++ for Engineering and Science? ?? Finding and fixing such errors is known as debugging the program The compiler will locate syntax errors when it attempts to translate the C++