ebook img

Introduction to Computational Modeling Using C and Open-Source Tools PDF

463 Pages·2013·8.065 MB·English
by  GarridoJ.M.
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 Introduction to Computational Modeling Using C and Open-Source Tools

INTRODUCTION TO COMPUTATIONAL MODELING USING C AND OPEN-SOURCE TOOLS José M. Garrido Kennesaw State University Kennesaw Georgia, USA K22074_FM.indd 1 10/4/13 11:37 AM Chapman & Hall/CRC Computational Science Series SERIES EDITOR Horst Simon Deputy Director Lawrence Berkeley National Laboratory Berkeley, California, U.S.A. PUBLISHED TITLES COMBINATORIAL SCIENTIFIC COMPUTING Edited by Uwe Naumann and Olaf Schenk CONTEMPORARY HIGH PERFORMANCE COMPUTING: FROM PETASCALE TOWARD EXASCALE Edited by Jeffrey S. Vetter DATA-INTENSIVE SCIENCE Edited by Terence Critchlow and Kerstin Kleese van Dam PETASCALE COMPUTING: ALGORITHMS AND APPLICATIONS Edited by David A. Bader FUNDAMENTALS OF MULTICORE SOFTWARE DEVELOPMENT Edited by Victor Pankratius, Ali-Reza Adl-Tabatabai, and Walter Tichy GRID COMPUTING: TECHNIQUES AND APPLICATIONS Barry Wilkinson HIGH PERFORMANCE COMPUTING: PROGRAMMING AND APPLICATIONS John Levesque with Gene Wagenbreth HIGH PERFORMANCE VISUALIZATION: ENABLING EXTREME-SCALE SCIENTIFIC INSIGHT Edited by E. Wes Bethel, Hank Childs, and Charles Hansen INTRODUCTION TO COMPUTATIONAL MODELING USING C AND OPEN-SOURCE TOOLS José M Garrido INTRODUCTION TO CONCURRENCY IN PROGRAMMING LANGUAGES Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen INTRODUCTION TO ELEMENTARY COMPUTATIONAL MODELING: ESSENTIAL CONCEPTS, PRINCIPLES, AND PROBLEM SOLVING José M. Garrido INTRODUCTION TO HIGH PERFORMANCE COMPUTING FOR SCIENTISTS AND ENGINEERS Georg Hager and Gerhard Wellein K22074_FM.indd 2 10/4/13 11:37 AM PUBLISHED TITLES CONTINUED INTRODUCTION TO REVERSIBLE COMPUTING Kalyan S. Perumalla INTRODUCTION TO SCHEDULING Yves Robert and Frédéric Vivien INTRODUCTION TO THE SIMULATION OF DYNAMICS USING SIMULINK® Michael A. Gray PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES Yu-Kwong Ricky Kwok PERFORMANCE TUNING OF SCIENTIFIC APPLICATIONS Edited by David Bailey, Robert Lucas, and Samuel Williams PROCESS ALGEBRA FOR PARALLEL AND DISTRIBUTED PROCESSING Edited by Michael Alexander and William Gardner SCIENTIFIC DATA MANAGEMENT: CHALLENGES, TECHNOLOGY, AND DEPLOYMENT Edited by Arie Shoshani and Doron Rotem K22074_FM.indd 3 10/4/13 11:37 AM MATLAB® and Simulink® are trademarks of The MathWorks, Inc. and are used with permission. The MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or discussion of MATLAB® and Simulink® software or related products does not constitute endorsement or sponsorship by The MathWorks of a particular pedagogical approach or particular use of the MATLAB® and Simulink® software. Original art “Aztec Calendar” by Victor H. Verde. Photograph and graphic design by Tino Garrido-Licha. CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2014 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Version Date: 20131028 International Standard Book Number-13: 978-1-4822-1679-0 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, includ- ing photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents ListofFigures xvii ListofTables xxi Preface xxiii AbouttheAuthor xxv Chapter1 ProblemSolvingandComputing 1 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 ComputerProblemSolving . . . . . . . . . . . . . . . . . . . . . 1 1.3 ElementaryConcepts . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 DevelopingComputationalModels . . . . . . . . . . . . . . . . . 4 1.5 ASimpleProblem:TemperatureConversion . . . . . . . . . . . . 6 1.5.1 InitialProblemStatement . . . . . . . . . . . . . . . . . . 6 1.5.2 AnalysisandConceptualModel . . . . . . . . . . . . . . . 7 1.5.3 MathematicalModel . . . . . . . . . . . . . . . . . . . . . 7 1.6 CategoriesofComputationalModels . . . . . . . . . . . . . . . . 8 1.7 ComputingtheAreaandCircumferenceofaCircle . . . . . . . . . 10 1.8 GeneralProcessofSoftwareDevelopment . . . . . . . . . . . . . 11 1.9 ProgrammingLanguages . . . . . . . . . . . . . . . . . . . . . . . 12 1.9.1 High-LevelProgrammingLanguages . . . . . . . . . . . . 12 1.9.2 Interpreters . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.9.3 Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.9.4 CompilingandExecutionofJavaPrograms . . . . . . . . . 13 1.9.5 CompilingandExecutingCPrograms . . . . . . . . . . . . 14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Chapter2 Programs 17 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2 Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.3 DataDefinitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.3.1 NameofDataItems . . . . . . . . . . . . . . . . . . . . . 18 2.3.2 DataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3.3 DataDeclarationsinC . . . . . . . . . . . . . . . . . . . . 19 v vi 2.4 StructureofaCProgram . . . . . . . . . . . . . . . . . . . . . . . 20 2.5 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.6 SimpleFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.6.1 FunctionDefinitions . . . . . . . . . . . . . . . . . . . . . 22 2.6.2 FunctionCalls . . . . . . . . . . . . . . . . . . . . . . . . 23 2.7 ASimpleCProgram . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.8 ASimpleProblem:TemperatureConversion . . . . . . . . . . . . 25 2.8.1 MathematicalModel . . . . . . . . . . . . . . . . . . . . . 25 2.8.2 ComputationalModel . . . . . . . . . . . . . . . . . . . . 26 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Chapter3 ModularDecomposition:Functions 29 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.2 ModularDecomposition . . . . . . . . . . . . . . . . . . . . . . . 29 3.3 DefiningFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.4 CallingFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.5 ClassificationofFunctions . . . . . . . . . . . . . . . . . . . . . . 31 3.5.1 SimpleFunctionCalls . . . . . . . . . . . . . . . . . . . . 31 3.5.2 CallingFunctionsthatReturnData . . . . . . . . . . . . . 31 3.5.3 CallingFunctionswithArguments . . . . . . . . . . . . . . 33 3.6 NumericalTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.6.1 IntegerNumbers . . . . . . . . . . . . . . . . . . . . . . . 35 3.6.2 NumberswithDecimalPoints . . . . . . . . . . . . . . . . 36 3.7 Built-inMathematicalFunctions . . . . . . . . . . . . . . . . . . . 37 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Chapter4 MoreConceptsofComputationalModels 41 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.2 IntroductiontoErrorsinComputing . . . . . . . . . . . . . . . . . 41 4.2.1 NumberRepresentation . . . . . . . . . . . . . . . . . . . 41 4.2.2 NumberofSignificantDigits . . . . . . . . . . . . . . . . . 42 4.2.3 PrecisionandAccuracy. . . . . . . . . . . . . . . . . . . . 42 4.2.4 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.3 AverageandInstantaneousRateofChange . . . . . . . . . . . . . 44 4.4 TheFree-fallingObject . . . . . . . . . . . . . . . . . . . . . . . . 47 4.4.1 InitialProblemStatement . . . . . . . . . . . . . . . . . . 47 4.4.2 AnalysisandConceptualModel . . . . . . . . . . . . . . . 48 4.4.2.1 Assumptions . . . . . . . . . . . . . . . . . . . . 48 4.4.2.2 BasicDefinitions . . . . . . . . . . . . . . . . . 48 4.4.3 MathematicalModel . . . . . . . . . . . . . . . . . . . . . 49 4.4.4 ComputationalModel . . . . . . . . . . . . . . . . . . . . 49 4.5 Simulation:BasicConcepts . . . . . . . . . . . . . . . . . . . . . 51 4.5.1 SimulationModels . . . . . . . . . . . . . . . . . . . . . . 51 vii 4.5.2 VariabilitywithTime . . . . . . . . . . . . . . . . . . . . . 53 4.5.3 SimulationResults . . . . . . . . . . . . . . . . . . . . . . 53 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Chapter5 AlgorithmsandPrograms 57 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5.2 ProblemSolving . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5.3 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.4 AlgorithmDescription . . . . . . . . . . . . . . . . . . . . . . . . 58 5.5 DesignStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.5.1 Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.5.2 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.5.3 Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.6 ImplementingAlgorithms . . . . . . . . . . . . . . . . . . . . . . 62 5.6.1 AssignmentandArithmeticExpressions . . . . . . . . . . . 63 5.6.2 SimpleNumericComputations . . . . . . . . . . . . . . . . 63 5.6.3 SimpleInput/Output . . . . . . . . . . . . . . . . . . . . . 65 5.7 ComputingAreaandCircumference . . . . . . . . . . . . . . . . . 67 5.7.1 Specification . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.7.2 AlgorithmwiththeMathematicalModel . . . . . . . . . . 67 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Chapter6 Selection 71 6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.2 SelectionStructure . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.2.1 SelectionStructurewithFlowcharts . . . . . . . . . . . . . 71 6.2.2 SelectionwithPseudo-Code . . . . . . . . . . . . . . . . . 72 6.2.3 ImplementingSelectionwiththeCLanguage . . . . . . . . 73 6.2.4 ConditionalExpressions . . . . . . . . . . . . . . . . . . . 73 6.2.5 ExamplewithSelection . . . . . . . . . . . . . . . . . . . 74 6.3 AComputationalModelwithSelection . . . . . . . . . . . . . . . 75 6.3.1 AnalysisandMathematicalModel . . . . . . . . . . . . . . 75 6.3.2 AlgorithmforGeneralSolution . . . . . . . . . . . . . . . 76 6.3.3 DetailedAlgorithm . . . . . . . . . . . . . . . . . . . . . . 76 6.4 Multi-LevelSelection . . . . . . . . . . . . . . . . . . . . . . . . 79 6.4.1 GeneralMulti-PathSelection. . . . . . . . . . . . . . . . . 79 6.4.2 CaseStructure . . . . . . . . . . . . . . . . . . . . . . . . 80 6.5 ComplexConditions . . . . . . . . . . . . . . . . . . . . . . . . . 82 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 viii Chapter7 Repetition 85 7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 7.2 RepetitionwiththeWhileLoop . . . . . . . . . . . . . . . . . . . 85 7.2.1 While-LoopFlowchart . . . . . . . . . . . . . . . . . . . . 85 7.2.2 WhileStructureinPseudo-Code . . . . . . . . . . . . . . . 86 7.2.3 WhileLoopintheCLanguage . . . . . . . . . . . . . . . . 87 7.2.4 LoopCounter . . . . . . . . . . . . . . . . . . . . . . . . . 87 7.2.5 AccumulatorVariables . . . . . . . . . . . . . . . . . . . . 88 7.2.6 SummationofInputNumbers . . . . . . . . . . . . . . . . 88 7.3 Repeat-UntilLoop . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7.4 ForLoopStructure . . . . . . . . . . . . . . . . . . . . . . . . . . 92 7.4.1 SummationProblemwithaForLoop . . . . . . . . . . . . 92 7.4.2 FactorialProblem . . . . . . . . . . . . . . . . . . . . . . . 93 7.4.2.1 MathematicalSpecificationofFactorial . . . . . . 94 7.4.2.2 ComputingFactorial . . . . . . . . . . . . . . . . 94 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Chapter8 Arrays 99 8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 8.2 DeclaringanArray . . . . . . . . . . . . . . . . . . . . . . . . . . 100 8.2.1 DeclaringArraysinPseudo-Code . . . . . . . . . . . . . . 100 8.2.2 DeclaringArraysinC . . . . . . . . . . . . . . . . . . . . 100 8.3 OperationsonArrays . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.3.1 ManipulatingArrayElementsinPseudo-Code . . . . . . . 101 8.3.2 ManipulatingElementsofanArrayinC . . . . . . . . . . . 101 8.4 ArraysasArguments . . . . . . . . . . . . . . . . . . . . . . . . . 102 8.5 ASimpleApplicationwithArrays . . . . . . . . . . . . . . . . . . 104 8.6 ArithmeticOperationswithVectors . . . . . . . . . . . . . . . . . 106 8.7 Multi-DimensionalArrays . . . . . . . . . . . . . . . . . . . . . . 107 8.7.1 Multi-DimensionalArraysinPseudo-Code . . . . . . . . . 108 8.7.2 Multi-DimensionalArraysinC . . . . . . . . . . . . . . . 108 8.7.3 PassingMulti-DimensionalArrays . . . . . . . . . . . . . . 109 8.8 ApplicationsUsingArrays . . . . . . . . . . . . . . . . . . . . . . 110 8.8.1 ProblemswithSimpleArrayManipulation . . . . . . . . . 110 8.8.1.1 AverageValueinanArray. . . . . . . . . . . . . 110 8.8.1.2 MaximumValueinanArray . . . . . . . . . . . 112 8.8.2 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 8.8.2.1 LinearSearch . . . . . . . . . . . . . . . . . . . 113 8.8.2.2 BinarySearch . . . . . . . . . . . . . . . . . . . 114 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 ix Chapter9 Pointers 117 9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 9.2 PointerFundamentals . . . . . . . . . . . . . . . . . . . . . . . . 117 9.3 PointerswithC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 9.4 Dereferencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 9.5 ConvertingPointerTypes . . . . . . . . . . . . . . . . . . . . . . . 119 9.6 ReferenceParameters . . . . . . . . . . . . . . . . . . . . . . . . 119 9.7 PointerswithValueNULL . . . . . . . . . . . . . . . . . . . . . . 120 9.8 ArraysasPointers . . . . . . . . . . . . . . . . . . . . . . . . . . 122 9.8.1 PointerParametersforArrays . . . . . . . . . . . . . . . . 122 9.8.2 FunctionsthatReturnArrays . . . . . . . . . . . . . . . . . 123 9.8.3 DynamicMemoryAllocation . . . . . . . . . . . . . . . . 124 9.9 ComplexDataStructures . . . . . . . . . . . . . . . . . . . . . . . 125 9.9.1 StructureTypes . . . . . . . . . . . . . . . . . . . . . . . . 125 9.9.2 ArrayofStructures . . . . . . . . . . . . . . . . . . . . . . 125 9.9.3 PointerstoStructures . . . . . . . . . . . . . . . . . . . . . 126 9.10 DefiningTypeNames . . . . . . . . . . . . . . . . . . . . . . . . 127 9.11 EnumeratedTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Chapter10 LinkedLists 131 10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 10.2 NodesandLinkedList . . . . . . . . . . . . . . . . . . . . . . . . 131 10.2.1 Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 10.2.2 ManipulatingLinkedLists . . . . . . . . . . . . . . . . . . 133 10.2.3 ExampleofManipulatingaLinkedList . . . . . . . . . . . 135 10.2.4 LinkedListasanAbstractDataType . . . . . . . . . . . . 138 10.3 LinkedListwithTwoEnds . . . . . . . . . . . . . . . . . . . . . . 142 10.4 Double-LinkedLists . . . . . . . . . . . . . . . . . . . . . . . . . 143 10.5 Higher-levelDataStructures . . . . . . . . . . . . . . . . . . . . . 145 10.5.1 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 10.5.2 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Chapter11 TextData 153 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 11.2 CStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 11.3 StringInputandOutput . . . . . . . . . . . . . . . . . . . . . . . 154 11.4 StringOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 11.5 UsingtheStringFunctions . . . . . . . . . . . . . . . . . . . . . . 156 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

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.