249987139

6
INTRODUCTION TO ENGINEERING PROGRAMMING: IN C, MATLAB AND JAVA Mark Austin Department of Civil Engineering University of Maryland College Park, Maryland David Chancogne Institute for Systems Research University of Maryland College Park, Maryland John Wiley & Sons, Inc. New York Chichester Weinheim Brisbane Singapore Toronto

description

Introduction to Engineering Programming: In C, Matlab and Java

Transcript of 249987139

Page 1: 249987139

INTRODUCTION TO ENGINEERING PROGRAMMING: IN C, MATLAB AND JAVA

Mark Austin Department of Civil Engineering University of Maryland College Park, Maryland

David Chancogne Institute for Systems Research University of Maryland College Park, Maryland

John Wiley & Sons, Inc. New York Chichester Weinheim Brisbane Singapore Toronto

Page 2: 249987139

CONTENTS

ABOUT THE AUTHORS PREFACE ACKNOWLEDGMENTS

in v x

PART 1 : CONCEPTS IN MODERN ENGINEERING COMPUTATIONS 1

CHAPTER 1 : INTRODUCTION TO ENGINEERING COMPUTATIONS

1.1 Applications of Computers in Engineering 1.2 Recent Advances in Computing 1.3 Computer Hardware Concepts 1.4 Operating System Concepts 1.5 Computer Networking Concepts 1.6 Review Exercises

CHAPTER 2: PRINCIPLES OF ENGINEERING SOFTWARE DEVELOPMENT

2.1 Hardware-Software Life Cycle 2.2 Principles of Engineering Software Design 2.3 Computer Programming Language Concepts 2.4 Programming Language Selection

3 3 4 6 8 9

19

20 20 22 30 34

Page 3: 249987139

XI I CONTENTS

PART 2: С PROGRAMMING TUTORIAL 39

CHAPTER 3: GETTING STARTED 41 3.1 Key Features of С 41 3.2 A Little History 42 3.3 Writing and Compiling a Simple С Program 42 3.4 Program 3.1: Print Approximate Value of n 46 3.5 Program 3.2: Compute and Print Area of Circle 52 3.6 Compiling and Running Small С Programs 62 3.7 Programming Guidelines 66 3.8 Programming Exercises 67

CHAPTER 4: BASIC DATA TYPES AND VARIABLES 70 4.1 Basic Data Types in С 70 4.2 Characters 71 4.3 Integers 76 4.4 Single and Double Precision Floating Point Numbers 78 4.5 Enumeration Data Types 81 4.6 Variable Attributes: Type, Address, Name, and Value 86 4.7 Variable Naming Conventions 88 4.8 Programming Exercises 91

CHAPTER 5: OPERATORS AND EXPRESSIONS 95 5.1 Arithmetic Operators and Expressions 95 5.2 Assignment Operations 96 5.3 Increment/Decrement Operators 97 5.4 Arithmetic Expressions and Precedence 100 5.5 Mixed Expressions and Data Type Conversions 107 5.6 Bitwise Operations and Applications - 110 5.7 Subtractive Cancellation 113 5.8 Programming Exercises 116

CHAPTER 6: CONTROL OF FLOW 121 6.1 Introduction 121 6.2 Relational and Logical Operands 121 6.3 Selection Constructs 124 6.4 Iteration Constructs 133 6.5 Comparison of Looping Constructs 142 6.6 Programming Exercises 143

CHAPTER 7: FUNCTIONS I 146 7.1 Introduction 146

Page 4: 249987139

CONTENTS XIII

7.2 Functions Required in a C Program 7.3 Writing User-Defined Functions 7.4 Case Study Program: Quadratic Equation Solver 7.5 Call-by-Value Mechanism for Function Calls 7.6 Scope and Storage of Variables and Functions 7.7 Packaging of Program Modules 7.8 Math Library Functions 7.9 Programming Exercises

148 148 151 162 165 173 175 181

CHAPTER 8: ARRAYS AND POINTERS 1 Need for Arrays and Pointers 2 Introduction to Arrays 3 Initializing the Contents of an Array

8.4 Character Arrays 5 Applications of One-Dimensional Arrays 6 Multidimensional Arrays 7 Application of Two-Dimensional Arrays 8 Pointers 9 Pointer Type, Address, Name, Value, and Indirect Value

8.10 Pointer Arithmetic 8.11 Relationship between Pointers and Arrays

12 Programming Exercises

184 184 185 188 190 193 208 21 1 217 217 223 225 232

CHAPTER 9: FUNCTIONS II 9.1 Introduction 9.2 Pointers as Function Arguments 9.3 Arrays as Function Arguments 9.4 String Functions 9.5 Programming Exercises

241 241 241 246 252 262

CHAPTER 10: DYNAMIC ALLOCATION OF MEMORY 266 10.1 Need for Dynamic Memory Allocation 266 10.2 Memory Allocation with ma l l oc ( ) , c a l l o c ( ) , and r e a l l o c () 267 10.3 Deallocating Memory with f r e e () 270 10.4 Dynamic Allocation of Character Strings 27 1 10.5 Generic Functions for Dynamic Memory Allocation 273 10.6 A File of Miscellaneous Functions 274 10.7 Dynamic Allocation of One-Dimensional Arrays 278 10.8 Dynamic Allocation of Two-Dimensional Arrays 283 10.9 Programming Exercises 294

Page 5: 249987139

xiv CONTENTS

CHAPTER I I : THE С PREPROCESSOR 300 11.1 Compiling Multiple File С Programs 300 11.2 Capabilities of the С Preprocessor 302

CHAPTER 12: INPUT AND OUTPUT 309 12.1 Basic Concepts 309 12.2 Using p r i n t f O for Buffered Output 310 12.3 Using scanf () for Buffered Input 316 12.4 File Input/Output 319 12.5 Program 12.1: Statistics of Weekly Rainfall 322 12.6 Reading Datasets from an Input File 326 12.7 Program 12.2: Memory Reallocation for Datasets 328 12.8 Programming Exercises 334

PART 3: MATLAB PROGRAMMING TUTORIAL 337

CHAPTER 13: INTRODUCTION TO MATLAB 339 13.1 Getting Started 339 13.2 Variables and Variable Arithmetic 342 13.3 Matrices and Matrix Arithmetic 351 13.4 Control Structures 367 13.5 General-Purpose Matrix Functions 372 13.6 Program Development with M-Files 375 13.7 Engineering Applications 382 13.8 Programming Exercises 402

CHAPTER 14: MATLAB GRAPHICS 408 14.1 Simple Two-Dimensional Plotting 408 14.2 Three-Dimensional Plots 417 14.3 Mesh and Surface Plotting 418 14.4 Contour Plots 421 14.5 Subplots 423 14.6 Hard Copies of MATLAB Graphics 424 14.7 Preparing MATLAB Graphics for the World Wide Web 425 14.8 Programming Exercises 425

CHAPTER 15: SOLUTION OF LINEAR MATRIX EQUATIONS 430 15.1 Systems of Linear Matrix Equations 430 15.2 Case Study Problem: Three Linear Matrix Equations 434 15.3 Singular Systems of Matrix Equations 436 15.4 Engineering Applications 437 15.5 Programming Exercises 457

Page 6: 249987139

CONTENTS xv

PART 4: JAVA PROGRAMMING TUTORIAL 465

CHAPTER 16: INTRODUCTION TO JAVA 467 16.1 Java—A Little History 467 16.2 The "Famous" Java Buzzwords 468 16.3 Java and the Internet 476

CHAPTER 17: OBJECT-ORIENTED PROGRAM DESIGN 481 17.1 Fundamental Concepts 481 17.2 Object-Oriented Software Development 483 17.3 Is Java Object-Oriented? 488 17.4 Review Exercises 489

CHAPTER 18: THE JAVA LANGUAGE 490 18.1 Getting Started 490 18.2 Program 18.1: "Peace on Earth" Stand-Alone Program 492 18.3 Program 18.2: "Peace on Earth" Java Applet Program 497 18.4 Primitive Data Types 501 18.5 Java Variables 502 18.6 Expressions 505 18.7 Control Statements 506 18.8 Classes and Objects 510 18.9 Class Libraries and Methods 521 18.10 Arrays 527 18.11 Program 18.3: Compute Roots of Quadratic Equation 529 18.12 Program 18.4: Point and Line Segment Operations 538 18.13 Programming Exercises 548

CHAPTER 19: JAVA GRAPHICS 554 19.1 Introduction 554 19.2 The Java Abstract Windowing Toolkit 555 19.3 AWT Components and Containers 558 19.4 AWT Graphics 562 19.5 AWT Layout Managers 569 19.6 AWT I/O Components 571 19.7 Handling AWT Events 580 19.8 Engineering Applications 590 19.9 Programming Exercises 614

APPENDIX I : INTRODUCTION TO UNIX 621

APPENDIX 2: THE STANDARD LIBRARY 629

REFERENCES 649

INDEX 651