Programming Fundamentals - A Modular Structured Approach Using C++ PDF
Programming Fundamentals - A Modular Structured Approach Using C++ PDF
CONNEXIONS
Rice University, Houston, Texas
This selection and arrangement of content as a collection is copyrighted by Kenneth Leroy Busbee. It is licensed under the Creative Commons Attribution 2.0 license (http://creativecommons.org/licenses/by/2.0/). Collection structure revised: June 6, 2010 PDF generated: December 29, 2011 For copyright and attribution information for the modules contained in this collection, see p. 324.
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Author Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Orientation and Syllabus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Why You should Create a Personal Connexions Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Creating a Connexions Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Sharing/Rating Connexions Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1 1. Introduction to Programming 1.1 1.2 1.3 1.4
Systems Development Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Bloodshed Dev-C++ 5 Compiler/IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Modularization and C++ Program Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Practice 1: Introduction to Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3 3. Data & Operators 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8
Data Types in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Identier Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Constants and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Data Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Practice 3: Data & Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4 4. Often Used Data Types 4.1 4.2 4.3 4.4 4.5 4.6 4.7
Integer Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Floating-Point Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Lvalue and Rvalue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Integer Division and Modulus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Practice 4: Often Used Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
iv
Pseudocode Examples for Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Hierarchy or Structure Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Program Control Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Void Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Documentation and Making Source Code Readable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Practice 6: Program Control Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
8 8. Standard Libraries 8.1 Standard Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 8.2 Practice 8: Standard Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
9 9. Character Data, Sizeof, Typedef, Sequence 9.1 9.2 9.3 9.4 9.5
Character Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Sizeof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Typedef - An Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Sequence Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Practice 9: Character Data, Sizeof, Typedef, Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
13.4 13.5
Repeat Until Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Practice 13: Test After Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
16 16. String Class, Unary Positive and Negative 16.1 16.2 16.3
String Class within C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Unary Positive and Negative Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Practice 16: String Class, Unary Positive and Negative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
19 19. File I/O and Array Functions 19.1 19.2 19.3 19.4 19.5
File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Arrays and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Loading an Array from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Math Statistics with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Practice 19: File I/O and Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
vi
22 22. Pointers 22.1 Address Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 22.2 Parameter Passing by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 22.3 Pointer Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 22.4 Indirection Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 22.5 Practice 22: Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
24 24. OOP & HPC 24.1 Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 24.2 Understanding High Performance Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 24.3 Practice 24: OOP & HPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
25 Review Materials 25.1 Review: Foundation Topics Group: 1-5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 25.2 Review: Modular Programming Group: 6-9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 25.3 Review: Structured Programming Group: 10-16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 25.4 Review: Intermediate Topics Group: 17-21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 25.5 Review: Advanced Topics Group: 22-24 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 26 Appendix 26.1 26.2 26.3 26.4 26.5 26.6 26.7
Abbreviated Precedence Chart for C++ Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 C++ Reserved Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 ASCII Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Show Hide File Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 Academic or Scholastic Dishonesty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Successful Learning Skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Study Habits that Build the Brain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Preface
collection of modules as a textbook will usually view it contents by reading the modules sequentially as
strongly related link. As the yellow decreases the importance decreases. Prerequisite links within the modules will be modules that student should have already read and most of the Supplemental links
Students using this collection for a college course should note that all of the will be modules that the student will read shortly. Thus, students should use Prerequisite links for review as needed and not be overly concerned about viewing all of the Supplemental links at the rst reading of this textbook/collection.
Conceptual Approach
The learning modules of this textbook/collection were, for the most part, written without consideration of a specic programming language. In many cases the C++ language is discussed as part of the explanation of the concept. Often the examples used for C++ are exactly the same for the Java programming language. However, some modules were written specically for the C++ programming language. This could not be avoided as the C++ language is used in conjunction with this textbook/collection by the author in teaching college courses.
Instructor Materials
Encrypted instructor materials are available in a module that is not part of this collection. It's title: Instructor Materials for: Programming Fundamentals - A Modular Structured Approach using C++ is available at: http://cnx.org/content/m34529/latest/ tutional faculty that are considering adoption of this collection as a textbook.
About Connexions
Connexions Modular Content
The Connexions Project http://cnx.org
dedicated to providing high quality learning materials free online, free in printable PDF format, and at low cost in bound volumes through print-on-demand publishing. This textbook is one of many collections available to Connexions users. Each
written in the Connexions XML markup language. Each module may also be re-used (or 're-purposed') as part of other collections and may be used outside of Connexions.
Connexions module may be used or modied for any purpose as long as proper attribution to the
original author(s) is maintained. Connexions' authoring tools make re-use (or re-purposing) easy. Therefore, instructors anywhere are permitted to create customized versions of this textbook by editing modules, deleting unneeded modules, and adding their own supplementary modules. Connexions' authoring tools keep track of these changes and maintain the CC license's required attribution to the original authors. This process creates a new collection that can be viewed online, downloaded as a single PDF le, or ordered in any quantity by instructors and students as a low-cost printed textbook.
Read the book online, print the PDF, or buy a copy of the book.
To browse this textbook online, visit the collection home page. You will then have three options. 1. You may view the collection modules on-line by clicking on the "Start through the subsequent modules by using the " PREVIOUS | NEXT " link, which takes you to " and " button that is towards
the rst module in the collection. You can then navigate to the next module using "NEXT
the upper right to move forward and backward in the collection. You can jump to any module in the collection by clicking on that module's title in the "TABLE OF CONTENTS" box on the left side of
2 "Instructor Materials for: Programming Fundamentals - A Modular Structured Approach using C++"
the window. If these contents are hidden, make them visible by clicking on the small triangle to the right of the "TABLE OF CONTENTS". Chapters also have a small triangle to show or hide contents. 2. You may obtain a PDF of the entire textbook to print or view oine by clicking on the "Download PDF" link in the "Content Actions" box. 3. You may order a bound copy of the collection (for a reasonable printing and shipping fee) by clicking on the "Order printed copy" button.
numbering within the textbook/collection's PDF and custom printing formats. 3. Within C++ there are three operators that do not convert properly to PDF format.
which is two minus signs which is two less than signs which is two greater than signs
Table 1
measure of quality. Thus, your participation in rating modules is welcomed and helps others determine
the quality of the educational materials being viewed. In order to rate modules you must have a Connexions account. Three (3) modules have been added to the preface series of modules for this collection. They explain why and how to create a Connexions account and how to rate a Connexions module.
Author Acknowledgements
I wish to acknowledge the many people who have helped me and have encouraged me in this project. 1. Mr. Abass Alamnehe, who is a fellow faculty member at Houston Community College. He has encouraged the use of Connexions as an "open source" publishing concept. His comments on several modules have led directly to the improvement of the materials in this textbook/collection. 2. The hundreds (most likely a thousand plus) students that I have taken programming courses that I have taught since 1984. The languages include: COBOL, main frame IBM assembly, Intel assembly, Pascal, "C" and "C++". They have often suggested that I write my own book because they thought that I was explaining the subject matter better than the author of the textbook that we were using. Little did my students understand that directly or indirectly they aided in the improvement of the materials from which I taught as well as improving me as a teacher. 3. To my future students and all those that will use this textbook/collection. They will provide suggestions for improvement as well as being the thousand eyes identifying the hard to nd typos, etc. 4. My wife, Carol, who supports me in all that I do. She has tolerated the many hours that I have spent in concentration on developing the modules that comprise this work. Without her support, this work would not have happened.
The approach of this course will be to take the student through a progression of materials that will allow the student to develop the skills of programming. The basic unit of study is a Connexions module. Several modules are collected into a chapter. The chapters are divided into ve groups.
Group Title
Pre-Chapter Items Foundation Topics Modular Programming Structured Programming Intermediate Topics Advanced Topics Review Materials Appendix
Chapters
N/A 1-5 6-9 10-16 17-21 22-24 N/A N/A
Modules
4 27 17 30 17 11 5 7
Total Modules
N/A
Table 1
118
Some professors using this textbook/collection might decide to eliminate certain modules or chapters. Some may eliminate the entire Advanced Topics group. Other professors may choose to add additional study materials. The advantage of this textbook/collection is that it may be adapted by professors to suit the needs of their students.
Chapter Layout
Each chapter will usually ow from: 1. One or more Connexions modules built for independent delivery. 2. A Connexions Practice module built specically for this textbook/collection. As you proceed with the Connexions modules that comprise a chapter, you should:
Do any exercises. Create 3x5 study cards for all denitions. When this material is used as a textbook for a course the denitions are to be memorized. Conrm this with your professor.
Learning Objectives Memory Building Activities aka MBAs Link These could consist of any of the following types of interactive computer activities: ordering or sorting. fun learning. ash card, crossword puzzle, seek a word, drag n drop, labeling, When the materials are used as a textbook for a course, it is imperative that
students do a variety of repetitive activities in order to memorize basic course material. Besides, have
Exercises In addition to any exercises within the study modules that you completed before the practice module, there will be at least one exercise for students to complete. Miscellaneous Items These will exist for some of the chapters. Lab Assignment Usually, completed on one's own eorts. Review the instructions/restrictions from your professor/teacher if using this for a high school or college credit course. Problems The intent of this activity is for students to formulate their own answers. Thus, solutions to the problems will not be provided. When the materials are used as a textbook for a course, the professor/teacher may assign students to a "Study Group" or let students form study groups to discuss their solutions with each other. If you are using this for a high school or college credit course, verify that you may work as team at solving the problems. This type of approved activity is called "authorized collusion" and is not a violation of "Academic or Scholastic Dishonesty" rules.
A professor using this textbook/collection/course will most likely have additional lab assignments, quizzes and exams that would be used in calculating your grade.
th Edition, ISBN: 0-321-51238-3 th Edition, ISBN: 0-321-38348-6 3. Starting Out with C++ Early Objects, by: Tony Gaddis et. al., 5 nd Edition, 4. Computer Science A structured Approach using C++, by: Behrouz A. Forouzan et. al., 2
ISBN: 0-534-37480-8 These textbooks are typically available in the used textbook market at a reasonable price. any one of the three books. If you acquire one of the above folder. Download from Connexions: Connexions_Module_Reading_List_col10621.pdf You may use
to download and store the following le to your storage device (disk drive or ash drive) in an appropriate
Syllabus
The syllabus for a course that is for credit will be provided by your specic course professor. If you are using this textbook/collection for non-credit as self-study, we have some suggestions:
1. Plan regular study periods 2. Review the three (3) Pre-Chapter Items modules 3. Review the last four (4) modules in the Appendix 4. Proceed with Chapter 1 going through all 24 chapters 5. Do all of the demo programs as you encounter them 6. Memorize all of the terms and denitions 7. Do all lab assignments 8. Prepare answers to all of the problems in the Practice modules 9. At the end of every section, do the Review module These is no magic way to learn about computer programming other than to immerse yourself into regular study and
study includes more than casual reading. To help you keep track of your study, we have
Check
Description
Pre-Chapter Items Last four Appendix Items Chapters 1 to 5 Review Materials for 1 to 5 Chapters 6 to 9 Review Materials for 6 to 9 Chapters 10 to 16 Review Materials for 10 to 16 Chapters 17 to 21 Review Materials for 17 to 21 Chapters 22 to 24 Review Materials for 22 to 24 First three Appendix Items
# Modules
6 4 27 1 17 1 30 1 17 1 11 1 3
N/A
Total Modules
Table 2
120
10
authors, students using textbook collections, etc.) helps authors decide which modules need improving and helps other users in evaluating the quality of respository content.
reading through a collection is a feature of the My Favorites lens private Member List lenses to create the ability for you to
part of the repository the quality of instructional materials and scholarly works available to the world via
the Internet free 24/7
Contribute materials that you author to the Connexions repository Remix or change (customize) materials provided by others that are in the Connexions repository Build collections (a group of modules) that specically serve your students or audience from
modules that you either create, improve or use without changing Often being an author, is over emphasized and pushed as the number one reason to get a Connexions account. Having authors contribute to the repository is important; however usage of the repository by users is equally important.
Increasing quality content in the Connexions repository goes hand in hand with increased usage of that content. It's like the Chinese Yin & Yang, both are important. We encourage all to create
an personal account.
11
12
Figure 1:
Available Training
A link is provided (in the box at the upper right corner of this module's page) to the Busbee's Connexions Training lens. It contains six collections that cover: 1. Understanding the Vision of Connexions 2. How to Search and Browse the Connexions Web Site (includes rating modules) 3. Eectively Using and Creating Connexions Lenses 4. Authoring Connexions Modules using Microsoft Word Documents 5. Ideas and Tools for Improving Connexions Modules and Collections 6. Appendix Materials for a Connexions Collection used as a College Course Each collection consists of several modules. The items appear alphabetically within the lens; however the Lens Comments for each item has its item position number similar to the list above.
URL
to
the
lens
in
your
browser.
Connexions Help
Don't hesitage to use the Help tab on the connexions home page at: http://cnx.org/ use the Connexions Project. expand as you click on them. Don't be afraid to click on something.
10
It is organized dierently than the above training collections, but contains ample information on how to Some of the menu items on the left
9 http://cnx.org/lenses/kbusbee/cnx-training 10 http://cnx.org/
13
Figure 2:
14
11
When prompted for Member Prole information you should complete elds as
Aliation Institution
You should review how others are typing their Aliation and type yours the same. For example: authors from Houston Community College could use: HCC or Houston CC or Houston Community College. However, they should all use: Houston Community College. This is not super important for the prole, but you will want to type the Institution eld in a collection consistently so collections created by authors from the same institution will all show up together when users do an institution search. The following two slides show you how to browse to the Institutions search and review what authors from your institution are using for their aliation/institution name.
15
16
Figure 1:
Browsing
17
Figure 2:
note: You could be the rst author and might need to decide what to use for your institution.
Portrait Picture
Using your picture processing skills; modify a picture of yourself cropping it square. It should be no greater than 150 by 150 pixels on each side. Usually this le is stored in a .jpg format.
note: The Connexions web site will shrink your picture to t its allocated space; thus, to avoid
Example
A link is provided (in the box at the upper right corner of this module's page) to my Member Prole (for Kenneth Leroy Busbee) at Connexions.
18
Figure 3:
13
At one time within Connexions there was a ve (5) star rating system provided within each Connexions module. This feature was not often used and was discontinued.
19
20
Figure 1:
Chapter 1
1. Introduction to Programming
1.1 Systems Development Life Cycle1
1.1.1 Discussion
The a major task (referred to as an application).
Systems Development Life Cycle is the big picture of creating an information system that handles The applications usually consist of many programs. An
example would be the Department of Defense supply system, the customer system used at your local bank, the repair parts inventory system used by car dealerships. There are thousands of applications that use an information system created just to help solve a business problem. Another example of an information system would be the "101 Computer Games" software you might buy at any of several retail stores. This is an entertainment application, that is we are applying the computer to do a task (entertain you). The software actually consists of many dierent programs (checkers, chess, tic tac toe, etc.) that were most likely written by several dierent programmers. Computer professionals that are in charge of creating applications often have the job title of System Analyst. The major steps in creating an application include the following and start at Planning step.
21
22
Figure 1.1:
During the
Design phase the System Analyst will document the inputs, processing and outputs of each During the Implementation phase programmers would be assigned to
write the specic programs using a programming language decided by the System Analyst. Once the system of programs is tested the new application is installed for people to use. As time goes by, things change and a specic part or program might need repair. During the
analysis, design and implementation. The programs that need modication are identied and programmers change or repair those programs. After several years of use, the system usually becomes obsolete. At this point a major revision of the application is done. Thus the cycle repeats itself.
1.1.2 Denitions
Denition 1.1: system analyst
Computer professional in charge of creating applications.
23
Unique Advantage: Can be installed and run on a ash drive, thus giving the student the ability to portability, Denition 1.6: portability
The ability to transport software on a ash drive and thus use it on various machines.
being able to do lab assignments at home, work, library, open lab, classroom, friend's house, etc.
You will need about 80MB of storage space. We suggest that you create
software and a C++ source code program that has been tested and is error free. two folders on your hard drive or
ash drive depending on which installation you choose. If on a ash drive create them at the root level of the drive. If on your home machine, you can use the folder area set up by the operating system for you as a user. Name them:
24
Compiler_Test
To help you keep les organized, you will want to create other sub-folders for storing source code les. We suggest you create at least two other sub-folder to be used with Connexions' related modules. Within the Cpp_Source_Code_Files, create sub-folders named:
Demo_Programs Monitor_Header
3.4.2 You can either download it from Bloodshed or download the version as of 12/8/2008 that is stored on the Connexions web site. Store it in the Cpp_Software_Download folder you created. The software is approximately 9.1 MB and will take several minutes to download if you are using a dial-up modem connection.
tip:
The software has not signicantly changed since 2007 and the Connexions version will be The Bloodshed link requires some additional navigation to get to the
software download. Thus, because it is signicantly easier, we recommend that you download the software from the Connections web site. Link to Bloodshed: http://www.bloodshed.net/dev/devcpp.html
25
devcpp-4.9.9.2_setup.exe software
C:\Dev-Cpp\ (or
select the location you want to store the installed program but use the default unless you are familiar
devcpp-4.9.9.2_setup.exe software
C:\Dev-Cpp\ however, it needs to be changed. Change the "Destination Directory" by selecting C:\Dev-Cpp\ to DriveLetter:\Dev-Cpp\ (where the DriveLetter is the drive that represents your ash drive).
changing the default software location from: 4. When it asks: "Do you want to install Dev C++ for all users on this computer?" answer "No". 5. After it installs, it will ask some "rst time conguration" questions. Again, use common sense and answer the questions. NOTE THE FOLLOWING ITEM: 6. Answer "No" to the retrieve information from header les. 7. It will start your compiler/IDE with a "Tip of the day". We suggest you check the box in the lower left and select "Close". 8. Close your compiler/IDE by using the normal red "X" box. We want to show you how to start your compiller normally. 9. To start your compiler software you navigate to the "Dev-Cpp" folder on your ash drive and select the "devcpp.exe" application. NOTE: When using the ash drive you should not try starting the compiler by double clicking on a C++ source code le. This method works on a machine installation but does not work on a ash drive installation. 10. On the menus at the top Select "File" then "Open project or le" then navigate to where your source code le (Compiler_Test.cpp) is stored on your ash drive. Select the source code le and open it. 11. You should see the source code listing. Press F9 key or select the "Execute" then "Compile & Run" from the menus at the top. A black screen box should appear and you answer questions appropriately to run the program. When you are done running your program the black screen box goes away. See the suggested folder name above.
26
what they are called in the two predominant programming languages of today: C++ and Java. Functions are important because they allow us to take large complicated programs and to divide them into smaller manageable pieces. Because the function is a smaller piece of the overall program, we can concentrate on what we want it to do and test it to make sure it works properly. Generally functions fall into two categories: 1.
Program Control - Functions used to simply sub divide and control the program. These functions are
unique to the program being written. Other programs may use similar functions maybe even functions with the same name, but the content of the functions are almost always very dierent.
2.
Specic Task - Functions designed to be used with several programs. These functions perform a
specic task and thus are useable in many dierent programs because the other programs also need to do the specic task. Specic task functions are sometimes referred to as building blocks. Because they are already coded and tested, we can use them with condence to more eciently write a large program.
The main program must establish the existence of functions used in that program. programming language, there is a formal way to: 1. dene a function (it's 2.
Depending on the
3. declare a function (a
Program Control functions normally do not communicate information to each other but use a common area for variable storage. Specic Task functions are constructed so that data can be communicated between the calling program piece (which is usually another function) and the function being called. This ability to communicate data is what allows us to build a specic task function that may be used in many programs. The rules for how the data is communicated in and out of a function vary greatly by programming language, but the concept is the same. The data items passed (or communicated) are called parameters. Thus the wording:
1. no communication in with no communication out 2. some communication in with no communication out 3. some communication in with some communication out 4. no communication in with some communication out
27
// Purpose: Average the ages of two people // Author: Ken Busbee; Kenneth Leroy Busbee // Date: Jan 5, 2009 // Comment: Main idea is to be able to // debug and run a program on your compiler. //****************************************************** // Headers and Other Technical Items #include <iostream> using namespace std; // Function Prototypes void pause(void); // Variables int int double age1; age2; answer;
//****************************************************** // main //****************************************************** int main(void) { // Input cout "\nEnter the age of the first person --->: "; cin age1; cout "\nEnter the age of the second person -->: "; cin age2; // Process answer = (age1 + age2) / 2.0; // Output cout "\nThe average of their ages is -------->: "; cout answer; pause(); return 0; } //****************************************************** // pause //****************************************************** void pause(void) {
28
special or uniqueness of main as a function is that this is where the program starts executing code and this is where it usually stops executing code. It is usually the rst function dened in a program and appears after the area used for includes, other technical items, declaration of prototypes, the listing of global constants and variables and any other items generally needed by the program. The code to dene the function main is provided; however, it is not prototyped or usually called like other functions within a program. In this simple example, there are no other program control functions.
pile_Test.cpp source code above we have such a task that is used to stop the execution of the code until the user hits the enter key. The functions name is: pause. This function is not communicating any information between the calling function and itself, thus the use of the data type void.
<return value data type> function identifier name(<data type> <identifier name for input value>) { lines of code;
return <value>; }
There is no semi-colon after the rst line. Semi-colons are used at the end of a statement in C++, but not on the rst line when dening a function. Functions have a set of
block of statements or lines of code. There are normally several lines of code within a function. Lines of code containing the instructions end in a semi-colon. Can you identify the denition of the pause function in the above program example? The pause function denition is after the function main. Though not technically required, most programs list all functions (program control or specic task) after the function main. Let's identify the location where the function pause is called. The calling function is the function main and it towards the end of the function. The line looks like:
pause();
When you call a function you use its identier name and a set of parentheses. You place any data items you are passing inside the parentheses, and in our example there are none. A semi-colon ends the statement
29
or line of code.
After our program is compiled and running, the lines of code in the function main are
executed and when it gets to the calling of the pause function, the control of the program moves to the pause function and starts executing the lines of code in the pause function. When it's done with the lines of code, it will return to the place in the program that called it (in our example the function main) and continue with the code in that function. Once we know how to dene a function and how to call a function, we usually will need to know how to declare a function to the compiler (called a prototype). Because of normal computer programming industry standards, programmers usually list the function main rst with other functions dened after it. executable version for running on our computer, the rst step of the process is compiling. Then somewhere in the function main, we will call a function. When we convert our source code program to an The compiler program demands to know what the communication will be between two functions when a function is called. It will know the communication (what going in and out as parameters) if the function being called has been dened. But, we have not dened that function yet; it is dened after the function main. To solve this problem, we show the compiler a prototype of what the function will look like (at least the communication features of the function) when we dene it.
void pause(void);
This line of code looks exactly like the rst line in our function denition with one important addition of a semi-colon. Prototypes (or declarations to the compiler of the communications of a function not yet
Summary concept: If you call a function before it is dened you must prototype it before it is called. Looking at our list of the
dened) are placed near the top of the program before the function main. three things you do in conjunction with a function in the order that they normally appear in a program, there is a formal way to: 1. declare a function (a prototype is a communications declaration to a complier) 2. call a function 3. dene a function
1.3.4 Denitions
Denition 1.9: modularization
The ability to group some lines of code into a unit that can be included in our program.
30
31
1.4.3 Exercises
Exercise 1.4.1 Answer the following statements as either true or false:
1. Beginning programmers participate in all phases of the Systems Development Life Cycle. 2. The Bloodshed Dev-C++ 5 compiler/IDE is the preferred compiler for this textbook/collection, however any C++ compiler will work. 3. Most compilers can be installed on a ash drive. 4. In addition to function as the name of a sub-program, the computer industry also uses macro, procedure and module. 5. Generally functions fall into two categories: Program Control and Specic Task.
(Solution on p. 34.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
32
Lab_01.cpp
If you are having problems seeing the le extensions, visit the Show Hide File Extensions instructions within the Appendix. Start your compiler and open the source code le. Carefully make the following modications:
note: During the rest of the course you will often use a source code le provided by the instructor
as your starting point for a new lab assignment. Sometimes you will use a source code le that you have created as your starting point for a new lab assignment. Either way, you should modify the comments area as appropriate to include at a minimum the four lines of information (lename, purpose, author and date) as established in this lab assignment.
We are now going to make simple modications to this program so that it is able to
average the weight of three people. Do the following:
Within the variables area, change the variable names for age1 and age2 to weight1 and weight2. Add another variable of integer data type with the identier name of weight3. The input area has two prompts and requests for data from the user. They are paired up a prompt and getting data from the keyboard. We need to modify the prompt to ask for weight instead of age. We need to change the variable name from age1 to weight1. Do this for the second pair that prompts and gets the second data item. Create a third pair that prompts and gets the third data item.
The process area has only one line of code and we need to make changes that add the weight3 and divides by 3.0 instead of 2.0. The code should look like this:
answer = (weight1 + weight2 + weight3) / 3.0; The output area needs the text modied from ages to weights.
Build (compile and run) your program. You have successfully written this program if when it run and you put in the three weights; it tells you the correct average.
After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
33
1.4.6 Problems
1.4.6.1 Problem 01a Instructions
List the steps of the Systems Development Life Cycle and indicate which step you are likely to work in as a new computer professional.
34
Chapter 2
Understanding the Program Using Design Tools to Create a Model Develop Test Data
This
IPO approach works very well for beginning programmers. Sometimes, it might help to visualize the
You can imagine what the monitor will look like, what the user
must enter on the keyboard and what processing or manipulations will be done.
of each function in your program. At rst, you will have only one function, and thus your pseudocode will
35
36
hierarchy or structure charts, pseudocode, HIPO, Nassi-Schneiderman charts, Warnier-Orr diagrams, etc. Programmers are expected to be able to understand and do owcharting and pseudocode. These methods of developing the model of a program are usually taught in most computer courses. Several standards exist for owcharting and pseudocode and most are very similar to each other. However, most companies have their own documentation standards and styles. Programmers are expected to be able to quickly adapt to any owcharting or pseudocode standards for the company at which they work. The others methods that are less universal require some training which is generally provided by the employer that chooses to use them. Later in your programming career, you will learn about using applications software that helps create an information system and/or programs. This type of software is called Computer-aided Software Engineering. Understanding the logic and planning the algorithm on paper before you start to code is very important concept. Many students develop poor habits and skipping this step is one of them.
2.1.5 Denitions
Denition 2.1: IPO
Inputs - Processing - Outputs
2.2 Pseudocode2
2.2.1 Overview
Pseudocode is one method of designing or planning a program. It looks like (imitates) real code but it is NOT real code.
false code. A better translation would be the word fake or imitation. Pseudocode is fake (not the real thing). It uses English statements to describe what a program is to accomplish. It is fake because no complier exists that will translate the pseudocode to any machine language. Pseudocode is used for documenting the program or module design (also known as the algorithm). The following outline of a simple program illustrates pseudocode. We want to be able to enter the ages of two people and have the computer calculate their average age and display the answer.
a message asking the user to enter the first age first age from the keyboard a message asking the user to enter the second age second age from the keyboard
37
Processing calculate the answer by adding the two ages together and dividing by two Output display the answer on the screen pause so the user can see the answer
After developing the program design, we use the pseudocode to write code in a language (like Pascal, COBOL, FORTRAN, "C", " C++", etc.) where you must follow the rules of the language (syntax) in order to code the logic or algorithm presented in the pseudocode. Pseudocode usually does not include other items produced during programming design such as identier lists for variables or test data. There are other methods for planning and documenting the logic for a program. One method is
HIPO. It
stands for Hierarchy plus Input Process Output and was developed by IBM in the 1960s. It involved using a hierarchy (or structure) chart to show the relationship of the sub-routines (or functions) in a program. Each sub-routine had an
IPO piece. Since the above problem/task was simple, we did not need to use multiple
sub-routines, thus we did not produce a hierarchy chart. We did incorporate the IPO part of the concept for the pseudocode outline.
2.2.2 Denitions
Denition 2.4: pseudo
Means false and includes the concepts of fake or imitation.
2. to check the coded program to see if it produces the correct results (code
checking) checking)
Test data is developed by using the algorithm of the program. This algorithm is usually documented during the program design with either owcharting or pseudocode. Here is the pseudocode in outline form describing the inputs, processing and outputs for a program used for painting rectangular buildings.
Example 2.2: Pseudocode using an IPO Outline for Painting a Rectangular Building
Input display get the display get the display get the display get the display get the
a message asking user for the length of the building length from the keyboard a message asking user for the width of the building width from the keyboard a message asking user for the height of the building height from the keyboard a message asking user for the price per gallon of paint price per gallon of paint from the keyboard a message asking user for the sq ft coverage of a gallon of paint sq ft coverage of a gallon of paint from the keyboard
38
Processing calculate the total area of the building by: multiplying the length by height by 2 then multiply the width by height by 2 then add the two results together calculate the number of gallons of paint needed by: dividing the total area by the coverage per gallon then round up to the next whole gallon calculate the total cost of the paint by: multiplying the total gallons needed by the price of one gallon of paint Output display the number of gallons needed on the monitor display the total cost of the paint on the monitor pause so the user can see the answer
chosen programming language (in our case C++)? The answer (or solution) to all of these questions is our
39
2.3.4 Denitions
Denition 2.5: model checking
Using test data to check the design model (usually done in pseudocode).
40
2.4.3 Exercises
Exercise 2.4.1 Answer the following statements as either true or false:
1. Coding the program in a language like C++ is the rst task of planning. You plan as you code. 2. Pseudocode is the only commonly used planning tool. 3. Test data is developed for testing the program once it is code into a language like C++. 4. The word pseudo means false and includes the concepts of fake or imitation. 5. Many programmers pick up the bad habit of not completing the planning step before starting to code the program.
(Solution on p. 43.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
41
Navigate to your sub-folder: Chapter_02 and print the: Solution_Lab_01_Pseudocode.txt and the: Solution_Lab_01_Test_Data.txt les. Review the printouts in conjunction with your Lab 01 source code le. Note: In Lab 01 we gave you step by step directions for modifying the Compiler_Test.cpp source code le into the Lab_01.cpp source code le. Chapter 2 materials, these les should make sense. I could have given you the pseudocode and test data les, but you had not received any instructions about them. Now, after having studied the
Here is the problem. Your local bank has many customers who save their change and periodically bring it in for deposit. The customers have sorted the coins (pennies, nickels, dimes and quarters forget half dollars and dollar coins) and know how many (an integer data type) of each value they have but have evolved (or their environment has made them lazy). They have lost their ability to do simple arithmetic. computer). They can't seem to determine the total value of all of the coins without a calculator (or Write a program to interact with the customers and to determine and report the total
not going to write a program (create source code) using the C++ programming
language. We are going to design a plan for a program using pseudocode as our model. Additionally, we need to develop some test data for use in testing our model.
Start your text editor (Microsoft Notepad. Set the font on Notepad to: Courier 12 font because Courier uses the same amount of space horizontally for each letter. It will make the information in the les show as it should be seen. Open your Lab 02 pseudocode text le. Change all items as needed to be able to solve the above problem. NOTE: You may use any application software that will let you open, modify and save ASCII text les. You may even use the Bloodshed Dev-C++ 5 compiler/IDE software to view, modify and save ASCII text les; after all, our source code les are ASCII text les with a special extension of .cpp
Open your Lab 02 test data text le. Modify the text le as appropriate. Use your test data and step through your pseudocode. Does everything seem to work? Have you missed something? When you are satised that you have your program plan and test data completed, close your text les. That's right, I said, "Save and close your text les." Files should be properly closed before being used by other software and this includes the Operating System software that moves, copies and renames les.
After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
2.4.6 Problems
2.4.6.1 Problem 02a Instructions
Create simple IPO pseudocode to solve the following: Problem: I have a friend who is visiting from Europe and he does not understand exactly how much gas he is buying for his car. We need to write a program that allows him to enter the gallons of gas and convert it to liters (metric system). NOTE: One US gallon equals 3.7854 liters.
42
cases) and the price he paid (Example: $4.67). The program has a list of 20 items and after the chef enters the information, the program provides a total for the purchases for that day. You need to prepare test data for ve (5) items: apples, oranges, bananas, lettuce and tomatoes.
43
44
Chapter 3
type denes a set of values and a set of operations that can be applied on those values. The set of 2 The four major families of data include:
The C++ programming language identies ve data types as standard data types:
The standard data types and the complex data types within C++ have a series of attributes, which include:
C++ Reserved or Key Word Domain the allowed values Signage do they allow negative numbers or only positive numbers Meaning i.e. What do they represent Rules of Denition What special characters indicate the data type Size in terms of the number of bytes of storage used in the memory
1 This content is available online at <http://cnx.org/content/m18653/1.13/>. 2 Behrouz A. Forouzan and Richard F. Gilberg, Computer Science A Structured Approach using C++ Second Edition (United
45
46
Operations Allowed i.e. Which operators can I use on the data type
Complex
String
string
No
Complex
Array
N/A
No
Complex
Pointer
N/A
A tion area
value to a (an
that locadata
No
points within
address)
the
Table 3.1
The ve standard data types usually exist in most programming languages and act or behave similarly from language to language. Most courses of study for a programming course or language will explain the standard data types rst. After they are learned, the complex data types are introduced. The Boolean, character and integer data types are identied as belonging to the Integer Family. These data types are all represented by integer numbers and thus act or behave similarly.
3.1.2 Denitions
Denition 3.1: data type
Denes a set of values and a set of operations that can be applied on those values.
47
These attributes vary from one programming language to another. The allowable characters and reserved words will be dierent. The length limit refers to how many characters are allowed in an identier name and often is compiler dependent and may vary from compiler to compiler for the same language. However, all programming languages have these three technical rules.
Meaningful identier names make your code easier for another to understand. After all what does "p" mean? Is it pi, price, pennies, etc. Thus do not use cryptic (look it up in the dictionary) identier names. Some programming languages treat upper and lower case letters used in identier names as the same. Thus: pig and Pig are treated as the same identier name. Unknown to you the programmer, the compiler usually forces all identier names to upper case. Thus: pig and Pig both get changed to PIG. However not all programming languages act this way. Some will treat upper and lower case letters as being dierent things. Thus: pig and Pig are two dierent identier names. If you declare it as pig and then reference it in
be case consistent. Use an identier name only once and spell it (upper and lower case) the same way within
your code later as Pig you get a compiler error. To avoid the problem altogether, we teach students to your program.
These rules are decided by the industry (those who are using the programming language). The above rules were commonly used within the "C" programming language and have to large degree carried over to C++.
48
3.2.5 Denitions
constant is a data item whose value cannot change during the program's execution. Thus, as its name variable is a data item whose value can change during the program's execution. Thus, as its name
implies their value is constant. implies their value can vary. Constants are used in three ways within C++. They are: 1. literal constant 2. dened constant 3. memory constant A
literal constant is a value you type into your program wherever it is needed. Examples include the Example 3.1: Literal Constants
constants used for initializing a variable and constants used in lines of code:
= = = =
#define PI 3.14159
Called a
dened constant because it uses a textual substitution method controlled by the compiler
49
itself. How can it be constant and vary at the same time? The better name for the second one is a
3.3.3 Denitions
Denition 3.7: constant
A data item whose value cannot change during the program's execution.
3.4.2 Denitions
Denition 3.9: expression
A valid sequence of operand(s) and operator(s) that reduces (or evaluates) to a single value.
50
51
int age; // variable set up then later in the program age = 21;
The value 21 is moved to the memory location for the variable named: age. Another way to say it: age is assigned the value 21.
int students_period_1 = 25; // variable set up with initialization int students_period_2 = 19; int total_students; then later in the program total_students = students_period_1 + students_period_2;
The expression to the right of the assignment operator contains some identier names. The program would fetch the values stored in those variables; add them together and get a value of 44; then assign the 44 to the total_students variable.
3.5.2 Denitions
Denition 3.16: assignment
An operator that changes the value of a modiable data object.
52
C ++ operator symbol
+ * / %
These arithmetic operators are binary that is they have two operands. constants or variables.
age + 1
This expression consists of one operator (addition) which has two operands. The rst is represented by a variable named age and the second is a literal constant. If age had a value of 14 then the expression would evaluate (or be equal to) 15. These operators work as you have learned them throughout your life with the exception of division and modulus. We normally think of division as resulting in an answer that might have a fractional part (a oating-point data type). However, division when both operands are of the integer data type act dierently. Please refer to the supplemental materials on "Integer Division and Modulus".
Implicit the change is implied Explicit the change is explicitly done with the cast operator
Promotion going from a smaller domain to a larger domain Demotion going from a larger domain to a smaller domain
55 + 1.75
8 This content is available online at <http://cnx.org/content/m18744/1.7/>.
53
In this example the integer value 55 is converted to a oating-point value (most likely double) of 55.0. It was promoted.
int money; // variable set up then later in the program money = 23.16;
In this example the variable money is an integer. We are trying to move a oating-point value 23.16 into an integer storage location. This is demotion and the oating-point value usually gets truncated to 23.
3.7.3 Promotion
Promotion is never a problem because the lower data type (smaller range of allowable values) is sub set of the higher data type (larger range of allowable values). to another. Promotion often occurs with three of the standard data types: character, integer and oating-point. The allowable values (or domains) progress from one type That is the character data type values are a sub set of integer values and integer values are a sub Even set of oating-point values; and within the oating-point values: oat values are a sub set of double. $, comma, etc.) their bit pattern also represent integer values from 0 to 255. promote them up the chain from character to integer to oat to double.
though character data represent the alphabetic letters, numeral digits (0 to 9) and other symbols (a period, This progression allows us to
3.7.4 Demotion
Demotion represents a potential problem with truncation or unpredictable results often occurring. you t an integer value of 456 into a character value? does not stop the compilation process. reasonable. If I calculate the number of cans of soup to buy based on the number of people I am serving (say 8) and the servings per can (say 2.3), I would need 18.4 cans. I might want to demote the 18.4 into an integer. It would truncate the 18.4 into 18 and because the value 18 is within the domain of an integer data type, it should demote with the I might have a get an How do How do you t the oating-point value of 45656.453 into an integer value? Most compilers give a warning if it detects demotion happening. A compiler warning It does warn the programmer to check to see if the demotion is
If I tried demoting a double that contained the number of stars in the Milky Way galaxy into an integer,
unpredictable result (assuming the number of stars is larger than allowable values
Within C++ the cast operator is a unary operator; it only has one
operand and the operand is to the right of the operator. The operator is a set of parentheses surrounding the new data type.
(int) 4.234
This expression would evaluate to: 4.
54
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
3.7.7 Denitions
Denition 3.17: implicit
A value that has its data type changed automatically.
55
11
3.8.3 Exercises
Exercise 3.8.1 Answer the following statements as either true or false:
1. A data type denes a set of values and the set of operations that can be applied on those values. 2. Reserved or key words can be used as identier names. 3. The concept of precedence says that some operators (like multiplication and division) are to be executed before other operators (like addition and subtraction). 4. An operator that needs two operands, will promote one of the operands as needed to make both operands be of the same data type. 5. Parentheses change the precedence of operators.
(Solution on p. 58.)
12
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
10 This content is available online at <http://cnx.org/content/m22455/1.4/>. 11 See the le at <http://cnx.org/content/m22455/latest/index.html> 12 See the le at <http://cnx.org/content/m22455/latest/Manipulation_Data_Part_1.pdf>
56
13
Navigate to your sub-folder: Chapter_03 and open and study the two les. We have learned that a fundamental concept of interaction with computers is to divide the problem/task into three parts input, processing and output. (input processing output) approach again. This problem is simple and we will use the IPO
it backwards.
What output do I want displayed? Number of gallons of paint Total cost of the paint
Thus, what calculations do I need to make? Total cost of the paint is the Number of gallons needed times price per gallon Number of gallons needed is the Total area to be covered (let's use square feet) divided by the coverage
per gallon of paint (Note: you must round up to the next full gallon of paint.)
Total area to be covered is the Length times height times 2 added to the width times height times 2
Which leads us to, what data do I need as input? Price of a gallon of paint Number of square feet that a gallon will cover Length of the house Width of the house Height of the house
You can see that by working the logic backwards, we can start to completely see what the program must do. (output). We need to enter some data (input), do some calculations (process) and display the results
Copy into your sub-folder: Chapter_03 one of the source code listings that we have used (we suggest the Lab 01 source code) and rename the copy to:
Lab_03.cpp
Modify the code to follow the Solution_Lab_03_Pseudocode.txt le. I am just going to give you the line of code for rounding up to the next whole gallon of paint (See the Data Type Conversions module within Chapter 3 of the Connexions materials. Do you understand why it works?).
57
Build (compile and run) your program. You have successfully written this program when it runs with your test data and gives the predicted results.
After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
3.8.6 Problems
3.8.6.1 Problem 03a Instructions
Write the C++ code to do the following pseudocode example.
Prompt the user for his monthly income. Get the users monthly income. Multiply the monthly income by 12. Display the annual income. Pause the program so the user can see the answer. (HINT: You may call a function.)
58
Chapter 4
The integer data type with its various modiers that create dierent domains The integer family which also includes the Boolean and character data types The integer values jump
The integer data type basically represents whole numbers (no fractional parts). from one value to another. There is nothing between 6 and 7.
numbers oating point which allow for fractional parts. The reason is twofold. First, some things in the real world are not fractional. A dog, even with only 3 legs, is still one (1) dog not one value to another. The integer data type has the same attributes and acts or behaves similarly in all programming languages. The most often used integer data type in C++ is the simple integer.
data type is often used to control program ow by counting, thus the need for a data type that jumps from
C++ Reserved Word Represent Size Normal Signage Domain (Values Allowed) C++ syntax rule C++ syntax rule
int Whole numbers (no fractional parts) Usually 4 bytes Signed (negative and positive values) -2,147,483,648 to 2, 147,483,647 Do not start with a 0 (zero) No decimal point
Table 4.1
Within C++ there are various reserved words that can be used to modify the size or signage of an integer. They include: long, short, signed and unsigned. Signed is rarely used because integers are signed by default you must specify unsigned if you want integers that are only positive. Possible combinations are:
59
60
The domain of each of the above data type options varies with the complier being used and the computer. The domains vary because the byte size allocated to the data varies with the compiler and computer. This eect is known as being
machine dependent.
upgrades to the language. In "C" the int data type was allocated 2 bytes of memory storage on an Intel compatible central processing unit (cpu) machine. In "C++" an int is allocated 4 bytes. These variations of the integer data type are an annoyance in C++ for a beginning programmer. For a beginning programmer it is more important to understand the general attributes of the integer data type that apply to most programming languages.
4.1.2 Denitions
Denition 4.1: machine dependent
An attribute of a programming language that changes depending on the computer's CPU.
has the same attributes and acts or behaves similarly in all programming languages. They can always store negative or positive values thus they always are signed; unlike the integer data type that could be unsigned. The small numbers. Rather than talk about the actual values, we mention the storage the larger the mantissa and exponent, thus more precision. The most often used oating-point family data type used in C++ is the
domain for oating-point data types varies because they could represent very large numbers or very precision. The more bytes of double. By default, most
compilers convert oating-point constants into the double data type for use in calculations. The double data type will store just about any number most beginning programmers will ever encounter.
61
C++ Reserved Word Represent Size Storage Normal Signage Domain (Values Allowed) C++ syntax rule
double Numbers with fractional parts Usually 8 bytes two parts (always treated together)a mantissa and an exponent Signed (negative and positive values)
1.7E-308
to
1.7E308
Within C++ there are various reserved words that can be used to establish the size in bytes of a oatingpoint data item. More bytes mean more precision:
The domain of each of the above data type options varies with the complier being used and the computer. The domains vary because the byte size allocated to the data varies with the compiler and computer. This eect is known as being
machine dependent.
These variations of the oating-point family of data types are an annoyance in C++ for a beginning programmer. For a beginning programmer it is more important to understand the general attributes of the oating-point family that apply to most programming languages.
4.2.2 Denitions
Denition 4.2: double
The most often used oating-point family data type used in C++.
string. Thus, the need to handle a series of characters as a single piece of data (in English correctly called a
62
null character (the value 0 or the rst character in the ASCII character code set). Associated with object oriented programming the string class has been added to C++ as a standard part of the programming language. This changed with the implementation with strings being stored as a length controlled item with a maximum length of 255 characters. Included in the C++ string class is the reserved word of it were a data type. Some basics about strings include:
string as if
C++ Reserved Word Represent Size Normal Signage Domain (Values Allowed) C++ syntax rule
string Series of characters (technically an array) Varies in length N/A Extended ASCII Character Code Set Double quote marks for constants
Table 4.5
For now, we will address only the use of strings as constants. Most modern compliers that are part of an Integrated Development Environment (IDE) will color the source code to help the programmer see dierent features more readily. Beginning programmers will use string constants to send messages to the monitor. A typical line of C++ code:
cout
"Hi Mom!";
would have the "Hi Mom" colored (usually red) to emphasize that the item is a string.
4.3.2 Denitions
Denition 4.5: string
A series or array of characters as a single piece of data.
arithmetic assignment operators are a form of short hand. Various textbooks call them "com-
pound assignment operators" or "combined assignment operators". Their usage can be explaned in terms of the assignment operator and the arithmetic operators. In the table we will use the variable age and you can assume that it is of integer data type.
Equivalent code:
63
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
concept refers to the requirement that the operand on the left side of the assignment operator is modiable,
Rvalue concept pulls or fetches the value of the expression or operand on the right side
Example 4.1
int age; // variable set up then later in the program age = 39;
The value 39 is pulled or fetched (Rvalue) and stored into the variable named age (Lvalue); destroying the value previously stored in that variable.
Example 4.2
5 See the le at <http://cnx.org/content/m18743/latest/Demo_Arithmetic_Assignment.cpp> 6 This content is available online at <http://cnx.org/content/m18058/1.12/>.
64
int age; // variable set up int voting_age = 18; // variable set up with initialization then later in the program age = voting_age;
If the expression has a variable or named constant on the right side of the assignment operator, it would pull or fetch the value stored in the variable or constant. The value 18 is pulled or fetched from the variable named voting_age and stored into the variable named age.
Example 4.3
test expression or Boolean expression, the concept is still an Rvalue one. The
value in the identier named age is pulled or fetched and used in the relational comparison of less than.
Example 4.4
const int JACK_BENNYS_AGE = 39; then later in the program JACK_BENNYS_AGE = 65;
// constant set up
This is illegal because the identier JACK_BENNYS_AGE does not have Lvalue properties. It is not a modiable data object, because it is a constant. Some uses of the Lvalue and Rvalue can be confusing.
Example 4.5
int oldest = 55; // variable set up with initialization then later in the program age = oldest++;
Postx increment says to use my existing value then when you are done with the other operators; increment me. Thus, the rst use of the oldest variable is an Rvalue context where the existing value of 55 is pulled or fetched and then assigned to the variable age; an Lvalue context. The second use of the oldest variable is an Lvalue context where in the value of oldest is incremented from 55 to 56.
4.5.2 Denitions
Denition 4.6: Lvalue
The requirement that the operand on the left side of the assignment operator is modiable, usually a variable.
65
oating-point division.
However, division when both operands are of the integer data type acts dierently on most computers and
integer division. Within the C++ programming language the following expression does not give
Example 4.6
11 / 4
Because both operands are of the integer data type the evaluation of the expression (or answer) would be 2 with no fractional part (it gets thrown away). Again, this type of division is call what you learned in grade school the rst time you learned about division.
Figure 4.1:
In the real world of data manipulation there are some things that are always handled in whole units or numbers (integer data type).
To illustrate our example: I have 11 dollar Answer is 2 with me still having each or 2.75 for each child. The
dollar coins are not divisible into fractional pieces. Don't try thinking out of the box and pretend you're a pirate. Using an axe and chopping the 3 remaining coins into pieces of eight. Then, giving each child 2 coins and 6 pieces of eight or 2 6/8 or 2
tomato soup. I dare you to try and chop up three cans of soup and give each kid
66
of a
living things like puppy dogs. After you divide them up with an axe, most children will not want the dog. What is
modulus? It's the other part of the answer for integer division. It's the remainder. Remember
in grade school you would say, "Eleven divided by four is two remainder three." In C++ programming language the symbol for the modulus operator is the percent sign (%).
Example 4.7
11 % 4
Thus, the answer or value of this expression is 3 or the remainder part of integer division.
Example 4.8
How many times can you divide 24 into 6? Six divied by 24 is zero. This is dierent from: What is the remainder of 6 divided by 24? Six, the remainder part given by modulus.
(Solution on p. 70.)
(Solution on p. 70.)
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
67
68
10
4.7.3 Exercises
Exercise 4.7.1 Answer the following statements as either true or false:
1. Integer data types are stored with a mantissa and an exponent. 2. Strings are identied by single quote marks. 3. An operand is a value that receives the operator's action. 4. Arithmetic assignment is a shorter way to write some expressions. 5. Integer division is rarely used in computer programming.
(Solution on p. 70.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
69
The Problem/Task I have a friend who is visiting from Europe and he does not understand Fahrenheit temperatures. We need to write a program that allows him to enter the temperature in Fahrenheit (as Clue 1: Fahrenheit water freezes at 32 degrees Google Clue 2: You can also use Celsius water freezes a zero (0) degrees and boils at 100 degrees. announced on TV or radio) and convert it to Celsius. and boils at 212 degrees.
the internet (how to convert Fahrenheit to Celsius) if you need more help. Internet sites to do a conversion and thus create your test data.
You only need two variables in this program: Fahrenheit and Celsius both of which should be the integer data type. When you convert the Fahrenheit to Celsius you will need to use a oating-point expression doing oating-point calculations for precision. Additionally we want to round up or down the Celsius answer by adding 0.5 to the calculation expression.
Within your sub-folder: Chapter_04 you will need to create three les: and
Lab_04_Pseudocode.txt Lab_04_Test_Data.txt and Lab_04.cpp NOTE: It will be easier to copy some previous les
from another assignment and use those copies by renaming them and modifying them as appropriate. The professor is expecting the items you create to have a similar format to those we have been using in the course.
Create your pseudocode, test data and source code les. Build (compile and run) your program. You have successfully written this program when it runs with your test data and gives the predicted results. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
4.7.6 Problems
4.7.6.1 Problem 04a Instructions
Decide on the data type and identier names for the following: Problem: A men's clothing store that caters to the very rich wants to create a data base for its customers that records clothing measurements. They need to record information for shoes, socks, pants, dress shirts and casual shirts. HINT: You may need more than 5 data items.
70
Chapter 5
integrated into one program usually called a compiler. However, this one compiler program is environment used by programmers to develop software. Thus the name: Integrated Development Environment or IDE.
have been really many software items that create an The following gure shows the progression of activity in an IDE as a programmer enters the source code and then directs the IDE to compile and run the program.
71
72
Figure 5.1:
Upon starting the IDE software the programmer usually indicates he wants to open a le for editing as source code. As they make changes they might either do a "save as" or "save". When they have nished entering the source code, they usually direct the IDE to "compile & run" the program. The IDE does the following steps: 1. If there are any unsaved changes to the source code le it has the 2. The
test editor save the changes. compiler opens the source code le and does its rst step which is executing the pre-processor
compiler directives and other steps needed to get the le ready for the second step. The #include will insert header les into the code at this point. If it encounters an error, it stops the process and returns the user to the source code le within the text editor with an error message. If no problems encountered it saves the source code to a temporary le called a translation unit. 3. The
compiler opens the translation unit le and does its second step which is converting the
programming language code to machine instructions for the CPU, a data area and a list of items to be resolved by the linker. Any problems encounted (usually a syntax or violation of the programming language rules) stops the process and returns the user to the source code le within the text editor with an error message. If no problems encountered it saves the machine instructions, data area and linker resolution list as an object le.
73
4. The
linker opens the program object le and links it with the library object les as needed. Unless all
linker items are resolved, the process stops and returns the user to the source code le within the text editor with an error message. If no problems encountered it saves the linked objects as an executable le. 5. The IDE directs the operating system's program called the
computer's memory and have the Central Processing Unit (CPU) start processing the instructions. As the user interacts with the program, entering his test data, he might discover that the outputs are not correct. These types of errors are called logic errors and would require him to return to the source code to change the algorithm.
debugging your program. The three types of errors in the order that they occur are:
1. Compiler 2. Linker 3. Logic There are two types of compiler errors; pre-processor (1st step) and conversion (2nd step). mistake. During the conversion (2nd step) the complier might give a A review of
Figure 1 above shows the four arrows returning to the source code so that the programmer can correct the
not be a problem to worry about. For example: Data type demotion may be exactly what you want your program to do, but most compilers give a warning message. Warnings don't stop the compiling process but as their name implies, they should be reviewed. The next three gures show IDE monitor interaction for the
74
Figure 5.2:
75
Figure 5.3:
Linker Error (no red line with an error message describing linking problem)
76
Figure 5.4:
Logic Error (from the output within the "Black Box" area)
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
77
5.1.4 Denitions
Denition 5.1: text editor
A software program for creating and editing ASCII text les.
4 See the le at <http://cnx.org/content/m18920/latest/Demo_Linker_Errors.cpp> 5 See the le at <http://cnx.org/content/m18920/latest/Demo_Logic_Errors.cpp> 6 This content is available online at <http://cnx.org/content/m18835/1.5/>.
78
Figure 5.5:
Our program now located in the memory has basically two areas:
Machine instructions our instructions for what we want done Data storage our variables that we using in our program
Often our program contains instructions to interact with the input/output devices. We need to move data into (read) and/or out of (write) the memory data area. A
connected to the memory so that data can be transferred between the memory and the device. Historically this was done with punched cards and printouts. Tape drives were used for electronic storage. With time we migrated to using disk drives for storage with keyboards and monitors (with monitor output called soft copy) replacing punch cards and printouts (called hard copy). Most computer operating systems and by extension programming languages have identied the keyboard as the
standard input device and the monitor as the standard output device. Often the keyboard and
monitor are treated as the default device when no other specic device is indicated.
79
#include <iostream>
Input-Output Stream library. This le contains necessary code cout and cin for sending data to the monitor or getting data from the keyboard. cout and cin as being locations that you can send to or receive data from; similar in
concept to any other variable storage location within the data area of our program. The C++ programming language has two operators to use in conjunction with I/O devices.
C ++ operator symbol (a pair of less than symbols) (a pair of greater than symbols)
Table 5.1
int age1; // variable set up then later on in our program cout "\nEnter the age of the first person --->: "; cin age1;
Using the
cout the programmer displays (or inserts) a prompting message on the monitor for the user to cin the user types an integer value and hits the enter key and the computer extracts the value
from the keyboard and stores it into the variable named age1. Within the computer all data are stored as numbers and thus part of the technical code provided by the developers of the C++ programming language that is within the
Input-Output Stream library converts data from numbers to those symbols we are used
to seeing as humans and vice versa. Example: If the user entered the numeral digits 57 and hit the enter key the extraction operator would convert the 57 into a binary number and move the binary number into the integer storage place named age1. The
cout which uses the standard output device does not format the output into a Graphical User
Interface (GUI) where you have a mouse to use. A modern operating system using GUI normally opens a 1960's. That is the default of how
black screen output box that would be similar to how the monitor was used when rst developed in the cout is normally implemented by most compilers.
The output message has a unique item worth mentioning. At the very front of the message is a backslash
followed by the letter n. They do not get printed on the monitor. It is a special code (called a printer
escape code) telling the printer to go to a new line. Printer! I thought we were using a monitor? We are but the
code is a left over from the early days of printer output. The backslash tells the printer or monitor that the next letter is a command. The letter n is used for telling the printer or monitor to go to the front of a new line.
5.2.3 Denitions
Denition 5.8: device
A piece of equipment that is electronically connected to the memory so that data can be transferred between the memory and the device.
80
compiler directive is an instruction to the compiler to complete a task before formally starting to compile
the program, thus they are sometimes called pre-processor directives. Among other items, during the preprocessor step the compiler is looking for compiler directives and processes them as they are encountered. After completing the tasks as directed, the compiler proceeds to its second step where it checks for syntax errors (violations of the rules of the language) and converts the source code into an object code that contains machine language instructions, a data area, and a list of items to be resolved when he object le is linked to other object les. Within C++ the pound symbol or # as the rst character of a line indicates that the next word is a directive (or command word) to be evaluated. The two most common compiler directives are: 1.
include with the item following include being the name of a le that is to be inserted at that place
in the le. The les are often called "Header Files" because the include directive is normally inserted toward the top of the le (at the head) as one of the rst items.
2.
dene with the item followed by an identier name and a value. This identier name and value is
stored by the compiler and when it encounters the identier name in the program it substitutes the value for the identier name.
In the following example the include directive is inserting a le that contains code from the getting data from the keyboard.
Input-Output Stream library. This le contains necessary code to use cout and cin for sending data to the monitor or
#include <iostream>
In the next example the dene directive is being used to handle a constant (called a dened constant).
#define PI 3.14159 ....Later on in the program when it encounters PI ....it will replace or substitute PI with the value 3.14159 ....For example: area_circle = radius * radius * PI; would become: area_circle = radius * radius * 3.14159;
7 This content is available online at <http://cnx.org/content/m18787/1.7/>.
81
Of note, compiler directives in C++ do not have a semi-colon after them. instructions or statements end with a semi-colon, but not compiler directives.
5.3.2 Denitions
Denition 5.14: compiler directive
An instruction to the compiler to complete a task before formally starting to compile the program.
82
5.4.3 Exercises
Exercise 5.4.1 Answer the following statements as either true or false:
1. IDE means Integer Division Expression. 2. Most modern compilers are really an IDE type of software, not just a compiler. 3. cin and cout are used for the standard input and output in C++. 4. Programming errors are extremely easy to understand and x. 5. All C++ programs will have at least one include type of compiler directive.
(Solution on p. 85.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
10
83
Copy into your sub-folder: Chapter_05 one of the source code listings that we have used. We suggest the Lab 01 source code and rename the copy:
Lab_05.cpp
Build (compile and run) your program. You have successfully written this program if when it runs and you use the test data [use the test data as supplied as the solution for Lab 02] it gives the predicted results.
After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
5.4.6 Problems
5.4.6.1 Problem 05a Instructions
List and describe what might cause the four (4) types of errors encountered in a program using an Integrated Development Environment software product.
//****************************************************** // Filename: Compiler_Test.cpp // Purpose: Average the ages of two people // Author: Ken Busbee; Kenneth Leroy Busbee // Date: Jan 5, 2009 // Comment: Main idea is to be able to // debug and run a program on your compiler. //****************************************************** // Headers and Other Technical Items #include <iostrern> using namespace std; // Function Prototypes void pause(void); // Variables int int double age1; age2; answear;
84
//****************************************************** // main //****************************************************** int main(void) { // Input cout "\nEnter the age of the first person --->: "; cin age1; cout "\nEnter the age of the second person -->: "; cin age2; // Process answer = (age1 + age2) / 3.0; // Output cout "\nThe average of their ages is -------->: "; cout answer; pause(); return 0; } //****************************************************** // End of Program //******************************************************
85
86
Chapter 6
beginning phrase word to start the function communication phrase word to identify the items being passed into the function
communication phrase word to identify the items being passed out of the function ending phrase word to end the function Use a calling phrase word to direct your program to use a fucntion
Use an
Starting Phrase Word Function Pass In: N/A Pass Out: N/A Call:
Table 6.1
6.1.2 Examples
Here are some examples showing functions dened in pseudocode using our conventions as described above.
88
Function clear monitor Pass In: nothing Direct the operating system to clear the monitor Pass Out: nothing Endfunction
Example 6.2: pseudocode: Function with parameter passing
Function delay program so you can see the monitor Pass In: integer representing tenths of a second Using the operating system delay the program Pass Out: nothing Endfunction
Example 6.3: pseudocode: Function main calling the clear monitor function
Function main Pass In: nothing Doing some lines of code Call: clear monitor Doing some lines of code Pass Out: value zero to the operating system Endfunction
6.1.3 Denitions
Denition 6.1: phrase word
Words used to make pseudocode logic clear to any reader.
hierarchy chart (also known as a structure chart) shows the relationship of various units.
Its
name comes from its general use in showing the organization (or structure) of a business. The President at the top, then vice presidents on the next level, etc. Within the context of a computer program it shows the relationship between modules (or functions). Detail logic of the program is not presented. It does represent the organization of the functions used within the program showing which functions are calling on a subordinate function. Those above are calling those on the next level down. Hierarchy charts are created by the programmer to help document a program. picture of the modules (or functions) used in a program. They convey the big
89
Figure 6.1:
6.2.2 Denitions
Denition 6.2: hierarchy chart
Convey the relationship or big picture of the various functions in a program.
90
You should review these materials before proceeding. If you are viewing this module on-line, links to these items are in the "Links" box to your right.
take a large complicated problem and to divide it into smaller manageable pieces. The hierarchy chart of any large
organization (government unit, company, university, hospital, etc.) will show levels of people with job titles that indicate a dierent area of responsibility. Each person is a small piece of the overall workings of the organization. Each person can concentrate on their unique talent or task to make sure it works properly. Collectively they accomplish the goals of the organization. Additionally, the concept has been around for a long time. A village of 300 years ago had farmers, tailors, butchers, blacksmiths, etc. Manufacturing is a prime example of not just work being modularized but the product itself is viewed in terms of modules or systems (Example of a automobile: engine, steering, brakes, etc.). The world of computers, both hardware (equipment) and software (computer programs), also uses this modular concept. Thus, the concept migrates to a single computer program; allowing us to modularize the program into manageable tasks called functions.
parameter passing. However, with program control functions we use no communication in with no communication out. Our data
variables and constants are placed in a common area available to all functions (called global scope). The identier names for program control functions usually imply a task to be accomplished, such as get-data, process-data or show-results. As you learn to write more complicated programs the number of lines of code will increase. Prudence dictates that it would be benecial to divide the program into functions that perform unique tasks. The larger the program the more need for modularization or creating of program control functions. Depending on the programming language, there is a formal way to: 1. dene a function (it's 2.
3. declare a function (a
One of the easier ways to understand program control function is to view an example. Even if you don't know the C++ programming language, you can study the materials to help understand the modularization process.
Demo_Programs
91
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
6.3.5 Denitions
Denition 6.4: common area
An area of the program where variables and constants are dened so that they are available to all functions.
void data type has no values and no operations. It's a data type that represents the lack of a data
type.
C++ Reserved Word Represent Size Normal Signage Domain (Values Allowed)
Table 6.2
<http://cnx.org/content/m19145/latest/Demo_Program_Control_Functions_before_Compiler_Test.cpp> 5 See the le at <http://cnx.org/content/m19145/latest/Demo_Program_Control_Functions_Pseudocode.txt> 6 See the le at <http://cnx.org/content/m19145/latest/Demo_Program_Control_Functions_Hierarchy_Chart.jpg> 7 See the le at <http://cnx.org/content/m19145/latest/Demo_Program_Control_Functions.cpp> 8 This content is available online at <http://cnx.org/content/m18655/1.4/>.
92
integer data type. Some functions don't return a value of any kind. Thus, the need to have a data type that indicates
nothing is being returned. The void data type is mainly used in the denition and prototyping of
functions to indicate that either nothing is being passed in and/or nothing is being passed out.
6.4.2 Denitions
Denition 6.5: void data type
A data type that has no values or operators and is used to represent nothing.
//****************************************************** // Filename: Compiler_Test.cpp // Purpose: Average the ages of two people // Author: Ken Busbee; Kenneth Leroy Busbee // Date: Jan 5, 2009 // Comment: Main idea is to be able to // debug and run a program on your compiler. //****************************************************** // Headers and Other Technical Items #include <iostream> using namespace std; // Function Prototypes void pause(void); // Variables int int double age1; age2; answer;
93
{ // Input cout "\nEnter the age of the first person --->: "; cin age1; cout "\nEnter the age of the second person -->: "; cin age2; // Process answer = (age1 + age2) / 2.0; // Output cout "\nThe average of their ages is -------->: "; cout answer; pause(); return 0; } //****************************************************** // pause //****************************************************** void pause(void) { cout "\n\n"; system("PAUSE"); cout "\n\n"; return; } //****************************************************** // End of Program //******************************************************
Within the programming industry there is a desire to make software programs easy to maintain. The desire centers in money. Simply put, it costs less money to maintain a well written program. One important aspect of program maintenance is making source code listings clear and as easy to read as possible. To that end we will consider the following: 1. Documentation 2. Vertical Alignment 3. Appropriate use of Comments 4. Banners for Functions 5. Block Markers on Lines by Themselves 6. Indent Block Markers 7. Meaningful Identier Names Consistently Typed 8. Appropriate use of Typedef The above items are not needed in order for the source code to compile. Technically the compiler does not read the source code the way humans read the source code. But that is exactly the point; the desire is to make the source code easier for humans to read. You should not be confused between what is possible (technically will compile) and what is ok (acceptable good programming practice that leads to readable code). Let's cover each item in more detail.
94
6.5.1.1 Documentation
Documentation is usually placed at the top of the program using several comment lines. or independent contractors to write the code. Notice the indication of revision dates.
information would vary based on the requirements or standards of the company who is paying its employees
float length, width, height, price_gal_paint, total_area, total_cost; int coverage_gal_paint, total_gal_paint;
However common this is in textbooks, it would generally not be acceptable to standards used in most companies. You should declare each item on its own line; like this:
This method of using one item per line is more readable by humans. It is quicker to nd an identier name, because you can read the list vertically faster than searching horizontally. Some programmers list them in alphabetic order, especially when the number of variables exceeds about twenty. The lines of code inside either function are also aligned vertically and indented two spaces from the left. The indentation helps set the block o visually.
95
Variables, constants, functions, typedefs and other items should use meaningful identier names.
Additionally, those names should be typed consistently in terms of upper and lower case as they are used in the program. Don't dene a variable as: Pig and then type it later on in your program as: pig.
6.5.2 Denitions
Denition 6.6: documentation
A method of preserving information useful to others in understanding an information system or part thereof.
96
97
11
6.6.3 Exercises
Exercise 6.6.1 Answer the following statements as either true or false:
1. Pseudocode has a strict set of rules and is the same everywhere in the computer programming industry. 2. Hierarchy Charts and Structure Charts are basically the same thing. 3. Program Control functions are used to simply sub divide and control the program. 4. The void data type is rarely used in C++. 5. Making source code readable is only used by beginning programmers.
(Solution on p. 102.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
98
12
13 15 16
17 Download from Connexions: Solution_Lab_01m_Hierarchy_Chart.jpg 18 Download from Connexions: Solution_Lab_03.cpp 19 Download from Connexions: Solution_Lab_03_Pseudocode.txt
Download from Connexions: Solution_Lab_03_Test_Data.txt
20
Navigate to your sub-folder: Chapter_06. Review the original Lab 01 materials. Compile and run the Lab 01 source code. Then review and compare the original Lab 01 materials to modularized Lab 01 materials taking note of the conversion to "program control" functions. Compile and run the Lab 01m source code. Review as needed the course materials. Email your professor if you have any questions.
We have supplied the solution to the Lab 03 assignment. Review the Lab 03 assignment by compiling and running the Lab 03 source code. You need to copy the Lab 03 source code le and pseudocode le to make the following new les:
6.6.6 Problems
6.6.6.1 Problem 06a Instructions
Create a hierarchy chart for the following pseudocode example.
at at at at at at at at at
99
****************************************************** Filename: Average_IQ.txt Purpose: Average the IQs of two people Author: Ken Busbee; Kenneth Leroy Busbee Date: Jan 17, 2009 ****************************************************** Function main Pass In: nothing Call: get_iqs Call: process_iqs Call: show_average Pass Out: zero to the OS Endfunction ******************** Function get_iqs Pass In: nothing display a message get the IQ of the display a message get the IQ of the Pass Out: nothing Endfunction
asking user for the IQ of the first person first person from the keyboard asking user for the IQ of the second person second person from the keyboard
******************** Function process_iqs Pass In: nothing calculate the answer by adding the two IQs and dividing by 2.0 Pass Out: nothing Endfunction ******************** Function show_average Pass In: nothing display the answer with an appropriate message Call: pause Pass Out: nothing Endfunction ******************** Function pause Pass In: nothing direct the operating system to pause the program Pass Out: nothing
100
Endfunction ****************************************************** Potential Variables Data Type Identifier Name ********* *************** integer iq1 integer iq2 double answer ****************************************************** End of file
//****************************************************** // Author: Ken Busbee; 2009 Kenneth Leroy Busbee // Date: Jan 17, 2009 //****************************************************** #include <iostream> using namespace std; void pause(void); int age1, age2; double xx; //****************************************************** // main //****************************************************** int main(void) { // Input cout "\nEnter the age of the first person --->: "; cin age1; cout "\nEnter the age of the second person -->: "; cin age2; // Process xx = (age1 + age2) / 2.0;
101
// Output cout "\nThe average of their ages is -------->: "; cout xx; pause(); return 0; } void pause(void) { cout "\n\n"; system("PAUSE"); cout "\n\n"; return; } //****************************************************** // End of Program //******************************************************
102
Chapter 7
The ability to modularize our program into specic task functions means that we can write the specic task function once making sure it works correctly, then reuse it over and over in many programs. As you can guess there is a balance. Most programs will have some program control functions and some specic task functions. The key to deciding if the function should be a specic task function is usually rooted in the uniqueness of the task so that it can be used in many programs. Specic task functions once created are usually placed into a
user dened library then shared with others for use in many programs.
103
104
7.1.4 Denitions
Global scope (and by extension global data storage) occurs when a variable is dened "outside of a
function". When compiling the program it creates the storage area for the variable within the program's
data area as part of the object code. The object code has a machine code piece, a data area and linker
resolution instructions. Because the variable has global scope it is available to all of the functions within your source code. It can even be made available to functions in other object modules that will be linked to your code; however we will forgo that explanation now. A key wording change should be learned at this
the point of denition to the end of the program source code. That is why most variable with global scope are placed near
point. Although the variable has global scope, technically it is available only from the top of the source code before any functions. This way they are available to all of the functions.
Local scope (and by extension local data storage) occurs when a variable is dened "inside of a function".
When compiling, the compiler creates machine instructions that will direct the creation of storage locations on an area known as the
exist until the function completes its task and returns to its calling function. In assembly language we talk about items being pushed onto the stack and popped o the stack when the function terminates. Although the variable has local scope, technically it is available only from the, stack is a reusable area of memory being used by all functions and released as functions terminate.
the point of denition to the end of the function. The parameter passing of data items into a function establishes them as local
variables. Additionally, any other variables or constants needed by the function usually occur near the top of the function denition so that they are available during the entire execution of the function's code. Scope is an important concept to modularization. for variables and constants placing them near the top of the program before any functions.
functions use only local scope variables by passing data as needed into the function with parameter passing and creating local variables and constants as needed. Any information that needs to be communicated back to the calling function is again done via parameter passing. is used in object oriented programming. This passes all data into and out of a function creates an important predecessor concept for
7.2.2 Denitions
Denition 7.2: global scope
Data storage dened outside of a function.
105
7.3 Using a Header File for User Dened Specic Task Functions3
7.3.1 Concept: User Dened Specic Task Functions
Most companies have certain tasks that are unique to their company. Collectively the programming sta may decide to build several functions and organize them into one or more user libraries. Specic task functions are often built using a testing shell program. The sole purpose of the testing shell program is to create the specic task functions and to test them to insure that they are working properly. Think of a clam, its shell surrounds the important part, the pearl. A testing shell program surrounds the specic task function (the important part). Usually the testing shell program will be used to create several functions that will be placed into a user dened library. The process ows as follows: 1. The
2. A copy of the
testing shell program with the specic task functions is built and thoroughly tested. testing shell source code is saved as the header le that once modied will be placed
You delete the main part of the program leaving a comments area, any needed
106
Figure 7.1:
retains only the prototypes for the functions that were placed into the
testing shell source code is saved as the prototypes le. This is a text le that header le. The functions
should be using meaningful identier names, thus the prototypes should provide adequate information to others on how to call the function with appropriate parameter passing.
107
Figure 7.2:
testing shell source code is saved as the verify header program. You delete header le is working properly.
the functions prototypes and denitions then provide an include that points to the header le. This program is compiled and run to make sure the
108
Figure 7.3:
A good way to understand the concept is to review the four les described above that have been created by a programmer. We will be using the C++ programming language, however the code is easy to understand and will serve our needs well at explaining the concepts; even if you are not familiar with C++.
Monitor_Header
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
109
4 6
Monitor_Testing_Shell.cpp program.
Bloodshed Dev-C++ 5 compiler/IDE you most likely installed the compiler/IDE C:\Dev-Cpp\ if you installed it on your machine or at: DriveLetter:\Dev-Cpp\ (where the DriveLetter is the drive that represents your ash drive) if you installed it on a ash drive. We suggest
you were using the software at: that you create a sub-folder at that location named:
user_library
C:\Dev-Cpp\user_library would be created as the location for your user library if using
your machine installation. You can literally place it anywhere and name the library any name, but once you decide on a place and name; you do not want to move or rename the folders.
udst_monitor.h le placing it into the user_library folder just created. As you can
The functions within this header le would be used
guess the udst stands for user dened specic task. language and indicates a header le. descriptive.
to control the interaction a user has with the monitor. The .h is a convention of the C++ programming Thus the identier name for the header le is very meaningful and
Monitor_Verify_Header.cpp source code le and note the two include commands are dif-
C:\Dev-Cpp\user_library\udst_monitor.h
at at at at
110
Depending on what drive you are using, what path folder structure you are using and what you called your folder; you may need to correct the include reference within the source code so that it properly references the header le. Compile and run the Monitor_Verify_Header.cpp program. Note: It should work exactly as the Monitor_Testing_Shell.cpp program.
7.3.3 Denitions
Denition 7.7: udst
User Dened Specic Task
Denition 7.8:
testing shell
111
7.4.3 Exercises
Exercise 7.4.1 Answer the following statements as either true or false:
1. Scope refers to a brand of mouth wash. 2. User dened specic task functions are usually placed into a user dened library. 3. Local and global data storage is associated with the concept of scope. 4. Creating a header le for user dened specic task functions is a dicult task. 5. The stack is part of the computer's memory used for storage of data.
(Solution on p. 113.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
10
8 This content is available online at <http://cnx.org/content/m22459/1.2/>. 9 See the le at <http://cnx.org/content/m22459/latest/index.html> 10 See the le at <http://cnx.org/content/m22459/latest/Lab_07_Testing_Shell.cpp>
112
Navigate to your sub-folder: Chapter_07. Compile and run the Lab 07 Testing Shell source code. Note: This program uses an include le that points to the "udst_monitor.h" le as explained in Connexions Chapter 7 materials.
same
process
as Task
shown
in
the
Connexions is within
module the
"Using Chapter
a 7
Header
File
for
Dened
Specic
Functions"
that
materials;
make
following les: udst_us_to_metric.h and udst_us_to_metric_prototypes.txt and Lab_07_Verify_Header.cpp Copy the header le to your user library, then build (compile and run) your verify header program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
7.4.6 Problems
7.4.6.1 Problem 07a Instructions
Create the pseudocode to solve the following specic task function: Problem: An interior designer always needs to calculate the area of a room to determine the amount of oor covering needed (usually carpet). The rooms are rectangular with the dimensions measured in feet (with decimal fractions). The function however needs to return square yards. Hint: There are 3 lineal feet to a yard.
113
114
Chapter 8
8. Standard Libraries
8.1 Standard Libraries1
8.1.1 Overview of Standard Libraries
Many common or called a
standard functions, whose denitions have been written, are ready to be used in any
program. They are organized into a group of functions (think of them as several books) and are collectively
Standard Library There are many function organized into several libraries For example, within
These functions were In most cases the functions were
C++ many math functions exist and have been coded (and placed into libraries). written by programmers and tested to insure that they work properly. the advantage of using these functions with saving us time and money.
reviewed by several people to double and triple check to insure that they did what was expected. We have
A main program must establish the existence of functions used in that program. programming language, there is a formal way to: 1. dene a function 2. declare a function (a prototype is a declaration to a compiler) 3. call a function
When we create functions in our program, we usually see them in the following order in our source code listing: 1. declare the function (prototype) 2. call the function 3. dene the function When we use functions created by others that have been organized into library, we include a header le in our program which contains the prototypes for the functions. Just like functions that we create, we see them in the following order in our source code listing: 1. declaring the function (prototype provided in the include le) 2. call the function (with parameter passing of values) 3. dene the function (it is either dened in the header le or the linker program provides the actual object code from a Standard Library object area)
115
116
In most cases, the user can look at the prototype and understand exactly how the communications (parameter passing) into and out of the function will occur when the function is called. Let's look at the math example of absolute value. The prototype is:
Not wanting to have a long function name the designers named it:
seem to violate the identier naming rule of using meaningful names, however when identier names are established for standard libraries they are often shortened to a name that is easily understood by all who would be using them. value. The function is of data type int, meaning that the function will return an integer It is obvious that the integer value returned is the answer to the question, "What is the absolute
value of the integer that is being passed into the function". This function is passed only one value; an int number. If I had two integer variables named apple and banana; and I wanted to store the absolute value of banana into apple; then a line of code to call this function would be:
apple = abs(banana);
Let's say it in English, pass the function absolute the value stored in variable banana and assign the returning value from the function to the variable apple. Thus, if you know the prototype you can usually properly call the function and use its returning value (if it has one) without ever seeing the denition of the code (i.e. the source code that tells the function how to get the answer; that is written by someone else; and either included in the header le or compiled and placed into an object library; and linked during the linking step of the Integrated Development Environment (IDE).
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
2 3
8.1.3 Denitions
Denition 8.1: Standard Library
A set of specic task functions that have been added to the programming language for universal use.
117
8.2.3 Exercises
Exercise 8.2.1 Answer the following statements as either true or false:
1. The standard library is a set of specic task functions that have been added to the programming language for universal use. 2. Programmers should not have condence that standard library functions work properly. 3. It would be easier to write programs without using specic task functions.
(Solution on p. 119.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
4 This content is available online at <http://cnx.org/content/m22460/1.3/>. 5 See the le at <http://cnx.org/content/m22460/latest/index.html> 6 See the le at <http://cnx.org/content/m22460/latest/Lab_08_Testing_Shell.cpp>
118
Navigate to your sub-folder: Chapter_08. Compile and run the Lab 08 Testing Shell source code. Note: This program uses an include le that points to the "udst_monitor.h" le as explained in Connexions Chapter 7 materials.
You need to add another function to this testing shell titled: area_triangle that is to calculate the area of a triangle. Dene the function, prototype it, and within the function main add an area for calling the function (similar to the existing functions with test data). Be condent that it is working properly.
Following same process as shown in the Connexions module "Using a Header File for User Dened Specic Task Functions" that is within the Chapter 7 materials; make the following les:
Copy the header le to your user library, then build (compile and run) your verify header program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
8.2.6 Problems
8.2.6.1 Problem 08a Instructions
Write the C++ code to do the following specic task function.
Function area_regular_hexagon Pass In: side Calculate: side times side times 3 times the square root of 0.75 Pass Out: the calculation Endfunction
119
120
Chapter 9
character data type basically represents individual or single characters. Characters comprise a variety
of symbols such as the alphabet (both upper and lower case) the numeral digits (0 to 9), punctuation, etc. All computers store character data in a one byte eld as an integer value. Because a byte consists of 8 bits, this one byte eld has 28 or 256 possibilities using the positive values of 0 to 255. Most microcomputers use the
and is pronounced "ask-key") Character Set which has established values for 0 to 127. For the values of 128 to 255 they usually use the Extended ASCII Character Set. When we hit the capital A on the keyboard, the keyboard sends a byte with the bit pattern equal to an integer 65. When the byte is sent from the memory to the monitor, the monitor converts the integer value of 65 to into the symbol of the capital A to display on the monitor. The character data type attributes include:
C++ Reserved Word Represent Size Normal Signage Domain (Values Allowed)
char Single characters 1 byte Unsigned (positive values only) Values from 0 to 127 as shown in the standard ASCII Character Set, plus values 128 to 255 from the Extended ASCII Character Set
122
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
9.1.3 Denitions
Denition 9.1: character
A data type representing single text characters like the alphabet, numeral digits, punctuation, etc.
been some size changes with upgrades to the language. In "C" the int data type was allocated 2 bytes of memory storage on an Intel compatible central processing unit (cpu) machine. In "C++" an int is allocated 4 bytes. There is an operator named "sizeof (. . . )" that is a unary operator, that is it has only one operand. The operand is to the right of the operator and is placed within the parentheses if it is a data type. The operand may be any data type (including those created by typedef ). If an identier name it does not need to go inside of the parentheses. It works for both variable and memory constant identier names. This operator is unique in that it does its calculation at compile time for global scoped items and at run time for local scoped items. Examples:
123
cout
to:
"
sizeof (int);
The compiler would determine the byte size of an integer on the specic machine and in essence replaces the sizeof operator with a value. Integers are usually 4 bytes long, thus the line of code would be changed
cout
"
4;
If you place an identier name that represents a data storage area (variable or memory constant), it looks at the denition for the identier name. NOTE: the parentheses are not needed and often not included for an identier name.
double money; // variable set up with initialization then later on in the program cout "The size of money is: " sizeof money;
The compiler would determine the byte size of money by looking at the denition where it indicates that the data type is double. The double data type on the specic machine (usually 8 bytes) would replace the code and it would become:
cout
"
8;
9.2.2 Denitions
Denition 9.4: sizeof
An operator that tells you how many bytes a data type occupies in storage.
Let's say a programmer is using a double data type to store the amount of money that is being used for various purposes in a program. He might dene the variables as follows:
However, he might use the typedef statement and dene the variables as follows:
124
typedef double cash; the typedef must be defined before its use cash income; cash rent; cash vacation;
The typedef statement is not used very often by beginning programmers. It usually creates more confusion than needed, thus stick to using the normal data types at rst.
9.3.2 Denitions
Denition 9.5: typedef
Allows the programmer to create an alias, or synonym, for an existing data type.
sequence (or comma) operator is used to separate items. It has several uses, four of which are listed
then demonstrated: 1. To separate identier names when declaring variables or constants 2. To separate several parameters being passed into a function 3. To separate several initialization items or update items in a for loop 4. Separate values during the inititalization of an array This rst example is often seen in textbooks, but this method of declaring variables is not preferred. It is dicult to quickly read the identier names.
The data types and identier names (known as parameters) are separated from each other. This example is a function prototype.
ization area which could have more than one initialization. The last is the update area which could have more than one update. Mutiple initializations or updates use the comma to separate them. This example is only the rst line of a for loop.
125
9.4.2 Denitions
Denition 9.6: sequence
An operator used to separate multiple occurrences of an item.
126
9.5.3 Exercises
Exercise 9.5.1 Answer the following statements as either true or false:
1. The character data type in C++ uses the double quote marks, like: char grade = "A"; 2. Sizeof is an operator that tells you how many bytes a data type occupies in storage. 3. Typedef helps people who can't hear and is one of the standard accommodation features of a programming language for people with a learning disability. 4. The sequence operator should be used when dening variables in order to save space. 5. Programming can be both enjoyable and frustrating.
(Solution on p. 128.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
127
Review the Connexions module "Systems Development Life Cycle" within the Chapter 1 materials. Think of yourself as a programmer assigned to a project during the Implementation phase with your professor as the System Analyst.
Navigate to your sub-folder: Chapter_09. Review the rst two items provided by the system analyst which he produced during the Design phase of the Systems Development Life Cycle. These two documents historically would have been printed and be placed into a program documentation folder. The items you produce in creating the program would be added to the folder. However, shifting to our paperless view of the world, today these items might be created and stored electronically in electronic folders (which is basically what we are doing by using our sub-folder titled: Chapter_09. The third item, the hierarchy chart, would normally be produced by the programmer. is to do. Any questions ask the system analyst (aka your professor). However, given your inexperience, the system analyst has created it for you. Make sure you understand what the program
Lab_09_Pseudocode.txt le and a Lab_09_Test_Data.txt le. WARNING: Don't touch the compiler/IDE. Don't start by creating
the source code le. Creating the source code then producing the planning documentation afterwards is a bad habit that beginning programmers often acquire. NOTE: In your pseudocode document you do not need to create any pseudocode for the Standard Library or User Library functions. Just indicate that you call them from the Program Control functions. If needed, review the Connexions module "Pseudocode Examples for Functions" within the Chapter 6 materials. HINT: Copying the pseudocode and test data les from the Chapter 06 folder might be a good way to start building these items.
After you have successfully planned the document and created your test data; create the source code le naming it:
Lab_09.cpp HINTS: Using a previous source code le as your starting le makes
sense. The le in the Chapter 06 folder might be a good start. You might want to copy some of the include information from the Verify Header code in Chapter 08 into your Lab 09 source code le.
Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
9.5.6 Problems
9.5.6.1 Problem 09a Instructions
The sequence operator can be used when declaring multiple identier names for variables or constants of the same data type. Is this a good or bad programming habit and why?
8 See the le at <http://cnx.org/content/m22461/latest/Lab_09_Narrative_Description.txt> 9 See the le at <http://cnx.org/content/m22461/latest/Lab_09_Aerial_View_Center_Pivot_Irrigation.jpg> 10 See the le at <http://cnx.org/content/m22461/latest/Lab_09_Hierarchy_Chart.jpg>
128
Chapter 10
Sequence
Very boring. Simply do one instruction then the next and the next. Just do them in a
Selection
This is where you select or choose between two or more ows. The choice is decided by
asking some sort of question. The answer determines the path (or which lines of code) will be executed.
Iteration
Also known as repetition, it allows some code (one too many lines) to be executed (or
repeated) several times. The code might not be executed at all (repeat it zero times), executed a xed number of times or executed indenitely until some condition has been met. Also known as looping because the owcharting shows the ow looping back to repeat the task.
Branching
A control structure that allows the ow of execution to jump to a dierent part of the
program. This category is rarely used in modular structured programming. All high-level programming languages have control structures. All languages have the rst three categories of control structures (sequence, selection, and iteration). Most have the to the selection category) and the The concept of two basic structures there are usually language variations.
if then else structure (which belongs while structure (which belongs to the iteration category). After these
structured programming started in the late 1960's with an article by Edsger Dijkstra.
He proposed a "go to less" method of planning programming logic that eliminated the need for the branching category of control structures. The topic was debated for about 20 years. But ultimately "By the end of the 20th century nearly all computer scientists were convinced that it is useful to learn and apply the concepts of structured programming. "
130
concepts usually controls which path is selected. All of the paths from a selection control structure join back up at the end of the control structure, before moving on to the next lines of code in a program. We have mentioned that the two-way selection.
if (age > 17) { out "You can vote."; } else { cout "You can't vote."; }
counter = 0; while (counter < 5) { cout "\nI love computers!"; counter ++; }
10.1.2 Denitions
Denition 10.1: control structures
Mechanisms that allow us to control the ow of execution within a program.
131
Use indentation to show the action part of a control structure Use an ending
The sequence control structure simply lists the lines of pseudocode. The concern is not with the sequence category but with selection and two of the iteration control structures. The following are commonly used ending phrase-words:
Table 10.1
The
Do While and Repeat Until iteration control structures don't need an ending phrase-word. We
simply use the rst word, then the action part, followed by the second word with the test expression. Here are some examples:
If age > 17 Display a message indicating you can vote. Else Display a message indicating you can't vote. Endif
3 This content is available online at <http://cnx.org/content/m18649/1.13/>.
132
Case of age 0 to 17 Display "You can't vote." 18 to 64 Display "Your in your working years." 65 + Display "You should be retired." Endcase
count assigned zero While count < 5 Display "I love computers!" Increment count Endwhile
Example 10.6: pseudocode: For
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero
Example 10.8: pseudocode: Repeat Until
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one
133
10.3 Flowcharting4
10.3.1 Flowcharting Symbols
10.3.1.1 Terminal
The rounded rectangles, or terminal points, indicate the owchart's starting and ending points.
Figure 10.1
10.3.1.2 Process
The rectangle depicts a process such as a mathematical computation, or a variable assignment. Note: the C++ language equivalent is the statement.
134
Figure 10.2
10.3.1.3 Input/Output
The parallelograms designate input or output operations. Note: the C++ language equivalent is cin or cout.
Figure 10.3
10.3.1.4 Connectors
Sometimes a owchart is broken into two or more smaller owcharts. This is usually done when a owchart does not t on a single page, or must be divided into sections. A connector symbol, which is a small circle
135
with a letter or number inside it, allows you to connect two owcharts on the same page. A connector symbol that looks like a pocket on a shirt, allows you to connect to a owchart on a dierent page. On-Page Connector
Figure 10.4
O-Page Connector
Figure 10.5
10.3.1.5 Decision
The diamond is used to represent the true/false statement being tested in a decision symbol.
136
Figure 10.6
Figure 10.7
137
Figure 10.8
Figure 10.9
10.3.2 Examples
We will demonstrate various owcharting items by showing the owchart for some pseudocode.
138
10.3.2.1 Functions
Function clear monitor Pass In: nothing Direct the operating system to clear the monitor Pass Out: nothing Endfunction
Figure 10.10:
Example 10.10: pseudocode: Function main calling the clear monitor function
Function main Pass In: nothing Doing some lines of code Call: clear monitor Doing some lines of code Pass Out: value zero to the operating system Endfunction
139
Figure 10.11:
Function main
Solution_Lab_04_Pseudocode.txt Convert Temperature from Fahrenheit to Celsius Ken Busbee; 2008 Kenneth Leroy Busbee Dec 24, 2008
Pseudocode = IPO Outline input display a message asking user for the temperature in Fahrenheit get the temperature from the keyboard processing calculate the Celsius by adding subtracting 32 from the Fahrenheit temperature then multiply the result by 5 then divide the result by 9. Round up or down to the whole number. HINT: Use 32.0 when subtracting to ensure floating-point accuracy. output display the celsius with an appropriate message
140
Figure 10.12:
141
Figure 10.13:
If age > 17 Display a message indicating you can vote. Else Display a message indicating you can't vote. Endif
142
Figure 10.14:
Case of age 0 to 17 Display "You can't vote." 18 to 64 Display "Your in your working years." 65 + Display "You should be retired." Endcase
143
Figure 10.15:
count assigned zero While count < 5 Display "I love computers!" Increment count Endwhile
144
Figure 10.16:
145
Figure 10.17:
count assigned five Do Display "Blast off is soon!" Decrement count While count > zero
146
Figure 10.18:
count assigned five Repeat Display "Blast off is soon!" Decrement count Until count < one
147
Figure 10.19:
10.3.3 Denitions
Denition 10.7: owcharting
A programming design tool that uses graphical elements to visually depict the ow of logic within a function.
148
10.4.3 Exercises
Exercise 10.4.1 Answer the following statements as either true or false:
1. There are only two categories of control structures. 2. Branching control structures are rarely used in good structured programming. 3. If then else is a multiway selection control structure. 4. The while control structure is part of the branching category. 5. Pseudocode is better than owcharting.
(Solution on p. 150.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
5 This content is available online at <http://cnx.org/content/m19692/1.14/>. 6 See the le at <http://cnx.org/content/m19692/latest/index.html> 7 See the le at <http://cnx.org/content/m19692/latest/Lab_10_Pseudocode.txt>
149
Create a source code le from the Lab_10_Pseudocode.txt le. Name it: Lab_10.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
10.4.6 Problems
10.4.6.1 Problem 10a Instructions
List the four categories of control structures and provide a brief description of each category.
150
Chapter 11
if then else structure. Asking a question that has a true or false answer
if the answer to the question is true then do this else because it's false do this
In most languages the question (called a
type has two values true and false. Let's rewrite the structure to consider this:
151
152
mutually exclusive. Either the child 1) goes outside and plays or 2) sits on a chair for ve minutes.
11.1.3 Denitions
Denition 11.1: if then else
A two way selection control structure.
153
Boolean data type is also known as the logical data type and represents the concepts of true and false.
The name "Boolean" comes from the mathematician George Boole; who in 1854 published: An Investigation of the Laws of Thought. Boolean algebra is the area of mathematics that deals with the logical representation of true and false using the numbers 0 and 1. The importance of the Boolean data type within programming is that it is used to control programming structures (if then else, while loops, etc.) that allow us to implement "choice" into our algorithms. The Boolean data type has the same attributes and acts or behaves similarly in all programming languages. The rules within the C++ programming language are:
C++ Reserved Word Represent Size Normal Signage Domain (Values Allowed) C++ syntax rule
bool Logical concepts of true and false Usually 1 byte Unsigned 0 meaning false, and 1 meaning true true and false are reserved words that can be used as values in expressions
C++ concept/rule
Any value from any data type can be demoted into a Boolean data type with zero representing false and all non-zero values representing true.
Table 11.1
test expression that executes either selection (as in the: if then else) or
iteration (as in the while; do while; or for loops) based on the truthfulness or falseness of the expression. Thus,
languages, this expression must be a Boolean expression and is governed by a tight set of rules. However, in C++ every data type can be used as a Boolean expression because the value of any data type within C++ can be demoted into a Boolean value. Within most languages, expressions that yield Boolean data type values are divided into two groups. One group uses the relational operators within their expressions and the other group uses logical operators within their expressions. Within the C++ programming language the Boolean data type is one of the standard or basic data types and is a member of the integer family.
11.2.2 Denitions
Denition 11.3: Boolean
A data type representing the concepts of true and false.
154
test expression that controls program ow. This type Boolean expression because they create a Boolean answer or value when
evaluated. There are six common relational operators that give a Boolean value by comparing (showing the relationship) between two operands. If the operands are of dierent data types, implicit promotion occurs to convert the operands to the same data type.
C++ Operator
Meaning less than greater than less than or equal to greater than or equal to equality (equal to) inequality (not equal to)
Table 11.2
(Solution on p. 159.)
6. 9 == 13 7. 9 != 13 8. 9 !< 25 The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false. Be careful. equal. In math you are familiar with using this symbol = to mean equal and
to mean not
155
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
language. However this problem generally exits for all control structures within any language that requires the use of compound statements. The syntax is:
if(expression) {
4 See the le at <http://cnx.org/content/m19549/latest/Demo_Relational_Operators.cpp> 5 This content is available online at <http://cnx.org/content/m19683/1.3/>.
156
block.
The compound statement allows a group of statements to become one single entry. You used a compound statement in your rst program when you formed the body of the function main. All C++ functions contain a compound statement known as the function body. A compound statement consist of an opening brace, optional declarations, denitions, and statements, followed by a closing brace. Although all three are optional, one should be present."
11.4.3 Denitions
Denition 11.6: compound statement
A unit of code consisting of zero or more statements.
6 Behrouz A. Forouzan and Richard F. Gilberg, Computer Science A Structured Approach using C++ Second Edition (United States of America: Thompson Brooks/Cole, 2004) 100.
157
11.5.3 Exercises
Exercise 11.5.1 Evaluate the following Boolean expressions:
1. 25 2. 3 3. 4. 5.
(Solution on p. 159.)
6. 13 == 7 7. 9 != 7 8. 5 !> 7
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
7 This content is available online at <http://cnx.org/content/m19764/1.15/>. 8 See the le at <http://cnx.org/content/m19764/latest/index.html> 9 See the le at <http://cnx.org/content/m19764/latest/if_then_else_ow.gif> 10 See the le at <http://cnx.org/content/m19764/latest/Manipulation_Data_Part_2.pdf>
158
11
Create a source code le from the Lab_11_Pseudocode.txt le. Name it: Lab_11.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
11.5.6 Problems
11.5.6.1 Problem 11a Instructions
Create a table with the six relational operators and their meanings.
"\nEnter one side of the rectangle --------->: " ; side1; "\nEnter the other side of the rectangle --->: " ; side2;
if (side1 > side2) { length = side1; width = side2; } else { length = side2; width = side1; }
159
160
Chapter 12
if age is less than 18 you can't vote if age is less than 16 you can't drive else you can drive else you can vote if age is less than 21 you can't drink else you can drink
As you can see we simply included as part of the "true action" a statement and another if then else control structure. We did the same (nested another if then else) for the "false action". In our example we nested if then else control structures. Nesting could have an if then else within a while loop. Thus, the concept of nesting allows the mixing of the dierent categories of control structures.
161
162
if age equal to 18 you can now vote else if age equal to 39 you are middle aged else if age equal to 65 you can consider retirement else your age is unimportant
You get an appropriate message depending on the value of age. The last item is referred to as the default. If the age is not equal to 18, 39 or 65 you get the default message. In some situations there is no default action. Consider:
if age equal to 18 you can now vote else if age equal to 39 you are middle aged else if age equal to 65 you can consider retirement
The last if then else control structure has no "else". It's implied "else do nothing". Without the default the multiway selection could be written as a series of "if then without the else" structures. Consider:
if age equal to 18 you can now vote if age equal to 39 you are middle aged if age equal to 65 you can consider retirement
We have shown two ways to accomplish multiway selection. The choice of using nested it then else control structures or a series of if then control structures is decided on the existence of a default action (you must use nested if then else) or programmer preference if there is not a default action (you may use nested if then else or a series of if then control structures).
163
note: The test expression is within the parentheses, but this is not a function call. The parentheses
are part of the control structure. Additionally, there is no semicolon after the parenthesis following the expression.
if(age1 > age2) { cout "\n\nThe first person is older."; } else { cout "\n\nThe second person is older."; }
What if the two persons are the same age? The program incorrectly says the second person is older. To solve this we must handle all three possibilities. Consider this mulitway selection example:
if(age1 == age2) { cout "\n\nThey are the same age."; } else { if(age1 > age2) { cout "\n\nThe first person is older."; } else
164
12.1.4 Denitions
Denition 12.1: nested control structures
Placing one control structure inside of another.
test expression that controls program ow. This Boolean expression because they create a Boolean answer or value
when evaluated. The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false. There are three common logical operators that give a Boolean value by manipulating other Boolean operand(s). Operator symbols and/or names vary with dierent programming languages. The C++ programming language operators with their meanings are:
Comment
unary
Table 12.1
note:
The vertical dashes or piping symbol is found on the same key as the backslash
\.
You
use the SHIFT key to get it. It is just above the Enter key on most keyboards. It may be a solid vertical line on some keyboards and show as a solid vertical line on some print fonts. In most languages there are strict rules for forming proper logical expressions. An example is:
This expression has two relational operators and one logical operator.
rules the two relational comparisons will be done before the logical and operation. Thus:
1 && 1
or
165
We can say this in English as: It is true that six is greater than four and that two is less than or equal to fourteen. When forming logical expressions programmers often use parentheses (even when not technically needed) to make the logic of the expression very clear. Consider the above complex Boolean expression rewritten:
y
false true false true
x && y
false false false true
Table 12.2
Logical or (||) x
false false true true
y
false true false true
x ||y
false true true true
Table 12.3
!x
true false
Table 12.4
12.2.3 Examples
I call this example of why I hate "and" and love "or". Everyday as I came home from school on Monday through Thursday; I would ask my mother, "May I go outside and play?" She would answer, "If your room is clean and your homework is done then you may go outside and play." I learned to hate the word "and". I could manage to get one of the tasks done and have some time to play before dinner, but both of them. . . well, I hated "and". On Friday my mother took a more relaxed view point and when asked if I could go outside and play she responded, "If your room is clean or your homework is done then you may go outside and play." I learned to clean my room quickly on Friday afternoon. Well needless to say, I loved "or".
166
says, "After all, your Dad is reasonable!" The teenager says, "Reasonable. (short pause) Not." Maybe college professors will think that all their students studied for the exam. Ha ha! Not. Well, I hope you get the point.
(Solution on p. 178.)
< 7 || 15 > 36 > 36 || 3 < 7 14 > 7 && 5 <= 5 4 > 3 && 17 <= 7
>
&& 7
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
12.2.5 Denitions
Denition 12.3: logical operator
An operator used to create complex Boolean expressions.
167
if age equal to 18 you can vote else if age equal to 39 you're middle aged else if age equal to 65 consider retirement else age is un-important
You get an appropriate message depending on the value of age. The last item is referred to as the default. If the age is not equal to 18, 39 or 65 you get the default message. In some situations there is no default action. Consider this owchart example:
168
Figure 12.1
This owchart is of the case control structure and is used for multiway selection. The decision box holds the variable age. The logic of the case is one of equality where in the value in the variable age is compared to the listed values in order from left to right. Thus, the value stored in age is compared to 18 or is "age equal to 18". If it is true, the logic ows down through the action and drops out at the bottom of the case structure. If the value of the test expression is false, it moves to the next listed value to the right and makes another comparison. It works exactly the same as our nested if then else structure.
169
break; case 39: cout "\nYou're middle aged."; break; case 65: cout "\nConsider retirement."; break; default: cout "\nAge is un-important."; }
The rst thing you should note is that the C++ programming language does not formally have a case control structure. It does have a switch control structure but it acts dierently than the traditional case control structure. We use a break (which is a branching control structure) with the switch to make it act like the traditional case structure. This is one of the few allowable ways to use the switch with break within the C++ programming language to simulate the traditional case structure. All other uses of the switch or break are to be avoided if you are to stay within the bounds of good structured programming techniques. The value in the variable age is compared to the rst "case" (note:
words) which is the value 18 (also called the listed value) using an equality comparison or is "age equal to 18". If it is true, the cout is executed which displays You can vote. and the next line of code (the break) is done (which jumps us to the end of the control structure). If it is false, it moves on to the next case for comparison. Most programming languages, including C++, require the listed values for the case control structure be of the integer family of data types. This basically means either an integer or character data type. Consider this example that uses character data type (choice is a character variable):
switch (choice) { case 'A': cout break; case 'B': cout break; case 'C': cout break; default: cout }
"\nYou are an A student."; "\nYou are a B student."; "\nYou are a C student."; "\nMaybe you should study harder.";
170
Figure 12.2
Display "You can't vote." Display "You're in your working years." Display "You should be retired."
Using the case control structure when using non integer family or ranges of values is allowed when designing a program and documenting that design with pseudocode or owcharting. However, the implementation in most languages would follow a
if age > 0 and age <= to 17 display You can't vote. else if age is >= 18 and age <= 64
171
display You're in your working years. else display You should be retired.
the intent to cover the C++ programming language in detail, despite the fact that at times we have to cover
12.3.4 Denitions
Denition 12.5: case
A control structure that does mulitway selection.
172
programming practices.
12.4.2 Examples
12.4.2.1 break
The break is used in one of two ways; with the switch (a C++ programming structure) to make it act like a case structure (it's more common name within most programming languages) or as part of a looping process to break out of the loop. The rst usage is allowed in good structured programming and the second is not allowed in good structured programming.
switch (age) { case 18: cout break; case 39: cout break; case 65: cout break; default: cout }
"\nYou can vote."; "\nYou are middle aged."; "\nYou are at retirement age."; "\nYour current age is not important.";
The following is an unauthorized use of break in a loop and it gives the appearance that the loop will execute 8 times, but the break statement causes it to stop during the fth iteration.
12.4.2.2 continue
The continue structure is not allowed in good structured programming. The following gives the appearance that the loop will print to the monitor 8 times, but the continue statement causes it not to print number 4.
173
12.4.2.3 goto
The goto structure is not allowed in good structured programming. It is with a certain amount of hesitancy that we even show it. Many textbooks do not cover the goto. Within the C++ programming language you create a label with an identier name followed by a colon. You use the command word goto followed by the label. A label can be used before it is declared.
some lines of code; goto mynewspot; some lines of code; some lines of code; some lines of code; mynewspot: some statement; some lines of code;
//Declared label
12.4.2.4 return
The return is allowed in good structured programming, but only at the end of a function. A function should not pre-maturely end by having the logic of the function have it terminate by jumping back to the function that called it.
//****************************************************** // get data //****************************************************** void get_data(void) { // Input - Test Data - 5678.9, 5432.1 cout "\nEnter the length of the property in feet --->: "; cin property_length; cout "\nEnter the width of the property in feet ---->: ";
174
12.4.2.5 exit
Although exit is technically a pre-dened function, it is covered here because of its common usage in programming. A good example is the opening a le and then testing to see if the le was actually opened. If not, we have an error that usually indicates that we want to pre-maturely stop the execution of the program. Within the C++ programming language the exit function terminates the running of the program and in the process returns an integer value back to the operating system. It ts the denition of branching which is to jump to some other place in the program. In our example the value returned to the operating system is the value of the constant named: EXIT_FAILURE.
inData.open(filename); //Open input file if (!inData) //Test to see if file was opened { cout "\n\nError opening file: " filename "\n\n"; pause(); //Pause - user reads message exit(EXIT_FAILURE); //Allows a pre-mature jump to OS }
175
12.5.3 Exercises
Exercise 12.5.1 Evaluate the following Logical Boolean expressions:
1. 25 2. 19 3. 4.
(Solution on p. 178.)
<
&& 8
6 This content is available online at <http://cnx.org/content/m19968/1.12/>. 7 See the le at <http://cnx.org/content/m19968/latest/index.html> 8 See the le at <http://cnx.org/content/m19968/latest/Manipulation_Data_Part_3.pdf>
176
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
Compile and run the Lab_12a.cpp source code le. Understand how it works. Copy the source code le Lab_12a.cpp naming it: Lab_12b.cpp Convert the nested if then else to a switch with breaks. Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
12.5.6 Problems
12.5.6.1 Problem 12a Instructions
Flowchart the following pseudocode:
177
If age equal to 24 Display a message "You're the same age as Melinda." Else If age equal to 27 Display a message "You're the same age as Ruth." Else If age equal to 34 Display a message "You're the same age as Ben." Else Display a message "You're age is un-important." Endif Endif Endif
178
>
and &&.
<
and &&.
Chapter 13
do
some statements or action some statements or action some statements or action update the flag while the answer to the question is true
In every language that I know the question (called a
Boolean data type has two values true and false. Let's rewrite the structure to consider this:
do
some statements or action some statements or action some statements or action update the flag while expression is true
Within the do while control structure there are three attributes of a properly working loop. They are:
179
180
The English phrasing is, "You do the action while the expression is true". This is looping on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test after loop the action will always happen Latin for after) the action and update.
the test comes after the action. It is also sometimes called a post-test loop, meaning the test is post (or
do
{ statement; statement; statement; statement; // This statement updates the flag; } while (expression);
note: The test expression is within the parentheses, but this is not a function call. The parentheses
are part of the control structure. Additionally, there is a semicolon after the parenthesis following the expression.
do
{ cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? "; cin age_friend; cout "\nTogether your ages add up to: "; cout (age_user + age_friend); cout "\nDo you want to do it again? y or n "; cin loop_response; } while (loop_response == 'y');
The three attributes of a test after loop are present. The action part consists of the 6 lines that prompt for data and then displays the total of the two ages. The update of the ag is the displaying the question and getting the answer for the variable loop_response. The test is the equality relational comparison of the value in the ag variable to the lower case character of y.
181
This type of loop control is called an event controlled loop. The ag updating is an event where someone decides if they want the loop to execute again. Using indentation with the alignment of the loop actions and ag update is normal industry practice within the C++ community.
loop_response = 'y'; do { cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? "; cin age_friend; cout "\nTogether your ages add up to: "; cout (age_user + age_friend); } while (loop_response == 'y');
The programmer assigned a value to the ag before the loop and forgot to update the ag. provide a way to exit the loop (he forgot to update the ag). Consider the following code: Every time
the test expression is asked it will always be true. Thus, an innite loop because the programmer did not
do
{ cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? "; cin age_friend; cout "\nTogether your ages add up to: "; cout (age_user + age_friend); cout "\nDo you want to do it again? y or n "; cin loop_response; } while (loop_response = 'y');
No matter what the user replies during the ag update, the test expression does not do a relational comparison but does an assignment. It assigns 'y' to the variable and asks if 'y' is true? Since all non-zero values are treated as representing true within the Boolean concepts of the C++ programming language, the answer to the text question is true. Viola, you have an innite loop.
182
13.1.3 Denitions
Denition 13.2: action item
An attribute of iteration control structures.
183
Figure 13.1
Computer programming uses the concept of a ag in the same way that physical ags are used. A ag is anything that signals some information to the person looking at it.
if gender equals 'F' display "Are you pregnant?" get answer from user store in pregnant variable
184
Looking at the ag implies comparing the value in the variable to another value (a constant or the value in another variable) using a relational operator (in our above example: equality). Control structures are "controlled" by using a test expression which is usually a Boolean expression. Thus, the ag concept of "looking" at the value in the variable and comparing it to another value is fundamental to understanding how all control structures work.
have the control structure loop and perform the action again. The solution lies in looking at the ag twice.
13.2.5 Denitions
Denition 13.7: ag
A variable or constant used to store information that will normally be used to control the program.
test expression that executes either selection (as in the: if then else) or
iteration (as in the while; do while; or for loops) based on the truthfulness or falseness of the expression. Thus,
185
languages, this expression must be a Boolean expression and is governed by a tight set of rules. However, in C++ every data type can be used as a Boolean expression, because every data type can be demoted into a Boolean value by using the rule/concept that zero represents false and all non-zero values represent true. Within C++ we have the potential added confusion of the equals symbol as an operator that does not represent the normal math meaning of with C++ means:
equality that we have used for most of our life. The equals symbol assignment. To get the equality concept of math within C++ we use two equal symbols
if (pig = 'y') { cout "\nPigs are good"; } else { cout "\nPigs are bad."; }
The test expression of the control structure will always be true, because the expression is an assignment (not the relational operator of ==). It assigns the 'y' to the variable pig, then looks at the value in pig and determines that it is not zero; therefore the expression is true. And it will always be true and the else part will never be executed. This is not what the programmer had intended. Let's consider:
do
{ cout "\nPigs are good"; cout "\nDo it again, answer y or n: "; cin do_it_again } while (do_it_again = 'y');
The loop's test expression will always be true, because the expression is an assignment (not the relational operator of ==). It assigns the 'y' to the variable do_it_again, then looks at the value in do_it_again and determines that it is not zero; therefore the expression is true. And it will always be true and you have just created an innite loop. As a reminder, innite loops are not a good thing. These examples are to remind you that you must be careful in creating your test expressions so that they are indeed a question usually involving the relational operators. Don't get caught using assignment for equality.
186
repeat some statements or action some statements or action some statements or action update the flag until the answer to the question becomes true
In every language that I know the question (called a
Boolean data type has two values true and false. Let's rewrite the structure to consider this:
repeat some statements or action some statements or action some statements or action update the flag until expression becomes true
Within the repeat until control structure there are three attributes of a properly working loop. They are:
The English phrasing is, "You repeat the action until the expression becomes true". This is looping on the false. When the test expression becomes true, you stop the loop and go on with the next item in the program. Notice, because this is a test after loop the action will always happen test is post (or Latin for after) the action and update.
after loop" because the test comes after the action. It is also sometimes called a post-test loop, meaning the
13.4.3 Denitions
Denition 13.8: repeat until
A test after iteration control structure that is not available in C++.
187
13.5.3 Exercises
Exercise 13.5.1 Answer the following statements as either true or false:
1. The do while and repeat until structure act exactly the same. 2. Students sometimes confuse assignment and equality. 3. The repeat until looping control structure is available in all programming languages. 4. Because ags are often used, they are usually a special data type. 5. The do while is a test before loop.
(Solution on p. 189.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
5 This content is available online at <http://cnx.org/content/m20642/1.8/>. 6 See the le at <http://cnx.org/content/m20642/latest/index.html> 7 See the le at <http://cnx.org/content/m20642/latest/do_while_ow.gif> 8 See the le at <http://cnx.org/content/m20642/latest/Lab_13_Pseudocode.txt>
188
Create a source code le from the Lab_13_Pseudocode.txt le. Name it: Lab_13.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
13.5.6 Problems
13.5.6.1 Problem 13a Instructions
Flowchart the following pseudocode:
Do
Display "I like cheese cake!" Display "Do it again? y or n ---> " Get answer from keyboard While answer is 'y'
189
190
Chapter 14
C++ Operator ++
As C++ statements, the four examples all do the same thing. is stored in counter.
192
subtract 1 from the value of whatever was in the variable being decremented. The precedence of increment and decrement depends on if the operator is attached to the right of the operand (postx) or to the left of the operand (prex). Within C++ postx and prex do not have the same precedence.
int oldest = 44; // variable set up with initialization then later on in the code age = oldest++;
The rst use of the oldest variable is an Rvalue context where the existing value of 44 is pulled or fetched and then assigned to the variable age; then the variable oldest is incremented with its value changing from 44 to 45. This seems to be a violation of precedence because increment is higher precedence than assignment. But that is how postx increment works within the C++ programming language.
int oldest = 44; // variable set up with initialization then later on in the code age = ++oldest;
The variable oldest is incremented with the new value changing it from 44 to 45; then the new value is assigned to age. In postx age is assigned 44 in prex age is assigned 45. One way to help remember the dierence is to think of postx as being polite (use my existing value and return to increment me after the other operators are done) where as prex has an ego (I am important so increment me rst and use my new value for the rest of the evaluations).
14.1.2.5 Exercises Exercise 14.1.1 Evaluate the following items using increment or decrement:
1. True or false: x = x +1 and x+=1 and x++ all accomplish increment? 2. Given: int y = 19; and int z; what values will y and z have after: z = y; 3. Given: double x = 7.77; and int y; what values will x and y have after: y = ++x; 4. Is this ok? Why or why not? 6 * ++(age -3)
(Solution on p. 201.)
193
14.1.3 Denitions
Denition 14.1: increment
Adding one to the value of a variable.
initialization of the flag while the answer to the question is true then do some statements or action some statements or action some statements or action update the flag
In almost all languages the question (called a
data type has two values true and false. Let's rewrite the structure to consider this:
initialization of the flag while the expression is true then do some statements or action some statements or action some statements or action update the flag
Within the while control structure there are four attributes to a properly working loop. They are:
Initializing the ag Test expression Action or actions Update of the ag
194
The initialization of the ag is not technically part of the control structure, but a necessary item to occur before the loop is started. The English phrasing is, "While the expression is true, do the following actions". This is looping on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test before loop the action a meaning the test is pre (or Latin for before) the action and update.
might not happen. It is called test before loop because the test comes before the action. It is also sometimes called a pre-test loop,
statement; // This statement initializes the flag; while (expression) { statement; statement; statement; statement; // This statement updates the flag; }
note: The test expression is within the parentheses, but this is not a function call. The parentheses
loop_response = 'y'; while (loop_response == 'y') { cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? ";
195
age_friend; "\nTogether your ages add up to: "; (age_user + age_friend); "\nDo you want to do it again? y or n "; loop_response;
The four attributes of a test before loop are present. The initialization of the ag. The test is the equality relational comparison of the value in the ag variable to the lower case character of y. ag is the displaying the question and getting the answer for the variable loop_response. This type of loop control is called an event controlled loop. The ag updating is an event where someone decides if they want the loop to execute again. Using indentation with the alignment of the loop actions and ag update is normal industry practice within the C++ community. The action part consists of the 6 lines that prompt for data and then displays the total of the two ages. The update of the
loop_response = 'y'; while (loop_response == 'y') { cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? "; cin age_friend; cout "\nTogether your ages add up to: "; cout (age_user + age_friend); }
The programmer assigned a value to the ag before the loop which is correct. However, he forgot to update the ag. Every time the test expression is asked it will always be true. Thus, an innite loop because the programmer did not provide a way to exit the loop (he forgot to update the ag). Consider the following code:
loop_response = 'y'; while (loop_response = 'y') { cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? ";
196
No matter what the user replies during the ag update, the test expression does not do a relational comparison but does an assignment. It assigns 'y' to the variable and asks if 'y' is true? Since all non-zero values are treated as representing true within the Boolean concepts of the C++ programming language, the answer to the test expression is true. Viola, you have an innite loop.
loop_response = 'y'; while (loop_response == 'y'); { cout "\nWhat is your age? "; cin age_user; cout "\nWhat is your friend's age? "; cin age_friend; cout "\nTogether your ages add up to: "; cout (age_user + age_friend); cout "\nDo you want to do it again? y or n "; cin loop_response; }
The undesirable semi-colon on the end of while line causes the action of the while loop to be the "nothingness" between the closing parenthesis and the semi-colon. there will be no output. The program will innitely loop because there is no action (that is no action and no update). If this is the rst item in your program it will appear to start but
counter = 0; while (counter < 5) { cout "\nI love ice cream!"; counter++; }
197
It is set to zero (called initialization) before entering But part After executing the loop
the while loop structure and as long as it is less than 5 (ve); the loop action will be executed. of the loop action uses the increment operator to increase counter's value by one.
ve times (once for counter's values of: 0, 1, 2, 3 and 4) the expression will be false and the next line of code in the program will execute. A counting loop is designed to execute the action (which could be more than one statement) a set of given number of times. In our example, the message is displayed ve times on the monitor. It is accomplished my making sure all four attributes of the while control structure are present and working properly. The attributes are:
Initializing the ag Test expression Action or actions Update of the ag
Missing an attribute might cause an innite loop or give undesired results (does not work properly).
198
counter = 0; while (counter < age) { cout "\nI love corn chips!"; counter++; }
This loop is a counting loop similar to our rst counting loop example. The only dierence is instead of using a literal constant (in other words 5) in our expression, we used the variable age (and thus the value stored in age) to determine how many times to execute the loop. However, unlike our rst counting loop example which will always execute exactly 5 times; it is possible that the person's age is 0 (zero) and the rst time we test the expression it will be false and the action part of the loop would never be executed.
14.2.4 Denitions
Denition 14.5: while
A test before iteration control structure available in C++.
199
14.3.3 Exercises
Exercise 14.3.1 Evaluate the following items using increment or decrement:
1. True or false: 2. Given: 3. 4.
(Solution on p. 201.)
x = x - 1; and x -= 1; and x; and x; all accomplish decrement. int y = 26; and int z; what values will y and z have after: z = y++; Given: double x = 4.44; and int y; what values will x and y have after: y = x; As an expression: 10 / ++(money * 4) Is this ok? Why or why not?
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
3 This content is available online at <http://cnx.org/content/m20643/1.10/>. 4 See the le at <http://cnx.org/content/m20643/latest/index.html> 5 See the le at <http://cnx.org/content/m20643/latest/while_ow.gif> 6 See the le at <http://cnx.org/content/m20643/latest/Lab_14_Pseudocode.txt>
200
Create a source code le from the Lab_14_Pseudocode.txt le. Name it: Lab_14.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
14.3.6 Problems
14.3.6.1 Problem 14a Instructions
Flowchart the following pseudocode:
Assign counter a value of zero While counter is less than 5 Display "I love cookies!" Increment counter Endwhile
201
202
Chapter 15
test expression that compares the starting value to the stopping value. This expression is
a Boolean expression and is usually using the relational operators of either less than (for counting up) or greater than (for counting down). The term loop comes from the circular looping motion that occurs when using owcharting. The basic form of the for loop (counting up) is as follows:
for initialization of the starting value starting value is less than the stopping value some statements or action some statements or action some statements or action increment the starting value
It might be best to understand the for loop by understanding a while loop acting like a counting loop. Let's consider;
initialization of the starting value while the starting value is less than the stopping value some statements or action some statements or action some statements or action increment the starting value
1 This content is available online at <http://cnx.org/content/m20600/1.4/>.
203
204
The initialization of the ag is not technically part of the while control structure, but it is usually part of the for control structure. The English phrasing is, "For x is 1; x less than 3; do the following actions; increment x; loop back to the test expression". This is doing the action on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test before loop the action
might not happen. It is called a test before loop because the test comes before the action. It
is also sometimes called a pre-test loop, meaning the test is pre (or Latin for before) the action and update.
by a semi-colon), but this is not a function call. The parentheses are part of the control structure. Additionally, there is not a semicolon after the parenthesis following the expression.
for (counter = 0; counter < 5; counter++) { cout "\nI love ice cream!"; }
The four attributes of a test before loop (remember the for loop is one example of a test before loop) are present. The initialization of the ag. The test is the less than relational comparison of the value in the ag variable to the constant value of 5. The action part consists of the 1 line of output. The update of the ag is done with the increment operator. Using indentation with the alignment of the loop actions is normal industry practice within the C++ community.
205
for (counter = 0; counter < 5;) { cout "\nI love ice cream!"; }
The programmer assigned a value to the ag during the initialization step which is correct. However, he forgot to update the ag (the update step is missing). Every time the test expression is asked it will always be true. Thus, an innite loop because the programmer did not provide a way to exit the loop (he forgot to update the ag).
Example 15.3: C++ source code: for with multiple initializations and updates
206
Figure 15.1
15.1.5 Denitions
Denition 15.1: for
A test before iteration control structure typically used for counting.
207
Figure 15.2
This circular nature of the integer family works for both integer and character data types. In theory, it
208
should work for the Boolean data type as well; but in most programming languages it does not for various technical reasons. "In mathematics, modular arithmetic (sometimes called clock arithmetic) is a system of arithmetic for integers where numbers "wrap around" after they reach a certain value the modulus. which the day is divided into two 12 hour periods.
...
A familiar use of modular arithmetic is its use in the 12 hour clock the arithmetic of time-keeping in If the time is 7:00 now, then 8 hours later it will be 3:00. Usual addition would suggest that the later time should be 7 + 8 = 15, but this is not the answer because clock time "wraps around" every 12 hours; there is no "15 o'clock". Likewise, if the clock starts at 12:00 (noon) and 21 hours elapse, then the time will be 9:00 the next day, rather than 33:00. Since the hour number starts over when it reaches 12, this is arithmetic modulo 12.
Figure 15.3
Time-keeping on a clock gives an example of modular arithmetic." (Modular arithmetic from Wikipedia) The use of the modulus operator in integer division is tied to the concepts used in modular arithmetic.
Innite loop usually caused by missing update attribute. Loop never executes usually the text expression is wrong with the direction of the less than or greater than relationship needing to be switched. Loop executes more times than desired update not properly handled. Usually the direction of counting (increment or decrement) need to be switched.
Let's give an example of the loop executing for what appears to be for innity (the third item on our list).
209
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
15.2.4 Denitions
Denition 15.2: circular nature
Connecting the negative and positive ends of the domain of an integer family data type.
\n
Using the item from the iostream named: endl; which is short for end line.
Thus the programmer is responsible for making text show reasonably on the screen. Both of the above also allow for adequate
210
xed Do not use scientic notation but show oating-point values like integer values (numeral digits of 0 to 9 no exponent notation). showpoint Always show a decimal point for oating-point values even if there is no fractional part. precision Always show this number of digits (change n to a number like 2) to the right of the decimal point.
cout cout
setw(4) setw(8)
Note that each value had to have its own setw(n) where n is an integer number telling it how many positions to use for the output. The iomanip header le (immediately shown) will need to be included in your program.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
15.3.4 Denitions
Denition 15.5: formatting
Modifying the way the output is displayed.
211
if age is less than 18 you can't vote if age is less than 16 you can't drive else you can drive else you can vote if age is less than 21 you can't drink else you can drink
As you can see we simply included as part of the "true action" a statement and another if then else control structure. We did the same (nested another if then else) for the "false action". In our example we nested if then else control structures. Nesting could have an if then else within a while loop. Thus, the concept of nesting allows the mixing of the dierent categories of control structures. Many complex logic problems require using nested control structures. By nesting control structures (or placing one inside another) we can accomplish almost any
1 2 3 4 5 6 7
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ------------------------------------------------------------------------! 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ! 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | ! 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 | ! 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 48 | ! 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | ! 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 72 | ! 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | 77 | 84 |
212
8 9 10 11 12
We might also see that the answers could be designed as a collection of cells (each cell being exactly six spaces wide). The C++ source code to produce the above is:
Example 15.5: C++ source code: nested for loops - multiplication table
cout " "; for(across=1; across <13; across++) { cout setw(4) across " |"; } cout endl; cout " "; for(across=1; across <13; across++) { cout "------"; } cout endl; for(down=1; down <13; down++) { cout setw(4) down " !"; for(across=1; across <13; across++) { cout setw(4) down*across " |"; } cout endl; }
213
Figure 15.4:
214
Figure 15.5:
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
215
15.4.4 Denitions
Denition 15.7: complex logic
Often solved with nested control structures.
216
15.5.3 Exercises
Exercise 15.5.1 Answer the following statements as either true or false:
1. Only for loops can be counting loops. 2. The integer data type has modular arithmetic attributes. 3. The escape code of
(Solution on p. 218.)
\n
4. Nested for loops is not allowed in the C++ programming language. 5. Counting loops use all four of the loop attributes.
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
10
8 This content is available online at <http://cnx.org/content/m20809/1.6/>. 9 See the le at <http://cnx.org/content/m20809/latest/index.html> 10 See the le at <http://cnx.org/content/m20809/latest/Lab_15a.cpp>
217
Compile and run the Lab_15a.cpp source code le. Understand how it works. Copy the source code le Lab_15a.cpp naming it: Lab_15b.cpp Convert the code that is counting (all four attributes) to a for loop. Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
15.5.6 Problems
15.5.6.1 Problem 15a Instructions
Using proper C++ syntax, convert the following for loop to a while loop.
218
Chapter 16
An array is a sequenced collection of elements of the same data type with a single
called a one-dimensional array; also know as a list because we usually list the members or elements vertiHowever, strings are viewed as a one-dimensional array that visualize as listed horizontally. are an array of character data. In the "C" programming language all strings were handled as an array of characters that end in an ASCII null character (the value 0 or the rst character in the
implementation of the string class within C++ where strings are stored as a length controlled array with a maximum length of 255 characters. This string class implementation also allowed programmers to use the reserved word
string as if it were a data type. Commonly used operators and some alternatives for the
C++ Operator =
String Class Implementation Same as for standard data types Same as for standard data types
220
+ sizeof
the period
class member
Table 16.1
Most other operators are not allowed and basically do not make sense for a string data type. The above items are demonstrated in the following program.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
16.1.3 Denitions
Denition 16.1: array
A sequenced collection of elements of the same data type with a single identier name.
221
+5 + -2
We have three operators in this order: unary positive, addition, and unary negative.
The answer to
this expression is a positive 3. As you can see, one must dierentiate between when the plus sign means unary positive and when it means addition. Unary negative and subtraction have the same problem. Let's consider:
-2 - +5
The expression evaluates to negative 7. Let's consider:
7 - -2
First constants that do not have a unary minis in front of them are assumed (the default) to be positive. When you subtract a negative number it is like adding, thus the expression evaluates to positive 9.
-money money * -1
The above two expressions evaluate to the same value. In the rst line, the value in the variable money is fetched and then it's negated to a negative 6. In the second line, the value in the variable money is fetched and then it's multiplied by negative 1 making the answer a negative 6.
6 + +5
The second + sign is interpreted as unary positive. The rst + sign is interpreted as addition.
For all three lines, if the value stored in money is 6 the value of the expression is 6. Even if the value in money was negative 77 the value of the expression would be negative 77. because multiplying anything by 1 does not change its value. The operator does nothing,
222
(Solution on p. 225.)
Evaluate the following items involving unary positive and unary negative:
1. +10 - -2 2. -18 + 24 3. 4 - +3 4. +8 + - +5 5. +8 + / +5
16.2.3 Denitions
Denition 16.5: unary positive
A worthless operator almost never used.
223
16.3.3 Exercises
Exercise 16.3.1
(Solution on p. 225.)
Evaluate the following items involving unary positive and unary negative:
1. +13 - -2 2. -10 + 14 3. 4 + - 3 4. +8 - * +5
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
4 This content is available online at <http://cnx.org/content/m20810/1.8/>. 5 See the le at <http://cnx.org/content/m20810/latest/index.html> 6 See the le at <http://cnx.org/content/m20810/latest/Lab_16_Pseudocode.txt>
224
Create a source code le from the Lab_16_Pseudocode.txt le. Name it: Lab_16.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
16.3.6 Problems
16.3.6.1 Problem 16a Instructions
Describe the normal C++ operators allowed with the string data type.
225
226
Chapter 17
C++ Operator ? :
Meaning conditional
As an operator it produces a value for the expression. An easy way to explain the conditional operator is to convert an "if then else" control structure to an expression using the conditional operator.
if (age > 17) { cout "You can vote."; } else { cout "You can't vote."; }
Example 17.2: conditional = option 1
228
cout
note:
expression is of lower precedence than the insertion (writing) operator. The rst operand is a
test expression similar to those that control program ow in control structures. Boolean expression because they create a Boolean answer of
true or false. If the test is true the second operand becomes the value of the expression. If false, the third operand becomes the value of the expression. The operators of the question mark and colon separate the three operands.
17.1.2 Denitions
Denition 17.1: conditional
A trinary C++ operator that acts like an if then else control structure.
Recursion is a repetitive process in which a function calls itself. Both approaches provide 3 repetition, and either can be converted to the other's approach." Iteration is one of the categories of control
structures. It allows for the processing of some action zero to many times. Iteration is also known as looping and repetition. The math term "to iterate" means to perform the statement parts of the loop. Many problems/tasks require the use of repetitive algorithms. done with either: 1. looping control structures, specically the for loop (an iterative approach) 2. recursive calling of a function Using repetitive algorithms as the solution method occurs in many mathematical oriented problems. in include factorial, Fibonacci numbers, and the Towers of Hanoi problem. These With most programming languages this can be
are often only presented in terms of using the recursive method. However, ". . . you should understand the two major limitations of recursion. First, recursive solutions may involve extensive overhead because they use function calls. Second, each time you make a call you use up some of your memory allocation. If the
2 This content is available online at <http://cnx.org/content/m20814/1.4/>. 3 Behrouz A. Forouzan and Richard F. Gilberg, Computer Science A Structured Approach using C++ Second Edition (United
229
recursion is deep that is, if there is a large number of recursive calls then you may run out of memory. Both the factorial and Fibonacci numbers solutions are better developed iteratively."
Understanding how recursion or the iterative approaches work will be left to others. They are usually covered in detail as part of studying data structures. Our goal in covering them is to: 1. Provide you with a denition of recursion 2. Introduce the alternate solution approach of iteration The following demonstration program shows both solutions for 8! (eight factorial).
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
17.2.3 Denitions
Denition 17.2: recursion
A repetitive process in which a function calls itself.
4 Behrouz A. Forouzan and Richard F. Gilberg, Computer Science A Structured Approach using C++ Second Edition (United States of America: Thompson Brooks/Cole, 2004) 272. 5 See the le at <http://cnx.org/content/m20814/latest/Demo_Factorial.cpp>
230
17.3.3 Exercises
Exercise 17.3.1 Answer the following statements as either true or false:
1. The conditional expression acts like a case structure. 2. The conditional operator is a two part operator with three operands. 3. Recursion is one method of implementing a repetitive algorithm. 4. Recursion is always preferred over an iterative approach to a repetitive problem. 5. Factorial is usually demonstrated with an iterative approach.
(Solution on p. 232.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
6 This content is available online at <http://cnx.org/content/m20815/1.6/>. 7 See the le at <http://cnx.org/content/m20815/latest/index.html> 8 See the le at <http://cnx.org/content/m20815/latest/Lab_17a.cpp>
231
Compile and run the Lab_17a.cpp source code le. Understand how it works. Copy the source code le Lab_17a.cpp naming it: Lab_17b.cpp Convert the code that is using the if then else to a conditional expression. Convert the code that is using the conditional expression to an if then else. Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
17.3.6 Problems
17.3.6.1 Problem 17a Instructions
Using proper C++ syntax, convert the following if then else to a conditional expression.
232
Chapter 18
array is a sequenced collection of elements of the same data type with a single identier name. As such, dimension. Thus a single dimension array is also known as a list. table (a spreadsheet like Excel is a two dimension array).
the array data type belongs to the "Complex" category or family of data types. Arrays can have multiple axes (more than one axis). Each axis is a A two dimension array is commonly known as a
In real life there are occasions to have data organized into multiple dimensioned arrays. Consider a theater ticket with section, row and seat (three dimensions). This module will only cover the single dimension array. Most single dimension arrays are visualized vertically and are often called a list. Most programmers are familiar with a special type of array called a single dimension array of characters. as a horizontal stream of characters and not vertically as a list. length-controlled array and is a pre-dened data class. We refer to the individual values as members (or elements) of the array. Programming languages implement the details of arrays dierently. Because there is only one identier name assigned to the array, we have operators that allow us to reference or access the individual members of an array. The operator commonly associated with referencing array members is the members in an array. array and initialize its members. Additionally, the
Unlike other single dimension arrays, we usually envision a string Within C++ the string data type is a
index operator. It is important to learn how to dene an sizeof operator is often used to calculate the number of
dening of storage space. The square brackets (left [ and right ]) are used here to create
the array with ve integer members and the identier name of ages. The assignment with braces (that is a block) establishes the initial values assigned to the members of the array. Note the use of the sequence or comma operator. We could have done it this way:
By leaving out the ve and having initial values assigned, the compiler will know to create the array with ve storage spaces because there are ve values listed. This method is preferred because we can simply add members to or remove members from the array by changing the items inside of the braces. We could have also done this:
233
234
initial values would have been unknown values (actually there would be values there but we don't know what they would be and thus think of the values as garbage). We could assign values later in our program by doing this:
= = = = =
another Connexions module that covers accessing array members and is listed in the supplemental links provided. See: Array Index Operator.
18.1.3 Denitions
Denition 18.1: dimension
An axis of an array.
called the
array notation of dereference or more commonly index operator. As an operator it either provides the value held by the member of the array
(Rvalue) or changes the value of member (Lvalue). In the above example the member that is two osets from the front of the array (the value 26) is assigned to variable named my_age. The dereference operator of [2] means to go the 2
nd oset from the front of the ages array and get the value stored there. In this case
the value would be 26. The array members (or elements) are referenced starting at zero. The more common way for people to reference a list is by starting with one. Many programming languages reference array members starting at one, however for some languages (and C++ is one of them) you will need to
change
235
Position
zero osets from the front one osets from the front two osets from the front three osets from the front four osets from the front
C++
ages [0] ages [1] ages [2] ages [3] ages [4]
Miss America
Winner 1 2 3 4
Other Contests
1 2 3 4 5
Table 18.1
rd Place. We would be talking about the same position in the array of the ve saying that she was in 3
nalists.
ages[3] = 20;
This is an example of changing an array's value by assigning 20 to the 4
This is an Lvalue context because the array is on the left side of the
The C++ operator name is called the array index or simply the index operator and it uses the square brackets as the operator symbols.
18.2.2 Denitions
Denition 18.4: array member
An element or value in an array.
int ages[] = {49,48,26,19,16}; int counter; for (counter = 0, counter < 5, counter++) { cout ages[counter] endl; }
This second usage of the square brackets is as the the
array notation of dereference or more commonly called index operator. As an operator it provides the value held by the member of the array. For example,
236
during one of the iterations of the for loop the index (which is an integer data type) will have the value of 3. The expression ages[counter] would in essence be: ages[3]. The dereference operator of [3] means to go the 3
be 19. The array members (or elements) are referenced starting at zero. The more common way for people to reference a list is by starting with one. Many programming languages reference array members starting at one, however for some languages (and C++ is one of them) you will need to Consider:
Position
zero osets from the front one osets from the front two osets from the front three osets from the front four osets from the front
C++
ages [0] ages [1] ages [2] ages [3] ages [4]
Miss America
Winner 1 2 3 4
Other Contests
1 2 3 4 5
Table 18.2
rd Place. We would be talking about the same position in the array of the ve saying that she was in 3
nalists. Rather than using the for loop to display the members of the array, we could have written ve lines of code as follows:
int ages[] = {49,48,26,19,16}; int counter; for (counter = 0, counter < sizeof ages / sizeof ages[0], counter++) { cout ages[counter] endl; }
Within the control of the for loop for the displaying of the grades, note that we calculated the number of the members in the array by using the
When you ask for the sizeof an array identier name the answer is how many total bytes long is the array (or in other words how many bytes of storage does this array need to store its values). This will depend
237
on the data type of the array and the number of elements. it tells you how many bytes one member needs.
When you ask for the sizeof one of its members, This method allows for
member, we get the answer we want: the number of members in the array.
exible
coding.
By writing the for loop in this fashion, we can change the declaration of the array by adding or
subtracting members and we don't need to change our for loop code.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
18.3.4 Denitions
Denition 18.7: exible coding
Using the sizeof operator to calculate the number of members in an array.
238
18.4.3 Exercises
Exercise 18.4.1 Answer the following statements as either true or false:
1. The array data type is one of the standard data types in C++. 2. Arrays can have more than one dimension. 3. For loops are often used to display the members of an array. 4. When dening an array, it is preferable to specify how many members are in the array. 5. Arrays are rarely used to represent data.
(Solution on p. 240.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
5 This content is available online at <http://cnx.org/content/m21321/1.5/>. 6 See the le at <http://cnx.org/content/m21321/latest/index.html> 7 See the le at <http://cnx.org/content/m21321/latest/Lab_18_Narrative_Description.txt>
239
Create a source code le following the directions in the Lab_18_Narrative_Description.txt le. Name it: Lab_18.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
18.4.6 Problems
18.4.6.1 Problem 18a Instructions
Briey explain what an array is and list the two common operators used with arrays.
240
Chapter 19
Text File A le consisting of characters from the ASCII character code set. Text les (also know an
ASCII text les) contain character data. When we create a text le we usually think of it consisting of a series of lines. On each line are several characters (including spaces, punctuation, etc.) and we generally end the line with a return (this a character within the ASCII character code set). The return is also known as the new line character. You are most likely already familiar with the escape code of
\n
C++ to indicate a return character when used with in a literal string with the cout. A typical text le consisting of lines can be created by text editors (Notepad) or word processing programs (Microsoft Word). When using a word processor you must usually specify the output le as text (.txt) when saving it. Most
source code les are ASCII text les with a unique le extension; such as C++ using Bloodshed Dev-C++ 5 compiler/IDE) can be used to create ASCII text
.cpp, Pascal using .pas, Cobol using .cob, etc. Thus, most compiler/Integrated Development Environment software packages (such as the les.
Filename The name and its extension. Most operating systems have restrictions on which characters
can be used in lenames. Example for MS-DOS and Windows: Lab_05.txt Because some operating systems do not allow spaces, we suggest that you use the needed for spacing in a lename.
underscore where
Filespec The location of a le along with its lename. It is short for le specication. Most operating
systems have a set of rules on how to specify the drive and directory (or path through several directory levels) along with the lename. Example for MS-DOS and Windows: C:\myles\cosc_1436\Lab_05.txt Because some operating systems do not allow spaces, we suggest that you use the needed when creating folders or sub-directories.
underscore where
Open Your program requesting the operating system to let it have access to an existing le or to open
a new le. Within C++ this is accomplished by including the header le:
<fstream>
File Input/Output is
handled in C++ by using a pre-dened class of data objects, similar to the way string data type is handled. This class of objects has both data type names and functions built to specically accomplish opening and closing a le. Within your program you create a local storage variable with the data type of fstream like this:
fstream inData;
241
242
opened. Thus, opening a le uses a class member function call like this:
inData.open("C:\\myfiles\\cosc_1436\\Lab_05.txt", ios::in);
device token from the operating
The device
The two parameters passed to the function are the lespec and the method that you want to use the le (in this example as input). The function provides a returning value of a system and it is stored in the variable named inData. It is considered good programming practice to determine if the le was opened properly. token should be a non zero value. It the operating system gives you the value of zero it was not able to open the le. The reason it usually can't open a le is because the lespec is wrong (misspelled or not typed case consistent in some operating systems) or the le is not stored in the location specied. We often test the device token by using an if then control structure with the action consisting of stopping the program if it is true that you got the zero. The rst line of the if then control structure looks like this:
if (!inData)
Don't be misled by the not operator. This reads "if it is true that the token stored in inData is zero". If inData is zero, noting zero is 1 or true.
Read Moving data from a device that has been opened into a memory location dened in your program.
When reading text les that have integer or oating-point constants, the operating systems converts the text symbols to a binary number. The operator used is the extraction or read operator. An example of reading is:
inData cin
next_number next_number
This expression is similar to reading from the standard input device (aka the keyboard): The "cin" is a predened device token associated with the Standard Input and Output devices. For our le reading example you might say, "Go to the device identied by the token stored in the inData variable and read in the next value storing it in the next_number variable within my program".
Write Moving data from a memory location dened in your program to a device that has been opened.
When writing integer or oating-point data types, the operating system converts the binary number into the proper text symbols. The operator used is the insertion or write operator. An example of writing is:
outData
"Total is:
"
total
endl;
This expression is similar to writing to the standard output device (aka the monitor):
cout
"Total is:
"
total
endl;
The "cout" is a predened device token associated with the Standard Input and Output devices. For our le writing example you might say, "Go to the device identied by the token stored in the outData variable and write the items listed (the string constant then the value stored in my program variable named total then the endl or new line or the return character)".
Close Your program requesting the operating system to release a le that was previously opened.
There are two reasons to close a le. First, it releases the le and frees up the associated operation system resources. Second, if closing a le that was opened for output; it will clear the out the operating system's buer and insure that all of the data is physically stored in the output le. Some examples of closing les:
inData.close(); outData.close();
You need to study this module in conjunction with the demo le provided.
Demo_Programs
243
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
2 3
Download from Connexions: Demo_File_IO_Input.txt by the program. You should see the output as:
After you run the program use a text editor to examine the
Total is:
33.3
19.1.3 Denitions
Denition 19.1: text le
A le consisting of characters from the ASCII character code set.
user dened specic task functions that can be used with an array for normal processing. These functions are usually passed the
array along with the number of elements within the array. Some of functions also pass another piece of data needed for that particular functions task. This module covers the displaying the array members on the monitor via calling an
array function
dedicated to that task. You need to study this module in conjunction with the demo le provided.
2 See the le at <http://cnx.org/content/m21623/latest/Demo_File_IO.cpp> 3 See the le at <http://cnx.org/content/m21623/latest/Demo_File_IO_Input.txt> 4 This content is available online at <http://cnx.org/content/m21624/1.2/>.
244
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
19.2.3 Denitions
Denition 19.9: array function
A user dened specic task function designed to process an array.
dynamic memory allocation by dening the array within a local scope. Local scope variables are created during the execution of the scope also known as static memory allocation) it stores it in the data write the source
program and use the stack as the storage location instead of the data area. If you dene the array outside of a function (global area and must know how much storage space to allocate to the array when you
code. Since we don't know how many elements will be on the input le when we write the source code
dening an array with global scope will not work. But, we can determine exactly how many members we need for the array by having our program count them (step 1) so that we can then dene the array with local scope to the precise size needed. 3. We can then load the array by reading the le a second time and storing the values read into the array just created. This method is demonstrated in the demo le provided, thus you need to study this material in conjunction with the demo program.
245
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
19.3.3 Denitions
Denition 19.10: dynamic memory
Aka stack created memory associated with local scope.
user dened specic task functions that can be used with an array for normal processing. These functions are usually passed the
simple one dimension arrays also called a list. Most programmers develop a series of array along with the number of elements within the array. Some of functions also pass another piece of data needed for that particular functions task. This module covers the totaling of the members of an integer array member. The Latin name for totaling is summa sometimes shortened to the word
some parameter passing. It passes into the function the common two items of the array: its name along with the number of elements; but it also returns a value representing sum or total of the values within the array. You need to study this module in conjunction with the demo le provided. Other mathematical functions often associated with statistics such as: average, count, minimum, maximum, standard deviation, etc. are often developed for processing arrays.
7 See the le at <http://cnx.org/content/m21625/latest/Demo_Loading_Array_from_File.cpp> 8 See the le at <http://cnx.org/content/m21625/latest/Demo_Farm_Acres_Input.txt> 9 This content is available online at <http://cnx.org/content/m21626/1.2/>.
246
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
10
19.4.3 Denitions
Denition 19.12: sum
Latin for summa or a total.
247
13
19.5.3 Exercises
Exercise 19.5.1 Answer the following statements as either true or false:
1. Text les are hard to create. 2. A lespec refers to a very small (like a spec dust) le. 3. A device token is a special non zero value the operating system gives your program and is associated with the le that you requested to be opened. 4. Programmers should not worry about closing a le. 5. Where you dene an item, that is global or local scope, is rarely important.
(Solution on p. 249.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
248
14
Create a source code le following the directions in the Lab_19_Narrative_Description.txt le. Name it: Lab_19.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
19.5.6 Problems
19.5.6.1 Problem 19a Instructions
For what purpose do we use the sizeof operator with an array.
249
250
Chapter 20
sequentially step through an array, starting with the rst element. It compares each element with the value being search for, and stops when either the value is found or the end of the array is encountered. If the value being searched for is not in the array, the algorithm will search to the end of the array."
Binary search is not cover in this module. Linear search and searching for the maximum is demonstrated in the demo le provided, thus you need to study this material in conjunction with the demo program.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
1 This content is available online at <http://cnx.org/content/m21627/1.2/>. 2 Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of
251
252
20.1.3 Denitions
Denition 20.1: linear search
Using a loop to sequentially step through an array.
There are several dierent methods of bubble sorting and some methods are more ecient than others. Most use a pair of nested loops or iteration control structures. One method sets a ag that indicates that the array is sorted, then does a pass and if any elements are exchanged (switched); it sets the ag to indicate that the array is not sorted. It is executed until it makes a pass and nothing is exchanged.
3 See the le at <http://cnx.org/content/m21627/latest/Demo_Finding_Array_Member.cpp> 4 See the le at <http://cnx.org/content/m21627/latest/Demo_Farm_Acres_Input.txt> 5 This content is available online at <http://cnx.org/content/m21628/1.2/>. 6 Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of
253
Figure 20.1
The bubble sort gets its name from the lighter bubbles that move or "bubble up" to the top of a glass of soda pop. We move the smaller elements of the array to the top as the larger elements move to the bottom of the array. This can be viewed from a dierent perspective. Using an Italian salad dressing with oil, water and herbs; once shaken you can either: 1. envision the lighter oil rising to the top;
OR
2. envision the heaver water and herbs sinking to the bottom Either way is correct and this version of the code simply demonstrates the sinking to the bottom the heaver or larger elements of the array. Bubble sorting is demonstrated in the demo le provided, thus you need to study this material in conjunction with the demo program.
254
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
20.2.3 Denitions
Denition 20.4: sorting
Arranging data according to their values.
255
10
20.3.3 Exercises
Exercise 20.3.1 Answer the following statements as either true or false:
1. Linear searches require complex algorithms. 2. Functions are often created for searching for the max and min values in an array. 3. The bubble sort is an easy way to arrange data an array. 4. There is only one method of bubble sorting. 5. Sorting an array is frequently done.
(Solution on p. 257.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
11
9 This content is available online at <http://cnx.org/content/m21631/1.4/>. 10 See the le at <http://cnx.org/content/m21631/latest/index.html> 11 See the le at <http://cnx.org/content/m21631/latest/Lab_20_Narrative_Description.txt>
256
Create a source code le following the directions in the Lab_20_Narrative_Description.txt le. Name it: Lab_20.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
20.3.6 Problems
20.3.6.1 Problem 20a Instructions
Give a short explanation of bubble sorting.
257
258
Chapter 21
We can use the following expression to calculate the number of members in the array:
exible coding allows us to change the members of the array by adding or subtracting a
in the array.
typedef is to allow us to write code that can be quickly changed to handle dierent data versatile. By changing
types. There are several integer and oating-point data types that all store number values with dierent domains. If we write our code using some typedef statement, then our code becomes only our typedef commands, our code can be used to process data of a dierent data type. This is demonstrated within the demo le provided, thus you need to study this material in conjunction with the demo program.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
259
260
21.1.3 Denitions
Denition 21.1: versatile
Easily modifying code to handle another data type.
2 See the le at <http://cnx.org/content/m21629/latest/Demo_Versatile_Array_Functions.cpp> 3 See the le at <http://cnx.org/content/m21629/latest/Demo_Farm_Acres_Input.txt> 4 See the le at <http://cnx.org/content/m21629/latest/Demo_Deposit_Checks_Input.txt>
261
21.2.3 Exercises
Exercise 21.2.1 Answer the following statements as either true or false:
1. Most programmers rarely worry about eciency. 2. Modular/structured programming helps improve eciency. 3. Flexible coding helps improve eciency. 4. Who cares about indentation and alignment within source code. It's a waste of time. 5. Versatile code is a concept that is easy to understand.
(Solution on p. 263.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
5 This content is available online at <http://cnx.org/content/m21632/1.6/>. 6 See the le at <http://cnx.org/content/m21632/latest/index.html> 7 See the le at <http://cnx.org/content/m21632/latest/Lab_21_Narrative_Description.txt> 8 See the le at <http://cnx.org/content/m21632/latest/Lab_21_Input.txt>
262
Create a source code le following the directions in the Lab_21_Narrative_Description.txt le. Name it: Lab_21.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
21.2.6 Problems
21.2.6.1 Problem 21a Instructions
Explain the dierence between exible coding and versatile coding.
263
264
Chapter 22
22. Pointers
22.1 Address Operator1
22.1.1 Address Operator in C++
"Every variable is assigned a memory location whose address can be retrieved using the address operator &. The address of a memory location is called a pointer. Every variable in an executing program is allocated a section of memory large enough to hold a value of that variable's type."
global scope and use the data area for storage or local scope and use the stack for storage; you can ask
the question at what address in the memory does this variable exist. Given an integer variable named age:
address operator [which is the ampersand or &] to determine where it exists (or its
&age
pointer data type. The concept of an address and a pointer are one in the same.
A pointer points to the location in memory because the value of a pointer is the address were the data item The address operator is commonly used in two ways: 1. To do parameter passing by reference 2. To establish the value of pointers Both of these items are covered in the supplemental links to this module. You can print out the value of the address with the following code:
cout
&age;
This will by default print the value in hexadecimal. Some people prefer an integer value and to print it as an integer you will need to cast the address into a long data type:
cout
long(&age);
One additional tidbit, an array's name is by denition a pointer to the arrays rst element. Thus:
22.1.2 Denitions
Denition 22.1: address operator
The ampersand or &.
1 This content is available online at <http://cnx.org/content/m22148/1.3/>. 2 Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of
265
266
stores them on the stack using them as needed for whatever processing the functions accomplishes. This is the preferred method when calling user dened specic task functions. The called function passes back a single value as the return item if needed. This has the advantage of a
with everything being neatly passed in as values and any needed item returned back as a parameter. By necessity there are two exceptions to this closed communications model: 1. When we need more than one item of information returned by the function 2. When a copy of an argument cannot reasonably or correctly be made (example: le stream objects). These exceptions are handled by parameter passing by reference instead of passing a value. The item passed is called a
reference variable and it represents a concept of an alias for the variable. Any change made to
the reference variable is actually performed on the variable that it represents. The symbol of the ampersand is used to designate the reference variable (and it is associated with the address operator).
// prototype void process_values(int qty_dimes, int qty_quarters, double &value_dimes, double &value_quarters); // variable definitions int dimes = 45; int quarters = 33; double value_dimes; double value_quarters; // somewhere in the function main process_values(dimes, quarters, value_dimes, value_quarters); // definition of the function void process_values(int qty_dimes, int qty_quarters, double &value_dimes, double &value_quarters); { value_dimes = dimes * 0.10; value_quarters = quarters * 0.25; }
note: The ampersand must appear in both the prototype and the function denition but it does
not appear in the function call. The above example shows the basic mechanics of parameter passing by reference. demonstration program in conjunction with this module. You should study the
267
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
22.2.3 Denitions
Denition 22.3: reference variable
Used with parameter passing by reference.
pointer variable is a variable that holds the address of a memory location. "Every variable is assigned
6 The pointer data type allows us to designate a variable to hold an address
a memory location whose address can be retrieved using the address operator &. The address of a memory location is called a pointer."
or a pointer. The concept of an address and a pointer are one in the same. A pointer points to the location in memory because the value of a pointer is the address were the data item resides in the memory. Given an integer variable named age:
We can create a pointer variable and establish its value which would be the done using the
address
asterisk is used to designate that the variable int_pointer is an integer pointer [int *]. This means
that whenever we use the variable int_pointer that the compiler will know that it is a pointer that points to an integer. In order to use pointers you will need to understand the supplemental link.
22.3.2 Denitions
Denition 22.4: pointer
A variable that holds an address as its value.
4 See the le at <http://cnx.org/content/m22150/latest/Demo_Parameter_Passing.cpp> 5 This content is available online at <http://cnx.org/content/m22151/1.3/>. 6 Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of
268
stores them on the stack using them as needed for whatever processing the functions accomplishes. This is the preferred method when calling user dened specic task functions. The called function passes back a single value as the return item if needed. This has the advantage of a
with everything being neatly passed in as values and any needed item returned back as a parameter. By necessity there are two exceptions to this closed communications model: 1. When we need more than one item of information returned by the function 2. When a copy of an argument cannot reasonably or correctly be made (example: le stream objects). These exceptions could be handled by parameter passing by reference instead of passing a value. Although dierent syntax than parameter passing when using a the
reference variable; using a pointer variable and indirection operator can accomplish the same eect. The indirection operator is the asterisk or the character that we also use for multiplication. The concept of indirection is also known as dereferencing,
meaning that we are not interested in the pointer but want the item to which the address is referring or referencing.
// prototype void process_values(int qty_dimes, int qty_quarters, double * ptr_value_dimes, double * ptr_value_quarte // variable definitions int dimes = 45; int quarters = 33; double value_dimes; double value_quarters; double * ptr_value_dimes = &value_dimes; double * ptr_value_quarters = &value_quarters; // somewhere in the function main process_values(dimes, quarters, ptr_value_dimes, ptr_value_quarters); // definition of the function void process_values(int qty_dimes, int qty_quarters, double * ptr_value_dimes, double * ptr_quarters); { * ptr_value_dimes = dimes * 0.10; * ptr_value_quarters = quarters * 0.25; }
note:
The asterisk and must appear in both the prototype and the function denition when
dening the pointer variables but it does not appear in the function call when the pointers are passed into the function.
269
The above example shows the basic mechanics of the indirection operator. The use of pointers with indirection is often preferred for processing arrays. The is also known as the
int ages[] = {47, 45, 18, 11, 9}; cout ages[3]; cout *(ages + 3);
array index operator array method of dereferencing. The following couts are equivalent:
The both say, "The name of an array is a pointer; take the pointer and calculate a new address that points to the 3
rd oset by adding the correct number of bytes onto the pointer (integer data type is normally
4 bytes long 3 osets times 4 bytes is 12 bytes); then dereference that pointer (since this is an Rvalue context fetch me the value that you are pointing at) and send it to the standard output device." You should study the demonstration programs in conjunction with this module.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
8 9
22.4.3 Denitions
Denition 22.5: indirection operator
The asterisk used for dereferencing a pointer.
270
11
22.5.3 Exercises
Exercise 22.5.1 Answer the following statements as either true or false:
1. The address operator is the @ symbol. 2. Passing by reference should be used when there is only one item to be modied. 3. Variables of pointer data type are dened using an asterisk. 4. Using pointers with the indirection operator can be used instead of passing variables by reference. 5. There are two kinds of dereferencing one with the indirection operator and the other with the index operator.
(Solution on p. 272.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
12
10 This content is available online at <http://cnx.org/content/m22153/1.4/>. 11 See the le at <http://cnx.org/content/m22153/latest/index.html> 12 See the le at <http://cnx.org/content/m22153/latest/Lab_22_Pseudocode.txt>
271
Create a source code le from the Lab_22_Pseudocode.txt le. Name it: Lab_22.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
22.5.6 Problems
22.5.6.1 Problem 22a Instructions
Give a general explanation of the pointer data type and the use of addresses and dereferencing. Include both the indirection operator and the index operator in your discussion.
272
Chapter 23
array is a sequenced collection of elements of the same data type with a single identier name. As such, dimension. Thus a single dimension array is also known as a list. table (a spreadsheet like Excel is a two dimension array).
the array data type belongs to the "Complex" category or family of data types. Arrays can have multiple axes (more than one axis). Each axis is a A two dimension array is commonly known as a
In real life there are occasions to have data organized into multiple dimensioned arrays. Consider a theater ticket with section, row and seat (three dimensions). We refer to the individual values as members (or elements) of the array. Programming languages implement the details of arrays dierently. Because there is only one identier name assigned to the array, we have operators that allow us to reference or access the individual members of an array. The operator commonly associated with referencing an
array member is the index operator. It is oset from the front of the array.
important to learn how to dene an array and initialize its members. The index operator is a set of square brackets with an integer value between the brackets that represents the Multidimensional arrays use one set of square brackets per dimension or axis of the array. For example a table which has two dimensions would use two sets of square brackets to dene the array variable and two sets of square brackets for the index operators to access the members of the array. Because of the complexity for multidimensional arrays, the demonstration program shows a two dimension array and you should study it in conjunction with this module.
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
273
274
23.1.3 Denitions
Denition 23.1: dimension
An axis of an array.
#define DEBUG 1
Then using another compiler directive pair, the #if and #endif, we can have the compiler during the pre-processor either include or not include one or more lines of code.
275
Demo_Programs
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
23.2.3 Denitions
Denition 23.7: conditional compilation
A compiler directive that includes or excludes lines of code based on a Boolean expression.
276
23.3.3 Exercises
Exercise 23.3.1 Answer the following statements as either true or false:
1. Very few arrays need more than one axis. 2. Multidimensional arrays use multiple square brackets, one set per axis. 3. Using a ag to activate debugging lines of code has been around since the 1950s. 4. Within C++ we can use the conditional compilation compiler directives to implement debugging line of code.
(Solution on p. 278.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
5 This content is available online at <http://cnx.org/content/m22205/1.3/>. 6 See the le at <http://cnx.org/content/m22205/latest/index.html> 7 See the le at <http://cnx.org/content/m22205/latest/Lab_23a.cpp>
277
Compile and run the Lab_23a.cpp source code le. Understand how it works. Copy the source code le Lab_23a.cpp naming it: Lab_23b.cpp Add conditional compilation statements similar to the demonstration program used in the Conditional Compilation Connexions module. Specically use: 157 pennies, 92 nickels, 23 dimes and 31 quarters as your test data.
Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
23.3.6 Problems
23.3.6.1 Problem 23a Instructions
Give three examples in the real world where data might be structured into a multidimensional array. One example (and you can't count it) is a theatre ticket which might have a section, row and seat number on it.
278
Chapter 24
procedural programming. Coupled with the imposition of using standardized modular structured programming. Another, equally valid approach to programming is object-oriented programming or OOP. It was
introduced in the mid 1980s and was widely accepted as a programming approach by the early 1990s. The rst languages to introduce OOP to the masses were C++ and Java. Shortly after their introduction, there were American National Standards Institute (ANSI) standards established for those languages. Today, C++ and Java are widely used. "The primary dierences between the two approaches is their use of data. In a procedural program, the design centers around the rules or procedures for processing the data. The procedures, implemented as functions in C++, are the focus of the design. The data objects are passed to the functions as parameters. The key question is how the functions will transform the data they receive for either storage or further processing. Procedural programming has been the mainstay of computer science since its beginning and is still heavily used today. In an object-oriented program, abbreviated OOP, the design centers around objects that contain (encapsulate) the data and the necessary functions to process the data. In OOP, the objects own the functions that process the data."
"Object-oriented programming
...
contains data and the procedures that operate on the data. The objects contain, within themselves, both the information and the ability to manipulate the information."
To help complicate the picture, the C++ programming language can be used (and is used) to write either a procedural program (modular structured program) or an object-oriented program. Some items used by those writing procedural programs in C++ are in fact objects. Examples include:
America: Pearson Addison Wesley, 2008) 22. 3 Behrouz A. Forouzan and Richard F. Gilberg, Computer Science A Structured Approach using C++ Second Edition (United States of America: Thompson Brooks/Cole, 2004) 156. 4 Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of America: Pearson Addison Wesley, 2008) 22.
1 This content is available online at <http://cnx.org/content/m22188/1.3/>. 2 Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of
279
280
1. Standard input and output items of: cout and cin; example: cout.setf(ios::xed) 2. Strings; calculating the length with: identier_name.length() 3. File input/output; example: inData.open(lespec, ios::in) Objects are implemented with a "class" data type; which is a complex or derived data type. Implementation details will not be presented in the module.
24.1.2 Transition
Many students will learn modular structured programming before learning object-oriented programming. The common way of teaching programming fundamentals is to cover them or divide them into three courses, usually covered in this order: 1. Modular structured 2. Object-oriented 3. Data structures The following items learned in modular structured programming ow into the learning of object-oriented programming: 1. The standard and complex data types are the same 2. The operators are the same, thus data manipulation is the same 3. The control structures are the same 4. Concepts of documentation and making code readable are the same 5. The use of test data to verify logical thinking and program results is similar
24.1.3 Denitions
Denition 24.1: procedural programming
Aka modular structured programming.
"Best Module" award for the "Parallel Algorithms and Applications" category
which included a US $500 prize. Those who reviewed the entries for the competition made some suggestions for improvement and most have been incorporated into this revised edition of the module. As always; my thanks to them and all others who make suggestions for improving educational materials. Kenneth Leroy Busbee
281
High Performance Computing (HPC). We want to explain how this is accomplished using parallel
Example 24.1
After eating all you can, you toss your chicken leg bone out of the car window (shame on you for trashing up the highway), but in short order an ant nds your tossed chicken bone. One single ant could bite o the left over on the bone and transport it to the colony, one bite at a time; but, it might take him 1 whole day (24 hours) of work. But, what if he gets help? He signals some buddies and being a small colony of ants they allocate a total of 10 ants to do the task. Ten times the workers take one tenth the time. The ten ants do the task in 2 hours and 24 minutes. I toss another bone out the window. An ant nds it and the colony allocates 50 ants to do the task of picking the bone clean. In less than 30 minutes (28.8 to be exact) the 50 ants working in parallel complete the task.
Example 24.2
One painter might take 8 hours to paint the exterior of an average sized house. But, if he can put a crew of 10 painters working simultaneously (or in other words in parallel) it takes only 48 munities. What about a crew of 50 painters assuming that they can do work and not get in the way of each other; well how about less than 10 minutes (9.6 to be exact). Now let's make sure we understand that the same amount of work was done in the examples given. The work was only completed in a shorter amount of time because we put more workers on the task. Not all tasks can be divided up in this way, but when it can be divided between multiple workers, we can take advantage of the workers doing their sub part of the task in parallel. Let's look at another example.
Example 24.3
I want to drive from Houston, Texas to Dallas, Texas; a distance of about 250 miles. For easy calculations let's say I can travel 50 miles in one hour. It would take me 5 hours. Well, I could divide the task between 5 cars and have each car travel 50 miles and arrive in Dallas in 1 hour. Right? Well, wrong. The task of driving from Houston to Dallas cannot be divided into tasks that can be done in parallel. The task can only be done by one person driving in a line from Houston to Dallas in 5 hours. I used the word "line" because it helps connect us to the word: with linear concept is
smaller tasks to be done in parallel by multiple workers. Within the computer world, the word associated
sequential processing. I must drive one mile at a time in sequence to get to Dallas.
Our natural tendency is to share the work that is to work in parallel whenever it is possible. As a group we can accomplish many tasks that can be done in parallel in less time.
24.2.2.2 The Birth of Computers A "Parallel" to Central Processing Unit (CPU) Story
ENIAC, short for Electronic Numerical Integrator And Computer, was the rst general-purpose electronic computer (July 1946). It was the rst Turing-complete, digital computer capable of being reprogrammed to solve a full range of computing problems. ENIAC had twenty ten-digit signed accumulators which used ten's complement representation and could perform 5,000 simple addition or subtraction operations between any of them and a source (e.g., another accumulator, or a constant transmitter) every second. It was possible to connect several accumulators to run simultaneously, so the peak speed of operation was potentially much higher due to parallel operation. (ENIAC from Wikipedia)
282
was a
parallel processing machine by using several accumulators to improve the speed. However, this did
not last for long. During its construction: The First Draft of a Report (commonly shortened to First Draft) on the EDVAC Electronic Discrete Variable Automatic Computer was an incomplete 101 page document written by John von Neumann and distributed on June 30, 1945 by Herman Goldstine, security ocer on the classied ENIAC project. It contains the rst published description of the logical design of a computer using the stored-program concept, which has come to be known as the von Neumann architecture. (First Draft of a Report on the EDVAC from Wikipedia) The von Neumann architecture is a design model for a stored-program digital computer that uses a [central] processing [unit] and a single separate storage structure to hold both instructions and data. It is named after the mathematician and early computer scientist John von Neumann. Such computers implement a universal Turing machine and have a sequential architecture. (Von Neumann architecture from Wikipedia) Von Neumann also proposed using a binary (base 2) numbering system for the electronics. One of the characteristics of the von Neumann architecture was the trade o of multiple processors using base 10 electronics to a single central processor using base 2 (or digital) electronics. To compare to our ant example, the idea was to use one real fast ant versus 10 slow ants. If one real fast ant can do 1,000 tasks in an hour; it would be more powerful (be able to do more tasks) than 10 ants doing 10 tasks an hour or the equivalent of 100 tasks per hour. The rest is history most commercially built computers for about the rst forty years (1951 to 1991) followed the von Neumann architecture. The electronic engineers keep building more reliable and faster electronics. From vacuum tube, to transistor, to integrated circuit to what we call today "chip" technology. This transformation made computers break down less frequently (they were more reliable), physically smaller, needing less electric power and faster. Personal computers were introduced in the late 1970's and within ten years became more commonly available and used. One short coming was that most programming eorts were towards improving the linear (or sequential) way of thinking or solving a problem. After all, the computer electronic engineers would be making a faster computer next year. Everyone understood that the computer had only one Right?
But it's not until the 1980's that we see the rst par-
allel processing computers (built by Cray and other computer companies) being sold as commercial built
Example 24.4
The circus traveling by train from one city to the next has an elephant that dies. They decide to toss the elephant o the train (shame on them for trashing up the country side), but in short order a "super" ant (faster than most regular ants) nds the elephant. This project is much larger than your tossed chicken bone. One single "super" ant could do the task (bite o a piece of the elephant and transport it to the colony, one bite at a time); but, it might take one whole year. After all this requires a lot more work than a chicken bone. But, what if he gets help? He signals some buddies and being a large colony of "super" ants they allocate a total of 2,190 ants to do the task. Wow, they devour the elephant in six hours. This elephant example is exactly where the computer scientists had arrived. The electronic engineers were going to continue to make improvements in the speed of a single central processing unit computer, but
immense computing power. Some of the new tasks that would require immense computer power included the human genome
not soon enough to satisfy the "need for power" to be able to solve tasks requiring project, searching for oil and gas by creating 3 dimensional images of geological formations and the study of gravitational forces in the universe; just to mention a few. The solution: parallel processing to the
283
rescue. Basically the only way to get this immense computer power was to implement parallel processing techniques. During the late 1970's and early 1980's scientists saw the need to explore the parallel processing paradigm more fully and thus the birth of High Performance Computing. Various national and international conferences started during the 1980's to be able to further the cause of High Performance Computing. For example in November of 2008 the "SC08" supercomputing conference celebrated their 20
th anniversary.
The predicting of the weather is a good example for the need of High Performance Computing. Using the fastest central processing unit computer it might take a year to predict tomorrow's weather. The information would be correct but 365 days late. Using parallel processing techniques and a powerful "high performance computer", we might be able to predict tomorrow's weather in 6 hours. Not only correct, but in time to be useful.
FLOPS (or ops or op/s) is an acronym meaning FLoating Operations Per Second." and again "On May 25, 2008, an American military supercomputer built by
IBM, named 'Roadrunner', reached the computing milestone of one petaop by processing more than 1.026 quadrillion calculations per second." (FLOPS from Wikipedia) For those of us not familiar:
24.2.2.6 Summary
We have bounced you back and forth between sequential and parallel concepts. We covered our natural
tendency to do work in parallel. But with the birth of computers the parallel concepts were set to the side and the computer industry implemented a faster single processor approach (sequential). We explained the limitations of sequential processing and the need for computing power. Thus, the birth of High Performance Computing. Parallel processing computers are migrating into our homes. With that migration, there is a great need to educate the existing generation and develop the next generation of scientists and programmers to be able to take advantage of High Performance Computing.
284
rst is a "Linear to Parallel Calculator" where the student enters how long it would take one person to complete a task, asks how many people will work as a group on the task, then calculates how long it will take the group to complete the task. The second is a "Parallel Speed Demonstration Program" that simulates parallel processing. It displays to the monitor the rst 60 factorial numbers in 60 seconds, then shows as if 10 processors are doing it in 6 seconds, then as if 100 processors are doing it in less than 1 second. Both are compiled and ready for use on an Intel CPU machine (compiled for use on Windows OS). Download the executable le from Connexions: Linear to Parallel Calculator
Download the executable le from Connexions: Parallel Speed Demonstration Program via Internet connections for parallel processing.
An interesting activity would be to join a group that is using thousands of personal microcomputers Several distributed processing projects are listed in the "FLOPS" article on Widipedia. One such group is the "Great Internet Mersenne Prime Search - GIMPS". A link to the GIMPS web site is: http://www.mersenne.org/ be dicult to focus on the precise subject that you want.
Another activity is to "Google" some keywords. Be careful - "Googling" can be confusing and often can
9 10
Download the source code le from Connexions: Parallel Speed Demonstration Program
Another appropriate activity is to "Google" some of the key words listed above. With your fundamental understanding of programming, you will understand more of the materials than those with no programming experience. You should get a sense that parallel programming is becoming a more important part of a computer professional's work and career. Review the "Top 500 Super Computers" at: http://www.top500.org/ these on your normal computer.
11
Look at the source code listings provided in the next section, but remember, you cannot compile or run
6 See the le at <http://cnx.org/content/m19804/latest/Demo_Linear_to_Parallel.exe> 7 See the le at <http://cnx.org/content/m19804/latest/Demo_Parallel_Speed.exe> 8 http://www.mersenne.org/ 9 See the le at <http://cnx.org/content/m19804/latest/Demo_Linear_to_Parallel.cpp> 10 See the le at <http://cnx.org/content/m19804/latest/Demo_Parallel_Speed.cpp> 11 http://www.top500.org/
285
1. During the past few years super-micro computers have become more powerful and more available. 2. Desk top computers are starting to be built with multiple processors (or cores) and we will have multiple (10 to 30) core processors within a few years. 3. Use of super-micro computing power is wide spread and growing in all areas: engineering applications, 3D animation for computer games and education, etc. 4. There is a shortage of educators, scientic researchers, and computer professionals that know how to manage and utilize this developing resource. Computer professionals needed include: Technicians that know how to create and maintain a super-micro computer; and Programmers that know how to create computer applications that use parallel programming concepts. This last item was emphasized to those of you beginning a career in computer programming that as you progress in your education, you should be aware of the changing nature of computer programming as a profession. Within a few years scientic research,
programming.
During the conference this author wrote a program that sorts an array of 150,000 integers using two dierent approaches. The rst way was without parallel processing. When it was compiled and executed The second way was to redesign the When it was compiled and That's using a single machine, it took 120.324 seconds to run (2 minutes).
program so parts of it could be run on several processors at the same time. approximately 6 times faster. Thus,
executed using 11 machines within a cluster of micro-computers, it took 20.974 seconds to run.
parallel programming will become a necessity to be able to utilize the multi-processor hardware of the near future.
A distributed computing environment was set up in a normal computer lab using a Linix operating system stored on a CD. After booting several computers with the CD, the computers can communicate with each other with the support of "Message Passing Interface" or MPI commands. This model known as the Bootable Cluster CD (BCCD) is available from: Bootable Cluster CD University of Northern Iowa at: http://www.bccd.net/ lename.
12
The source code les used during the above workshop were modied to a version 8, thus an 8 is in the The non-parallel processing "super" code was named: nonps8.cpp with the parallel processing "super" code named: ps8.cpp (Note: The parallel processing code contains some comments that describe that part of the code being run by a machine identied as the "SERVER_NODE" with a part of the code being run by the 10 other machines (the Clients). The client machines communicate critical information to the server node using "Message Passing Interface" or MPI commands.) You may need to right click on the link and select "Save Target As" in order to download these source code les. Download the source code le from Connexions: nonps8.cpp
13
Two notable resources with super computer information were provided by presenters during the workshop: http://www.oscer.ou.edu/education.php
15
Center
for
Education
&
Research
at:
12 http://www.bccd.net/ 13 See the le at <http://cnx.org/content/m19804/latest/nonps8.cpp> 14 See the le at <http://cnx.org/content/m19804/latest/ps8.cpp> 15 http://www.oscer.ou.edu/education.php
286
16
High Performance Computing. Consider reviewing the "Educator Resources" links provided in the next section.
17
18 CSERD Computational Science Education Reference Desk at: http://www.shodor.org/refdesk/ 19 National Computational Science Institute at: http://www.computationalscience.org/
Association of Computing Machinery at: http://www.acm.org/
20
21
287
23
24.3.3 Exercises
Exercise 24.3.1 Answer the following statements as either true or false:
1. Procedural programming and object-oriented programming cannot be done with the same compiler/IDE. 2. Object-oriented programming encapsulates data and functions. 3. High Performance Computing is a new topic on the computer scene. 4. The concepts and examples of High Performance Computer are dicult to explain. 5. All programmers will need to know about parallel programming in the near future.
(Solution on p. 289.)
If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.
24
22 This content is available online at <http://cnx.org/content/m22194/1.6/>. 23 See the le at <http://cnx.org/content/m22194/latest/index.html> 24 See the le at <http://cnx.org/content/m22194/latest/Lab_24_Narrative_Description.txt>
288
Create a source code le following the directions in the Lab_24_Narrative_Description.txt le. Name it: Lab_24.cpp Build (compile and run) your program. After you have successfully written this program, if you are taking this course for college credit, follow the instructions from your professor/instructor for submitting it for grading.
24.3.6 Problems
24.3.6.1 Problem 24a Instructions
Describe the fundamental dierences between procedural (modular structured) programming and objectoriented programming.
289
290
Chapter 25
Review Materials
291
292
Practice module for that chapter. We suggest the following test preparation strategies:
1. If your professor is testing the denitions and expecting you to have them memorized, you should review the Using the Flash Card Activity within the Study Habits that Build the Brain module within the Appendix materials. Practice writing your denitions using the Flash Card Activity in the Memory Building Activities (MBAs) available within the Activities within this
Review module.
2. Do a quick review of any exercises within the Connexions modules or the why you missed a question.
review quizzes or exams that you have taken and pay special attention to making sure you understand 3. If your professor has indicated that they might include a few of the problems presented within the
Practice modules, make sure you have formulated a good answer for each problem. If authorized,
collaborate with other students to improve your answers to the problems. Spend a moderate amount of time reviewing each problem with its answer before the exam.
1 This content is available online at <http://cnx.org/content/m22418/1.5/>. 2 See the le at <http://cnx.org/content/m22418/latest/index.html> 3 See the le at <http://cnx.org/content/m22418/latest/Manipulation_Data_Part_1.pdf>
293
Practice module for that chapter. We suggest the following test preparation strategies:
1. If your professor is testing the denitions and expecting you to have them memorized, you should review the Using the Flash Card Activity within the Study Habits that Build the Brain module within the Appendix materials. Practice writing your denitions using the Flash Card Activity in conjunction with the Memory Building Activities (MBAs) available within the in the Memory Building Activities within this
Practice modules or
2. Do a quick review of any exercises within the Connexions modules or the why you missed a question.
review quizzes or exams that you have taken and pay special attention to making sure you understand 3. If your professor has indicated that they might include a few of the problems presented within the
Practice modules, make sure you have formulated a good answer for each problem. If authorized,
collaborate with other students to improve your answers to the problems. Spend a moderate amount of time reviewing each problem with its answer before the exam.
294
Practice module for that chapter. We suggest the following test preparation strategies:
1. If your professor is testing the denitions and expecting you to have them memorized, you should review the "Using the Flash Card Activity" within the "Study Habits that Build the Brain" module within the Appendix materials. Practice writing your denitions using the Flash Card Activity in conjunction with the Memory Building Activities (MBAs) available within the in the Memory Building Activities within this
Practice modules or
2. Do a quick review of any exercises within the Connexions modules or the why you missed a question.
review quizzes or exams that you have taken and pay special attention to making sure you understand 3. If your professor has indicated that they might include a few of the problems presented within the
Practice modules, make sure you have formulated a good answer for each problem. If authorized,
collaborate with other students to improve your answers to the problems. Spend a moderate amount of time reviewing each problem with its answer before the exam.
8 9
10 Link to: Animated gif showing a while loop 11 Link to: Manipulation of Data Part 2 12 Link to: Manipulation of Data Part 3
6 This content is available online at <http://cnx.org/content/m21653/1.6/>. 7 See the le at <http://cnx.org/content/m21653/latest/index.html> 8 See the le at <http://cnx.org/content/m21653/latest/if_then_else_ow.gif> 9 See the le at <http://cnx.org/content/m21653/latest/do_while_ow.gif> 10 See the le at <http://cnx.org/content/m21653/latest/while_ow.gif> 11 See the le at <http://cnx.org/content/m21653/latest/Manipulation_Data_Part_2.pdf> 12 See the le at <http://cnx.org/content/m21653/latest/Manipulation_Data_Part_3.pdf>
295
Practice module for that chapter. We suggest the following test preparation strategies:
1. If your professor is testing the denitions and expecting you to have them memorized, you should review the "Using the Flash Card Activity" within the "Study Habits that Build the Brain" module within the Appendix materials. Practice writing your denitions using the Flash Card Activity in conjunction with the Memory Building Activities (MBAs) available within the in the Memory Building Activities within this
Practice modules or
2. Do a quick review of any exercises wtihin the Connexions modules or you missed a question.
quizzes or exams that you have taken and pay special attention to making sure you understand why 3. If your professor has indicated that they might include a few of the problems presented within the
Practice modules, make sure you have formulated a good answer for each problem. If authorized,
collaborate with other students to improve your answers to the problems. Spend a moderate amount of time reviewing each problem with its answer before the exam.
14
296
Practice module for that chapter. We suggest the following test preparation strategies:
1. If your professor is testing the denitions and expecting you to have them memorized, you should review the "Using the Flash Card Activity" within the "Study Habits that Build the Brain" module within the Appendix materials. Practice writing your denitions using the Flash Card Activity in conjunction with the Memory Building Activities (MBAs) available within the in the Memory Building Activities within this
Practice modules or
2. Do a quick review of any exerecises within the Connexions modules or the why you missed a question.
review quizzes or exams that you have taken and pay special attention to making sure you understand 3. If your professor has indicated that they might include a few of the problems presented within the
Practice modules, make sure you have formulated a good answer for each problem. If authorized,
collaborate with other students to improve your answers to the problems. Spend a moderate amount of time reviewing each problem with its answer before the exam.
16
Appendix
26.1 Abbreviated Precedence Chart for C++ Operators1
An
operator is a language-specic syntactical token (one or more symbols) that causes an action to be
taken on one or more operands. The following item provides an abbreviated list of those C++ operators that are typically taught in a programming fundamentals course that teaches modular structured programming concepts. The rst column shows the precedence (the higher precedence is 1 or it goes rst) and operators that have the same precedence also have the same associativity (the associativity is only listed once for the group of operators). Decrement is two minus signs, but some word processing software programs might have problems printing two minus signs and convert it to a double dash. Insertion (two (two
<
>
signs) might also have printing problems. These printing problems are noted in the comments with
emphasized text.
PR
1
OPERATOR NAME
function call
SYMBOL(S)
()
COMMENTS
index
[]
(Sec-
class member
a period
Right to Left
(Sec-
postx ment
incre-
++
unary
(Sec-
postx ment
decre-
(Sec-
indirection
(Sec-
298
APPENDIX
address & unary m22148 tion 22.1) (Sec-
unary positive
(Sec-
unary negative
(Sec-
prex ment
incre-
++
unary
(Sec-
prex ment
decre-
(Sec-
cast
(type)
(Sec-
sizeof
sizeof (type)
unary
(Sec-
logical NOT
unary
(Sec-
multiply
Left to Right
(Sec-
divide
(Sec-
modulus
remainder
(Sec-
add
Left to Right
(Sec-
subtract
(Sec-
insertion
writing,
less signs
6 extraction reading,
two than
Left to Right
(Sec-
(Sec-
less than
<
Left to Right
(Sec-
APPENDIX
7 greater than
299
>
or
(Sec-
less
than
<= >=
== equal to Left to Right
(Sec-
(Sec-
(Sec-
inequality
!=
not equal to
(Sec-
logical AND
&&
Left to Right
(Sec-
10
logical OR
||
Left to Right
(Sec-
11
conditional
? :
trinary
Left to Right
(Sec-
12
assignment
Right to Left
(Sec-
12
addition signment
as-
+=
(Sec-
12
subtraction assignment
-=
(Sec-
12
multiplication assignment
*=
(Sec-
12
division assignment
/=
(Sec-
12
modulus signment
as-
%=
(Sec-
13
sequence comma
or
Left to Right
(Sec-
Table 26.1
300
APPENDIX
and and_eq asm auto bitand bitor bool break case catch char class compl const const-cast continue default delete do
double dynamic_cast else enum explicit extern false oat fro friend goto if inline int long mutable namespace new not
not_eq operator or or_eq private protected public register reinterpret-cast return short signed sizeof static static_cast struct switch template this
Table 26.2
throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while xor xor_eq
APPENDIX
301
Notepad within Windows OS and it uses by default the .txt extension. Microsoft Word by saving the le as 'text only' and it uses by default the .txt extension. Integrated Development Environment (IDE) compliers for most programming languages usually save source code as ASCII text les but they will use an extension that describes the content of the text le. Example: C++ usually uses .cpp as the extension.
The following web links provide more information and tables listing the ASCII Character Set: http://asciiset.com/
http://www.asciitable.com/
5 6
http://en.wikipedia.org/wiki/ASCII
302
APPENDIX
However, you
APPENDIX
303
Figure 26.1
Without the check present, it will show all le types. Click on the box to make the check appear [hide le
304
APPENDIX
Instructions for navigating to the Folder Options for various Windows operating systems along with an
extensions] or disappear [show le extensions] and then select OK. Internet link for additional help are provided below.
26.4.1 Windows XP
With the Windows Explorer open, slect the Tools tab and then Folder Options.
9 http://dotwhat.net/page/displayextensions/
Link
for
additional
help:
http://www.leinfo.net/help/windows-show-extensions.html
or
10
26.4.3 Windows 7
Select the Start button, then Control Panel and then Folder Options. Link for additional help: http://maximumpcguides.com/windows-7/hide-le-extensions/
11
APPENDIX
305
26.5.2 Collusion
Another category of academic dishonesty is collusion which is the unauthorized collaboration with another person in preparing written work (including lab assignments) oered for credit (counting towards your grade calculation). To better understand collusion, students need to realize that as part of the learning and evaluation of that learning, many professors use group projects; a directed or authorized collaboration. Often students are encouraged to form study groups to help discuss the course materials thus improving the learning process. These authorized and sometimes directed activities are not collusion. The following discussion is to help the student understand collusion (unauthorized collaboration) with specic reference to courses that use computers. This is not an all inclusive list, but will cover the common situations that faculty have encountered over the years. Unless your specic professor informs you dierently, you are to assume that the following items discussed are collusion.
Unless specically authorized by your professor, students should not complete computerized course work as a team or group and then share the nal completed product.
don't use an existing le and modify it to complete the assignment. Students have said that they worked as a team or group and that all participated and all learned the materials. Don't try this excuse because professors don't buy it. Here is the problem: Part of the learning process is in you doing it yourself. Example: I ask two students to make me some pancakes for breakfast; I expect two individually prepared plates of pancakes (one from each of them) for my breakfast. The professor really does not want to eat two plates of pancakes (or 50 to 100 plates of pancakes, depending on how many students they are teaching), but part of your directed learning activity for the course is to demonstrate that you can make pancakes (not watch someone else make pancakes or participate as a group to make pancakes).
306
APPENDIX
Unless specically authorized by your professor, don't share any les that you create or modify with another student ever, not now and not in the future.
turns it in as his work you will be included in the charge of academic dishonesty. Here are two suggestions for controlling access to your les: When using a course delivery software product or learning system, such as BlackBorad Vista, don't give another person your password. With the password, they will have access to your submitted assignments including the les that you created. Don't leave your les on a machine where others may have access to them. If multiple students are using or have access to the same machine (often happens with students living in the same household husband/wife, siblings or roommates) or in an on-campus course where many students will have access to the machine
store your les on a ash drive. Physically control who gets access to your ash drive.
The burden is for you to ask for any clarication for the specic
course from your professor. Don't assume that what another instructor allowed in another course will
be allowed by this professor in this course.
Additionally, the years of technical computer experience of professors who teach using computers in lab
Don't gamble that you can beat us at the Academic Dishonesty Game. Please don't take
this as a challenge and use it as an excuse to see if you can be academically dishonest and not get caught. We are warning you, not challenging you.
bond of trust is broken, many instructors will simply expel you from the
course you are taking. As an example: Within the BCIS1405 course at Houston Community College, we
APPENDIX
Spring 2008 term for Academic Dishonesty. Be ready for what ever the consequences your instructor will deliver if you are dishonest.
307
expelled 8 students (along with giving them the grade of F) from Distance Educations sections during the
26.5.8 Summary
The ethics of academic honesty; there is a bond of trust that whatever the student does in relationship to the evaluation process are their own work and eorts.
Seek clarication from your professor if you have any doubt that the collaborative activity might be considered collusion.
308
APPENDIX
faster pace (9 week summer term) you will need to 3 to 4 hour study period daily for 6 days a week (with
APPENDIX
26.6.4 Learning Requires Variety and Repetition
309
Variety comes in many forms and includes lecture, lab assignments, studying textbooks, multi-media materials, quizzes, writing a research papers, learning activities such as group discussions, crossword puzzles, ash cards, etc. This variety actually helps our brain to understand and build memory. In addition to variety, repetition (exposure over multiple study periods) is essential for our brains to be able to learn and recall the course materials. Again, this understanding and recall are essential to courses that require cumulative learning (you must understand item a before you can learn item b). Textbooks and professors break-up course materials into chapters or learning modules often with learning objectives rst and review items at the end of each unit. Each chapter or module might have any of the above mentioned items. But doing things and study are dierent. You can't just show up to class and listen, you can't just read stu, you need to study. Study requires a variety of activities. Ask yourself:
Do you understand each learning objective? Can you explain or formulate an answer for each learning objective? If you did not understand the reading materials, did you re-read it? Do the review items (especially questions). Take lecture notes. Do the lecture notes or handouts give you a better understanding than the textbook? Often the problems or lab assignments are to be studied in conjunction with and reinforce the study materials. Have you tried to do and understand the problems or lab assignments? Are there any learning activities available and if yes, did you do them. Did you consider using 3x5 cards to study denitions and vocabulary? Did you review the learning objectives before taking any quizzes? If the quizzes are computerized, did you study your quiz results? After reviewing quiz results and re-study, did you retake the quiz again if available?
All of this requires time and eort on your part as the student in any course (distance education or oncampus). You need several study periods a week to learn the materials in any course. The purpose of a quiz is for you to self assess your understanding of the materials. If your learning is not complete,
change or
regular study periods. The lack of regular study periods extra study periods in order to
310
APPENDIX
and share notes with each other if absent. In addition to course materials, other administrative matters are discussed in class (such as the announcement of exam date change). If you are taking a distance education course, you need to regularly enter the learning management system (such as Blackboard Vista) and review the announcements, discussion list postings and read (and answer if appropriate) email. Most distance education professors assume that anything he has communicated via these tools will have been read by the student within 3 days. In short this means you are responsible for having read the items and completing any action requested.
APPENDIX
311
15
16 17 19
14 This content is available online at <http://cnx.org/content/m19977/1.5/>. 15 See the le at <http://cnx.org/content/m19977/latest/Study_Habits_that_Build_the_Brain.pdf> 16 See the le at <http://cnx.org/content/m19977/latest/Reading_the_Textbook.pdf> 17 See the le at <http://cnx.org/content/m19977/latest/Taking_Lecture_Notes.pdf> 18 See the le at <http://cnx.org/content/m19977/latest/Using_3x5_Cards.pdf> 19 See the le at <http://cnx.org/content/m19977/latest/Using_the_Flash_Card_Activity.pdf>
312
GLOSSARY
Glossary
A
abs
A function within the cmath standard library in C++ which stands for absolute.
block
Another name for a compound statement.
Boolean
A data type representing the concepts of true and false.
action item
An attribute of iteration control structures.
braces
Used to identify a block of code in C++.
address operator
The ampersand or &.
applications
An information system or collection of programs that handles a major task.
branching
A control structure that allows the ow of execution to jump to a dierent part of the program.
array
A sequenced collection of elements of the same data type with a single identier name.
array function
A user dened specic task function designed to process an array.
break
A branching control structure that terminates the existing structure.
array member
An element or value in an array.
bubble sort
A method of swapping array members until they are in the desired sequence.
ASCII
American Standard Code for Information Interchange
case
A control structure that does mulitway selection.
assignment
An operator that changes the value of a modiable data object.
associativity
Determines the order in which the operators of the same precedence are allowed to manipulate the operands.
character
A data type representing single text characters like the alphabet, numeral digits, punctuation, etc.
at least once
Indicating that test after loops execute the action at least once.
circular nature
Connecting the negative and positive ends of the domain of an integer family data type.
banners
A set of comment lines used to help separate the functions and other sections of a program.
class member
An operator used to invoke functions associated with a class.
GLOSSARY
close
Your program requesting the operating system to release a le that was previously opened.
313
constant
A data item whose value cannot change during the program's execution.
continue
A branching control structure that causes a loop to stop its current iteration and begin the next one.
code checking
Using test data to check the coded program in a specic language (like C++).
control structures
Mechanisms that allow us to control the ow of execution within a program.
comments
Information inserted into a source code le for documentation of the program.
counting controlled
Using a variable to count up or down to control a loop.
common area
An area of the program where variables and constants are dened so that they are available to all functions.
data area
A part of an object code le used for storage of data.
compiler
Converts source code to object code.
data type
Denes a set of values and a set of operations that can be applied on those values.
compiler directive
An instruction to the compiler to complete a task before formally starting to compile the program.
complex logic
Often solved with nested control structures.
debugging
The process of removing errors from a program. 1) compiler 2) linker 3) logic
compound statement
A unit of code consisting of zero or more statements.
decision symbol
A diamond used in owcharting for asking a question and making a decision.
concatenation
Combining two strings into one string.
conditional
A trinary C++ operator that acts like an if then else control structure.
decrement
Subtracting one from the value of a variable.
conditional compilation
A compiler directive that includes or excludes lines of code based on a Boolean expression.
demotion
Going from a larger domain to a smaller domain.
dereferencing
The concept of using the item to which a pointer or address is pointing at.
condence
The reliance that Standard Library functions work properly.
device
A piece of equipment that is electronically connected to the memory so that data can be transferred between the memory and the device.
consistent
A rule that says to type identier names in upper and lower case consistently throughout your source code.
device token
314
GLOSSARY
A key value provided by the operating system to associate a device to your program.
factorial
A math problem that often is solved using recursion.
dimension
An axis of an array.
lename
The name and its extension.
do while
A test after iteration control structure available in C++.
lespec
The location of a le along with its lename.
documentation
A method of preserving information useful to others in understanding an information system or part thereof.
ag
A variable or constant used to store information that will normally be used to control the program.
domain
The set of allowed values for a data type.
exible coding
Using the sizeof operator to calculate the number of members in an array.
double
The most often used oating-point family data type used in C++.
oating point
A data type representing numbers with fractional parts.
dynamic memory
Aka stack created memory associated with local scope.
ow lines
Lines (sometimes with arrows) that connect the various owcharting symbols.
escape code
A code directing an output device to do something.
owcharting
A programming design tool that uses graphical elements to visually depict the ow of logic within a function.
evaluation
The process of applying the operators to the operands and resulting in a single value.
folder
A named area for storage of documents or other les on a disk drive or ash drive.
event controlled
Using user input to control a loop.
for
A test before iteration control structure typically used for counting.
exit
A pre-dened function used to prematurely stop a program and jump to the operating system.
formatting
Modifying the way the output is displayed.
explicit
Changing a value's data type with the cast operator.
function call
A function's using or invoking of another function.
expression
A valid sequence of operand(s) and operator(s) that reduces (or evaluates) to a single value.
function denition
The code that denes what a function does.
extraction
Aka reading or getting data from an input device.
function prototype
A function's communications declaration to a complier.
GLOSSARY
function
What modules are called in the two predominant programming languages of today: C++ and Java.
315
index
An operator that allows us to reference a member of an array.
indirection operator
The asterisk used for dereferencing a pointer.
global scope
Data storage dened outside of a function.
goto
A branching control structure that causes the logic to jump to a dierent place in the program.
innite loop
No method of exit, thus a bad thing.
initialize item
An attribute of iteration control structures.
header le
A le that contains items we want to have included toward the top of our source code.
input/output symbol
A parallelogram used in owcharting for input/output interactions.
hierarchy chart
Convey the relationship or big picture of the various functions in a program.
insertion
Aka writing or sending data to an output device.
integer
A data type representing whole numbers.
integer division
Division with no fractional parts.
iteration
A control structure that allows some lines of code to be executed many times.
if then else
A two way selection control structure.
life cycle
Systems Development Life Cycle: Planning - Analysis - Design Implementation - Maintenance
implementation
The phase of a Systems Development Life Cycle where the programmers would be assigned to write specic programs.
linear search
Using a loop to sequentially step through an array.
implicit
A value that has its data type changed automatically.
linker
Connects or links object les into an executable le.
include
A compiler directive to insert the contents of a le into the program.
list
A single dimension array.
increment
Adding one to the value of a variable.
loader
Part of the operating system that loads executable les into memory and direct the CPU to start running the program.
indention
A method used to make sections of source code more visible.
316
GLOSSARY
local scope
Data storage dened inside of a function. A system of arithmetic for integers where numbers "wrap around".
logical operator
An operator used to create complex Boolean expressions.
modularization
The ability to group some lines of code into a unit that can be included in our program.
loop attributes
Items associated with iteration or looping control structures.
modulus
The remainder part of integer division.
loop control
Making sure the attributes of a loop are properly handled.
multiway selection
Using control structures to be able to select from more than two choices.
Lvalue
The requirement that the operand on the left side of the assignment operator is modiable, usually a variable.
mutually exclusive
Items that do not overlap. Example: true and false.
machine dependent
An attribute of a programming language that changes depending on the computer's CPU.
object oriented
A programming approach that encapsulates data with functions.
mantissa exponent
The two integer parts of a oating-point value.
oset
The method of referencing array members by starting at zero.
maximum
Aka max or the largest member of an array.
open source
Group development of source code for software that is made available to the public at no cost.
meaningful
A rule that says identier names must be easily understood by another reading the source code.
open
Your program requesting the operating system to let it have access to an existing le or to open a new le.
operand
A value that receives the operator's action.
minimum
Aka min or the smallest member of an array.
operator
A language-specic syntactical token (usually a symbol) that causes an action to be taken on one or more operands.
minus
Aka unary negative.
model checking
Using test data to check the design model (usually done in pseudocode).
parallel processing
Dividing a task into parts that can utilize more than one processor.
modular arithmetic
parallel programming
GLOSSARY
Involves developing programs that utilize parallel processing algorithms that take advantage of multiple processors.
317
program control
Functions used to simply sub divide and control the program.
parameter passing
How the data is communicated in to and out of a function.
promotion
Going from a smaller domain to a larger domain.
parentheses
Change the order of evaluation in an expression. You do what's in the parentheses rst.
pseudo
Means false and includes the concepts of fake or imitation.
phrase word
Words used to make pseudocode logic clear to any reader.
pseudocode
English-like statements used to convey the steps of an algorithm or function.
plus
Aka unary positive.
read
Moving data from a device that has been opened into a memory location dened in your program.
pointer
A variable that holds an address as its value.
recursion
A repetitive process in which a function calls itself.
portability
The ability to transport software on a ash drive and thus use it on various machines.
reference variable
Used with parameter passing by reference.
postx
Placing the increment or decrement operator to the right of the operand.
relational operator
An operator that gives a Boolean value by evaluating the relationship between two operands.
pre-processor
The rst step the complier does in converting source code to object code.
precedence
Determines the order in which the operators are allowed to manipulate the operands.
repeat until
A test after iteration control structure that is not available in C++.
reserved word
Words that cannot be used by the programmer as identier names because they already have a specic meaning within the programming language.
precision
The eect on the domain of oating-point values given a larger or smaller storage area in bytes.
prex
Placing the increment or decrement operator to the left of the operand.
return
A branching control structure that causes a function to jump back to the function that called it.
procedural programming
Aka modular structured programming.
Rvalue
Pulls or fetches the value stored in a variable or constant.
process symbol
A rectangle used in owcharting for normal processes such as assignment.
scope
318
GLOSSARY
The area of a source code le where an identier name is recognized. Aka data area memory associated with global scope.
selection
A control structure where you select between two or more choices.
string
A series or array of characters as a single piece of data.
sequence
A control structure where you do the items in the sequence listed.
string class
A complex data item that uses object oriented programming.
sequence
An operator used to separate multiple occurrences of an item.
structure chart
Another name for a hierarchy chart.
structured programming
A method of planning programs that avoids the branching category of control structures.
sequential processing
Using only one processor and completing the tasks in a sequential order.
sum
Latin for summa or a total.
switch
A C++ control structure that can be made to act like a case control structure.
sizeof
An operator that tells you how many bytes a data type occupies in storage.
system analyst
Computer professional in charge of creating applications.
sorting
Arranging data according to their values.
source code
Any collection of statements or declarations written in some human-readable computer programming language.
table
A two dimension array.
test data
Providing input values and predicting the outputs.
specic task
Functions designed to be used with several programs.
test expression
An expression used to control programming structures.
stack
A part of the computer's memory used for storage of data.
test item
An attribute of iteration control structures.
standard input
The keyboard.
testing shell
A program used to create specic task functions.
Standard Library
A set of specic task functions that have been added to the programming language for universal use.
text editor
A software program for creating and editing ASCII text les.
standard output
The monitor.
text le
A le consisting of characters from the ASCII character code set.
static memory
truncation
GLOSSARY
The fractional part of a oating-point data type that is dropped when converted to an integer. A data item whose value can change during the program's execution.
319
versatile
Easily modifying code to handle another data type.
truth tables
A common way to show logical relationships.
vertical alignment
A method of listing items vertically so that they are easier to read quickly.
typedef
Allows the programmer to create an alias, or synonym, for an existing data type.
udst
User Dened Specic Task
unary negative
An operator that causes negation.
W warning
A compiler alert that there might be a problem.
unary positive
A worthless operator almost never used.
while
A test before iteration control structure available in C++.
update item
An attribute of iteration control structures.
wrapping
When output is not vertically spaced properly.
write
Moving data from a memory location dened in your program to a device that has been opened.
variable
320
INDEX
Ex.
Ex.
apples, 1
3 A
3x5 cards, 26.7(311) abs, 116 academic dishonesty, 26.5(305) action item, 182 address operator, 265, 267 ampersand, 265, 267 applications, 22 arithmetic assignment, 62 arithmetic operators, 3.6(51) array, 220, 233, 273 array data type, 18.1(233) array function, 244 array index operator, 18.2(234) array member, 235, 274 array members, 18.3(235) ASCII, 122, 219 ASCII Character Set, 26.3(301) assignment, 51, 185 associativity, 49, 26.1(297) asterisk, 267, 268 at least once, 182
C++ data types, 3.1(45) C++ program layout, 1.3(26) C:\Dev-Cpp\, 25, 25, 25 call, 90 capacity to concentrate, 26.6(308) case, 171 central processing unit, 286 character, 122 cin, 79 circular nature, 209 class member, 220 close, 243 closed communications model, 266, 268 code checking, 39 collusion, 26.5(305) comments, 95 common area, 91 compiler, 77 compiler directive, 81 compiler/IDE, 1.2(23) complex logic, 215 compound statement, 156 computational science, 24.2(280) concatenation, 220 conditional, 228 conditional compilation, 275 conditional expression, 17.1(227) conditional operator, 17.1(227) condence, 116 consistent, 96 constant, 49 continue, 171 control structures, 130, 11.1(151) counting controlled, 198 counting loops, 130 cout, 79
banners, 95 bits as ags, 13.2(182) black screen output box, 79 block, 156 Bloodshed Dev-C++ 5 compiler/IDE, 23, 31, 40, 54, 55, 63, 66, 68, 76, 82, 90, 97, 108, 109, 111, 116, 117, 122, 126, 148, 154, 157, 166, 175, 187, 199, 209, 210, 214, 216, 220, 223, 229, 230, 237, 238, 242, 244, 245, 246, 247, 251, 253, 255, 259, 261, 267, 269, 270, 273, 275, 276, 287 Boolean, 153 Boolean expression, 64, 151, 153, 154, 164, 179, 185, 186, 193 braces, 30, 96, 155 branching, 131 branching control structures, 171 break, 171 bubble sort, 254
data area, 104 data type, 46 data type conversion, 3.7(52) data type families, 46 debugging, 77
INDEX
decision symbol, 147 decrement, 193 dened constant, 48 denition, 90 demotion, 54 dereferencing, 269 devcpp-4.9.9.2_setup.exe, 25, 25 device, 79 device token, 243 dimension, 234, 274 distributed processing, 24.2(280) do while, 182 documentation, 95 domain, 46 double, 61 DriveLetter, 25 DriveLetter:\Dev-Cpp\, 25 dynamic memory, 245 dynamic memory allocation, 244 goto, 171
321
header le, 110 hierarchy chart, 89 high performance computing, 286 HIPO, 37
IDE, 1.2(23) identier name, 30 identier names, 3.2(47) if then else, 129, 152, 155 implementation, 23 implicit, 54 include, 81 increment, 193 indention, 96 index, 235, 274 indirection operator, 267, 269 innite loop, 182, 14.2(193), 15.1(203) initialize item, 198 Input-Output Stream, 79, 80 input/output symbol, 147 insertion, 80 integer, 46 integer data type, 4.1(59) integer division, 65 Integrated Development Environment, 71 IPO, 36, 37 iteration, 131 iteration as alt to recursion, 17.2(228)
equality, 185 escape code, 80 evaluation, 50 event controlled, 198 exit, 171 explicit, 54 expression, 49 extraction, 80
factorial, 229 File Extensions, 26.4(302) lename, 243 lespec, 243 xed, 15.3(209) ag, 184 ash cards, 26.7(311) exible coding, 237, 260 oat, 4.2(60) oating point, 46 oating-point data type, 4.2(60) oating-point division, 65 ow lines, 147 owcharting, 10.3(133), 147 folder, 24 for, 206 formatting, 210 function, 29 function call, 30 function denition, 30 function prototype, 30
K L
keywords, 26.2(300) lecture notes, 26.7(311) life cycle, 22 linear search, 252 linker, 77 list, 234, 274 literal constant, 48 loader, 77 local scope, 104, 244, 265, 266, 268 logical operator, 166 long double, 4.2(60) loop attributes, 198 loop control, 209 Lvalue, 64
machine dependent, 60, 122 mantissa exponent, 61 maximum, 252 meaningful, 96 memory constant, 49 might not happen, 198
322
INDEX
minimum, 252 minus, 16.2(221), 222 model checking, 39 modular arithmetic, 209 modular structured programming, 279 modularization, 29, 7.1(103) modulo, 15.2(207) modulus, 66 multidimensional arrays, 23.1(273) multiplication table, 15.4(211) multiway selection, 130, 164 mutually exclusive, 152 pseudocode for functions, 6.1(87)
read, 243 recursion, 229 reference variable, 267, 268 regular study times, 26.6(308) relational operator, 154 repeat until, 186 repetitive algorithms, 17.2(228) reserved word, 48 reserved words, 26.2(300) return, 171 Rvalue, 64
negation, 16.2(221) nested control structures, 164, 15.4(211) nested if then else, 170 no communication in with no communication out, 90
scholastic dishonesty, 26.5(305) scope, 105 selection, 131 sequence, 125, 130 sequential processing, 286 setw, 15.3(209) showpoint, 15.3(209) single quote marks, 122 sizeof, 123, 236 sorting, 254 source code, 24, 6.5(92), 241 specic task, 30, 104 specic task functions, 7.1(103) stack, 105 standard functions, 115 standard input, 80 Standard Library, 116 standard output, 80 static memory, 245 static memory allocation, 244 string, 62, 219, 233 string class, 220 structure chart, 89 structured programming, 131 sum, 246 supercomputing, 24.2(280) switch, 171 system analyst, 22 Systems Development Life Cycle, 21
object oriented, 280 oset, 235, 274 open, 243 open source, 23 operand, 49 operator, 49, 297
parallel processing, 286 parallel programming, 286 parameter passing, 30, 90, 104 parentheses, 50 phrase word, 88, 131 plus, 16.2(221), 222 pointer, 266, 267, 268 portability, 23 postx, 193 pre-processor, 77 precedence, 49 precedence chart, 26.1(297) precision, 61, 15.3(209) prex, 193 procedural programming, 280 process symbol, 147 procrastination, 26.6(308) program control, 30, 6.3(89), 104 program design, 2.1(35) program documentation, 6.5(92) promotion, 54 prototype, 90 prototypes le, 106 pseudo, 37 pseudocode, 36, 2.2(36) pseudocode for control structures, 10.2(131)
table, 234, 274 test after loop, 180 test after loops, 130 test before loop, 194, 204 test before loops, 130 test data, 36, 2.3(37) test expression, 64, 151, 153, 154, 164, 179, 184, 186, 193, 203
INDEX
test item, 182 testing shell, 110 text editor, 77 text le, 243 time commitment, 26.6(308) truncation, 54 truth tables, 166 two way selection, 11.1(151) two-way selection, 130 typedef, 124, 260 user dened library, 104 user dened specic task, 243, 245 user library, 7.3(105)
323
variable, 49 variable as a ag, 13.2(182) variety and repetition, 26.6(308) verify header program, 107 versatile, 260 vertical alignment, 95 vertical spacing, 209 void data type, 92
udst, 110 unary negative, 16.2(221), 222 unary positive, 16.2(221), 222 underscore, 241 update item, 182 user dened header le, 7.3(105)
W warning, 77
while, 129, 198 wrapping, 210 write, 243
324
Attributions
Collection: Edited by: Kenneth Leroy Busbee URL: http://cnx.org/content/col10621/1.20/ License: http://creativecommons.org/licenses/by/2.0/ Module: "Preface - Fundamentals I using C++" Used here as: "Preface" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19049/1.16/ Pages: 1-3 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Author Acknowledgements - Fundamentals I using C++" Used here as: "Author Acknowledgements" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19055/1.5/ Page: 5 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Orientation and Syllabus - Fundamentals I using C++" Used here as: "Orientation and Syllabus" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19684/1.11/ Pages: 7-9 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Why You should Create a Personal Connexions Account" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m33937/1.1/ Pages: 11-13 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Creating a Connexions Account" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m33888/1.1/ Pages: 15-18 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Sharing/Rating Connexions Materials" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m33960/1.2/ Pages: 19-20 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
ATTRIBUTIONS
Module: "Systems Development Life Cycle" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18684/1.6/ Pages: 21-23 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Bloodshed Dev-C++ 5 Compiler/IDE" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18785/1.15/ Pages: 23-26 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Modularization and C++ Program Layout" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18861/1.13/ Pages: 26-30 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 1: Introduction to Programming" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22450/1.4/ Pages: 31-33 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Program Design" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18686/1.5/ Pages: 35-36 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Pseudocode" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18648/1.10/ Pages: 36-37 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Test Data" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18781/1.4/ Pages: 37-39 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/
325
326 Module: "Practice 2: Program Planning & Design" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22453/1.3/ Pages: 40-42 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Data Types in C++" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18653/1.13/ Pages: 45-46 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Identier Names" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18692/1.5/ Pages: 47-48 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Constants and Variables" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18719/1.8/ Pages: 48-49 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Data Manipulation" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18705/1.6/ Pages: 49-50 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Assignment Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18725/1.4/ Pages: 50-51 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Arithmetic Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18706/1.4/ Pages: 51-52 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Data Type Conversions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18744/1.7/ Pages: 52-54 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Practice 3: Data & Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22455/1.4/ Pages: 55-57 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Integer Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18654/1.6/ Pages: 59-60 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Floating-Point Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18657/1.6/ Pages: 60-61 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "String Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18656/1.7/ Pages: 61-62 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Arithmetic Assignment Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18743/1.7/ Pages: 62-63 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Lvalue and Rvalue" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18058/1.12/ Pages: 63-64 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Integer Division and Modulus" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18717/1.13/ Pages: 65-67 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 4: Often Used Data Types" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22456/1.2/ Pages: 68-69 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
327
328 Module: "Integrated Development Environment" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18920/1.9/ Pages: 71-77 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Standard Input and Output" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18835/1.5/ Pages: 77-80 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Compiler Directives" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18787/1.7/ Pages: 80-81 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 5: Integrated Development Environment" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22457/1.3/ Pages: 82-84 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Pseudocode Examples for Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19136/1.7/ Pages: 87-88 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Hierarchy or Structure Chart" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18682/1.4/ Pages: 88-89 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Program Control Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19145/1.9/ Pages: 89-91 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Void Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18655/1.4/ Pages: 91-92 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Documentation and Making Source Code Readable" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18912/1.5/ Pages: 92-96 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 6: Program Control Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22458/1.4/ Pages: 97-101 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Specic Task Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19146/1.4/ Pages: 103-104 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Global vs Local Data Storage" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19147/1.5/ Pages: 104-105 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Using a Header File for User Dened Specic Task Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19346/1.7/ Pages: 105-110 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 7: Specic Task Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22459/1.2/ Pages: 111-112 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Standard Libraries" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19202/1.6/ Pages: 115-116 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 8: Standard Libraries" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22460/1.3/ Pages: 117-118 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
329
330 Module: "Character Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18669/1.6/ Pages: 121-122 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Sizeof Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18736/1.8/ Pages: 122-123 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Typedef - An Alias" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18741/1.5/ Pages: 123-124 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Sequence Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18690/1.8/ Pages: 124-125 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Practice 9: Character Data, Sizeof, Typedef, Sequence" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22461/1.4/ Pages: 126-127 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Structured Programming" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19628/1.7/ Pages: 129-131 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Pseudocode Examples for Control Structures" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18649/1.13/ Pages: 131-132 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Flowcharting" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19623/1.6/ Pages: 133-147 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Practice 10: Introduction to Structured Programming" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19692/1.14/ Pages: 148-149 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "If Then Else" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19679/1.4/ Pages: 151-152 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Boolean Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19544/1.5/ Page: 153 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Relational Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19549/1.11/ Pages: 154-155 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Compound Statement" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19683/1.3/ Pages: 155-156 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 11: Two Way Selection" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19764/1.15/ Pages: 157-158 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Nested If Then Else" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19951/1.6/ Pages: 161-164 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Logical Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19847/1.8/ Pages: 164-166 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
331
332 Module: "Case Control Structure" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19963/1.4/ Pages: 167-171 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Branching Control Structures" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19947/1.3/ Pages: 171-174 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 12: Multiway Selection" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19968/1.12/ Pages: 175-177 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Do While Loop" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20596/1.5/ Pages: 179-182 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Flag Concept" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20404/1.6/ Pages: 182-184 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Assignment vs Equality within C++" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19542/1.7/ Pages: 184-185 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Repeat Until Loop" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20597/1.3/ Pages: 185-186 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 13: Test After Loops" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20642/1.8/ Pages: 187-188 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Increment and Decrement Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20499/1.4/ Pages: 191-193 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "While Loop" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20598/1.8/ Pages: 193-198 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 14: Test Before Loops" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20643/1.10/ Pages: 199-200 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "For Loop" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20600/1.4/ Pages: 203-206 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Circular Nature of the Integer Data Type Family" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20743/1.4/ Pages: 207-209 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Formatting Output" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20660/1.4/ Pages: 209-210 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Nested For Loops" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20653/1.6/ Pages: 211-215 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 15: Counting Loops" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20809/1.6/ Pages: 216-217 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
333
334 Module: "String Class within C++" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20796/1.6/ Pages: 219-220 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Unary Positive and Negative Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20501/1.2/ Pages: 221-222 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 16: String Class, Unary Positive and Negative" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20810/1.8/ Pages: 223-224 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Conditional Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20811/1.7/ Pages: 227-228 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Recursion vs Iteration" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20814/1.4/ Pages: 228-229 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 17: Conditional Operator and Recursion" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m20815/1.6/ Pages: 230-231 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Array Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21315/1.3/ Pages: 233-234 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Array Index Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21316/1.3/ Pages: 234-235 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Displaying Array Members" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21317/1.5/ Pages: 235-237 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 18: Introduction to Arrays" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21321/1.5/ Pages: 238-239 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "File Input and Output" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21623/1.3/ Pages: 241-243 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Arrays and Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21624/1.2/ Pages: 243-244 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Loading an Array from a File" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21625/1.2/ Pages: 244-245 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Math Statistics with Arrays" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21626/1.2/ Pages: 245-246 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 19: File I/O and Array Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21630/1.4/ Pages: 247-248 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
335
336 Module: "Finding a Specic Member of an Array" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21627/1.2/ Pages: 251-252 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Sorting an Array" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21628/1.2/ Pages: 252-254 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 20: More Array Functions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21631/1.4/ Pages: 255-256 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Versatile Code with Typedef" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21629/1.2/ Pages: 259-260 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 21: More on Typedef" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21632/1.6/ Pages: 261-262 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Address Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22148/1.3/ Pages: 265-266 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Parameter Passing by Reference" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22150/1.4/ Pages: 266-267 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Pointer Data Type" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22151/1.3/ Page: 267 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Indirection Operator" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22152/1.2/ Pages: 268-269 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 22: Pointers" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22153/1.4/ Pages: 270-271 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Multidimensional Arrays" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22203/1.3/ Pages: 273-274 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Conditional Compilation" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22204/1.5/ Pages: 274-275 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 23: More Arrays & Compiler Directives" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22205/1.3/ Pages: 276-277 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Object Oriented Programming" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22188/1.3/ Pages: 279-280 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Understanding High Performance Computing" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19804/1.8/ Pages: 280-286 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Practice 24: OOP & HPC" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22194/1.6/ Pages: 287-288 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
337
338 Module: "Review: Foundation Topics Group: 1-5" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22418/1.5/ Page: 292 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Review: Modular Programming Group: 6-9" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22421/1.3/ Page: 293 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Review: Structured Programming Group: 10-16" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21653/1.6/ Page: 294 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Review: Intermediate Topics Group: 17-21" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m21642/1.5/ Page: 295 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Review: Advanced Topics Group: 22-24" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m22215/1.5/ Page: 296 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/ Module: "Abbreviated Precedence Chart for C++ Operators" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18168/1.13/ Pages: 297-299 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "C++ Reserved Keywords" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18181/1.6/ Page: 300 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "ASCII Character Set" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18169/1.8/ Page: 301 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/
ATTRIBUTIONS
ATTRIBUTIONS
Module: "Show Hide File Extensions" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18100/1.6/ Pages: 302-304 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Academic or Scholastic Dishonesty" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18096/1.11/ Pages: 305-307 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Successful Learning Skills" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m18101/1.10/ Pages: 308-310 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/2.0/ Module: "Study Habits that Build the Brain" By: Kenneth Leroy Busbee URL: http://cnx.org/content/m19977/1.5/ Page: 311 Copyright: Kenneth Leroy Busbee License: http://creativecommons.org/licenses/by/3.0/
339
About Connexions
Since 1999, Connexions has been pioneering a global system where anyone can create course materials and make them fully accessible and easily reusable free of charge. We are a Web-based authoring, teaching and learning environment open to anyone interested in education, including students, teachers, professors and lifelong learners. We connect ideas and facilitate educational communities. Connexions's modular, interactive courses are in use worldwide by universities, community colleges, K-12 schools, distance learners, and lifelong learners. Connexions materials are in many languages, including English, Spanish, Chinese, Japanese, Italian, Vietnamese, French, Portuguese, and Thai. Connexions is part of an exciting new information distribution system that allows for
has partnered with innovative on-demand publisher QOOP to accelerate the delivery of printed course materials and textbooks into classrooms worldwide at lower prices than traditional academic publishers.