ebook img

Programming Fundamentals: A Modular Structured Approach Using C++ PDF

342 Pages·2009·2.381 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Fundamentals: A Modular Structured Approach Using C++

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee Online: < http://cnx.org/content/col10621/1.22/ > C O N N E X I O N S RiceUniversity,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 3.0 license (http://creativecommons.org/licenses/by/3.0/). Collection structure revised: January 10, 2013 PDF generated: January 10, 2013 For copyright and attribution information for the modules contained in this collection, see p. 318. Table of Contents Preface ...............................................................................................1 Author Acknowledgements .........................................................................5 Orientation and Syllabus ...........................................................................7 Sharing/Rating Connexions Materials ...........................................................11 1 1. Introduction to Programming 1.1 Systems Development Life Cycle ............................................................13 1.2 Bloodshed Dev-C++ 5 Compiler/IDE ......................................................15 1.3 Modularization and C++ Program Layout .................................................18 1.4 Practice 1: Introduction to Programming ...................................................23 Solutions ........................................................................................26 2 2. Program Planning & Design 2.1 Program Design ............................................................................27 2.2 Pseudocode ................................................................................28 2.3 Test Data ..................................................................................29 2.4 Practice 2: Program Planning & Design ....................................................32 Solutions ........................................................................................35 3 3. Data & Operators 3.1 Data Types in C++ ........................................................................37 3.2 Identi(cid:28)er Names ............................................................................39 3.3 Constants and Variables ....................................................................40 3.4 Data Manipulation .........................................................................41 3.5 Assignment Operator .......................................................................42 3.6 Arithmetic Operators .......................................................................43 3.7 Data Type Conversions .....................................................................44 3.8 Practice 3: Data & Operators ..............................................................47 Solutions ........................................................................................50 4 4. Often Used Data Types 4.1 Integer Data Type ..........................................................................51 4.2 Floating-Point Data Type ..................................................................52 4.3 String Data Type ..........................................................................53 4.4 Arithmetic Assignment Operators ..........................................................54 4.5 Lvalue and Rvalue ..........................................................................55 4.6 Integer Division and Modulus ..............................................................57 4.7 Practice 4: Often Used Data Types .........................................................60 Solutions ........................................................................................62 5 5. Integrated Development Environment 5.1 Integrated Development Environment .......................................................63 5.2 Standard Input and Output ................................................................69 5.3 Compiler Directives ........................................................................72 5.4 Practice 5: Integrated Development Environment ...........................................74 Solutions ........................................................................................77 6 6. Program Control Functions 6.1 Pseudocode Examples for Functions ........................................................79 6.2 Hierarchy or Structure Chart ...............................................................80 6.3 Program Control Functions .................................................................81 iv 6.4 Void Data Type ............................................................................83 6.5 Documentation and Making Source Code Readable .........................................84 6.6 Practice 6: Program Control Functions .....................................................89 Solutions ........................................................................................94 7 7. Speci(cid:28)c Task Functions 7.1 Speci(cid:28)c Task Functions .....................................................................95 7.2 Global vs Local Data Storage ...............................................................96 7.3 Using a Header File for User De(cid:28)ned Speci(cid:28)c Task Functions ...............................97 7.4 Practice 7: Speci(cid:28)c Task Functions ........................................................103 Solutions .......................................................................................105 8 8. Standard Libraries 8.1 Standard Libraries ........................................................................107 8.2 Practice 8: Standard Libraries .............................................................109 Solutions .......................................................................................111 9 9. Character Data, Sizeof, Typedef, Sequence 9.1 Character Data Type ......................................................................113 9.2 Sizeof Operator ...........................................................................114 9.3 Typedef - An Alias ........................................................................115 9.4 Sequence Operator ........................................................................116 9.5 Practice 9: Character Data, Sizeof, Typedef, Sequence .....................................118 Solutions .......................................................................................121 10 10. Introduction to Structured Programming 10.1 Structured Programming .................................................................123 10.2 Pseudocode Examples for Control Structures .............................................125 10.3 Flowcharting .............................................................................127 10.4 Practice 10: Introduction to Structured Programming ....................................142 Solutions .......................................................................................144 11 11. Two Way Selection 11.1 If Then Else .............................................................................145 11.2 Boolean Data Type ......................................................................147 11.3 Relational Operators .....................................................................148 11.4 Compound Statement ....................................................................149 11.5 Practice 11: Two Way Selection ..........................................................151 Solutions .......................................................................................153 12 12. Multiway Selection 12.1 Nested If Then Else ......................................................................155 12.2 Logical Operators ........................................................................158 12.3 Case Control Structure ...................................................................161 12.4 Branching Control Structures ............................................................165 12.5 Practice 12: Multiway Selection ..........................................................169 Solutions .......................................................................................172 13 13. Test After Loops 13.1 Do While Loop ..........................................................................173 13.2 Flag Concept ............................................................................176 13.3 Assignment vs Equality within C++ .....................................................178 13.4 Repeat Until Loop .......................................................................179 13.5 Practice 13: Test After Loops ............................................................181 Solutions .......................................................................................183 AvailableforfreeatConnexions<http://cnx.org/content/col10621/1.22> v 14 14. Test Before Loops 14.1 Increment and Decrement Operators .....................................................185 14.2 While Loop ..............................................................................187 14.3 Practice 14: Test Before Loops ...........................................................193 Solutions .......................................................................................195 15 15. Counting Loops 15.1 For Loop .................................................................................197 15.2 Circular Nature of the Integer Data Type Family .........................................201 15.3 Formatting Output .......................................................................203 15.4 Nested For Loops ........................................................................205 15.5 Practice 15: Counting Loops .............................................................210 Solutions .......................................................................................212 16 16. String Class, Unary Positive and Negative 16.1 String Class within C++ .................................................................213 16.2 Unary Positive and Negative Operators ..................................................215 16.3 Practice 16: String Class, Unary Positive and Negative ...................................217 Solutions .......................................................................................219 17 17. Conditional Operator and Recursion 17.1 Conditional Operator ....................................................................221 17.2 Recursion vs Iteration ....................................................................222 17.3 Practice 17: Conditional Operator and Recursion .........................................224 Solutions .......................................................................................226 18 18. Introduction to Arrays 18.1 Array Data Type ........................................................................227 18.2 Array Index Operator ....................................................................228 18.3 Displaying Array Members ...............................................................229 18.4 Practice 18: Introduction to Arrays ......................................................232 Solutions .......................................................................................234 19 19. File I/O and Array Functions 19.1 File Input and Output ...................................................................235 19.2 Arrays and Functions ....................................................................237 19.3 Loading an Array from a File ............................................................238 19.4 Math Statistics with Arrays ..............................................................239 19.5 Practice 19: File I/O and Array Functions ...............................................241 Solutions .......................................................................................243 20 20. More Array Functions 20.1 Finding a Speci(cid:28)c Member of an Array ...................................................245 20.2 Sorting an Array .........................................................................246 20.3 Practice 20: More Array Functions .......................................................249 Solutions .......................................................................................251 21 21. More on Typedef 21.1 Versatile Code with Typedef .............................................................253 21.2 Practice 21: More on Typedef ............................................................255 Solutions .......................................................................................257 22 22. Pointers 22.1 Address Operator ........................................................................259 22.2 Parameter Passing by Reference ..........................................................260 AvailableforfreeatConnexions<http://cnx.org/content/col10621/1.22> vi 22.3 Pointer Data Type .......................................................................261 22.4 Indirection Operator .....................................................................262 22.5 Practice 22: Pointers .....................................................................264 Solutions .......................................................................................266 23 23. More Arrays & Compiler Directives 23.1 Multidimensional Arrays .................................................................267 23.2 Conditional Compilation .................................................................268 23.3 Practice 23: More Arrays & Compiler Directives .........................................270 Solutions .......................................................................................272 24 24. OOP & HPC 24.1 Object Oriented Programming ...........................................................273 24.2 Understanding High Performance Computing .............................................274 24.3 Practice 24: OOP & HPC ................................................................281 Solutions .......................................................................................283 25 Review Materials 25.1 Review: Foundation Topics Group: 1-5 ...................................................286 25.2 Review: Modular Programming Group: 6-9 ..............................................287 25.3 Review: Structured Programming Group: 10-16 ..........................................288 25.4 Review: Intermediate Topics Group: 17-21 ...............................................289 25.5 Review: Advanced Topics Group: 22-24 ..................................................290 26 Appendix 26.1 Abbreviated Precedence Chart for C++ Operators .......................................291 26.2 C++ Reserved Keywords ................................................................294 26.3 ASCII Character Set .....................................................................295 26.4 Show Hide File Extensions ...............................................................296 26.5 Academic or Scholastic Dishonesty .......................................................299 26.6 Successful Learning Skills ................................................................302 26.7 Study Habits that Build the Brain .......................................................305 Glossary ............................................................................................306 Index ...............................................................................................314 Attributions ........................................................................................318 AvailableforfreeatConnexions<http://cnx.org/content/col10621/1.22> Preface1 About this Textbook/Collection Programming Fundamentals (cid:21) A Modular Structured Approach using C++ Programming Fundamentals - A Modular Structured Approach using C++ is written by Kenneth Leroy Busbee, a faculty member at Houston Community College in Houston, Texas. The materials used in this textbook/collectionweredevelopedbytheauthorandothersasindependentmodulesforpublicationwithin theConnexionsenvironment. Programmingfundamentalsareoftendividedintothreecollegecourses: Mod- ular/Structured, Object Oriented and Data Structures. This textbook/collection covers the (cid:28)rst of those three courses. On January 10, 2013 Version 1.22 was created with the modules that make up the collection "(cid:28)xed" to the their current versions. This will allow Version 1.22 to remain static with the modules as of that date. Thecollectionisgoingtoberevisedwithadi(cid:27)erentorganizationofchaptersalongwithupdatedmodules tohandleC++,JavaandC#programminglanguages. Thenextversionofthecollectionwillhavesigni(cid:28)cant changes. Connexions Learning Modules The learning modules of this textbook/collection were written as standalone modules. Students using a collection of modules as a textbook will usually view it contents by reading the modules sequentially as presented by the author of the collection. However, the majority of readers of these modules will (cid:28)nd them as a result of an Internet search. The Connexions Project allows the author of a module to create web links to other Connexions modules and Internet locations. These links are shown when viewing materials on-line and are categorized into three types: Example, Prerequisite and Supplemental. The importance of each link is numbered from 1 to 3 by the author. When viewing the module each links shows a three part box with yellow or white rectangles. All three yellow means it is a strongly related link. As the yellow decreases the importance decreases. Students using this collection for a college course should note that all of the Prerequisite links within the modules will be modules that student should have already read and most of the Supplemental links 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 (cid:28)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 speci(cid:28)c 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 speci(cid:28)cally for the C++ programming language. This could not be 1Thiscontentisavailableonlineat<http://cnx.org/content/m19049/1.17/>. AvailableforfreeatConnexions<http://cnx.org/content/col10621/1.22> 1 2 avoided as the C++ language is used in conjunction with this textbook/collection by the author in teaching college courses. Bloodshed Dev-C++ 5 Compiler/IDE This open source compiler/IDE (Integrated Development Environment) was used to develop the demon- stration source code (cid:28)les provided within the modules of this textbook/collection. The compiler/IDE is presented to the student in the second module of Chapter 1, with instructions for downloading, installing andusingthecompiler/IDE.AmorecompleteexplanationoftheIDEalongwithdemonstrationsourcecode listings with errors is presented in (cid:28)rst module of Chapter 5. All of the source code (cid:28)les provided in this textbook/collectioncontainonlyANSIstandardC++codeandshouldworkonanystandardC++compiler likeMicrosoftVisualStudio(whichincludesC++),MicrosoftVisualC++ExpressorBorlandC++Builder. Instructor Materials Encryptedinstructormaterialsareavailableinamodulethatisnotpartofthiscollection. It’stitle: Instruc- tor Materials for: Programming Fundamentals - A Modular Structured Approach using C++ is available 2 at: http://cnx.org/content/m34529/latest/ and the encryption code is only available to educational insti- tutional faculty that are considering adoption of this collection as a textbook. About Connexions Connexions Modular Content 3 The Connexions Project http://cnx.org is part of the Open Educational Resources (OER) movement 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 collection is composed of a number of re-usable learning modules 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. Re-use and Customization 4 The Creative Commons (CC) Attribution license applies to all Connexions modules. Under this license, any Connexions module may be used or modi(cid:28)ed 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. There- fore, 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 (cid:28)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 (cid:29)" link, which takes you to the (cid:28)rst module in the collection. You can then navigate to the next module using "NEXT (cid:29)" and 2"InstructorMaterialsfor: ProgrammingFundamentals-AModularStructuredApproachusingC++" <http://cnx.org/content/m34529/latest/> 3http://cnx.org/ 4http://creativecommons.org/licenses/by/2.0/ AvailableforfreeatConnexions<http://cnx.org/content/col10621/1.22>

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.