PROBLEM SOLVING WITH FORTRAN 90

Similar documents
CROSS-REFERENCE TABLE ASME A Including A17.1a-1997 Through A17.1d 2000 vs. ASME A

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Certificate Program. Introduction to Microsoft Excel 2013

Introduction to Windchill PDMLink 10.2 for the Implementation Team

Measurement in Science

Introduction to Creo Elements/Direct 19.0 Modeling

Andale Store Getting Started Manual

Introduction to PTC Windchill MPMLink 11.0

AAM Guide for Authors

Welcome to Starting Out with Programming Logic and Design, Third Edition.

"Charting the Course... SharePoint 2007 Hands-On Labs Course Summary

ENGINEERING PROBLEM SOLVING WITH C++

Verilog HDL. A Guide to Digital Design and Synthesis. Samir Palnitkar. SunSoft Press A Prentice Hall Title

F# for Scientists. Jon Harrop Flying Frog Consultancy Ltd. Foreword by Don Syme A JOHN WILEY & SONS, INC., PUBLICATION WILEY

DATA STRUCTURES AND ALGORITHMS

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

DATA STRUCTURES AND ALGORITHMS

VMware - vsphere INSTALL & CONFIGURE BEYOND INTRODUCTION V1.3

C Programming for Electronic Engineers

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

7. Mobile Wallets 78

A Web-Based Introduction

Table of Contents at a Glance

JAVA CONCEPTS Early Objects

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents Chapter 1 Introduction to Programming and the Java Language

Brief Contents. Foreword by Sarah Frostenson...xvii. Acknowledgments... Introduction... xxiii. Chapter 1: Creating Your First Database and Table...

Chapter 1: Excel in a Nutshell 3 Chapter 2: Basic Facts about Formulas 33 Chapter 3: Working with Names 59

Computer Programming C++ (wg) CCOs

Contents Computing with Formulas

This manual is intended for Fortran 77 programmers who want to use Fortran 77 under GCOS7.

A Brief Outlook at Block Ciphers

Mathematics Shape and Space: Polygon Angles

SOME ASSEMBLY REQUIRED

RULES OF THE TENNESSEE DEPARTMENT OF STATE DIVISION OF BUSINESS SERVICES CHAPTER UNIFORM COMMERCIAL CODE SEARCH REQUESTS AND REPORTS

Practical C++ Programming

SQL Server T-SQL Recipes. Andy Roberts Wayne Sheffield. A Problem-Solution Approach. Jonathan Gennick. Jason Brimhall David Dye

About the Authors. Preface

SQL Queries. for. Mere Mortals. Third Edition. A Hands-On Guide to Data Manipulation in SQL. John L. Viescas Michael J. Hernandez

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Contents. Preface. Introduction. Introduction to C Programming

Shop Manager Help. Version 5

Before You Begin 1 Graphing Application 1 Introducing Computers, the Internet and Visual Basic.NET

Subject to Change Drawing Application 1 Introducing Computers, the Internet and C#

Chapters and Appendix F are PDF documents posted online at the book s Companion Website (located at

Chapters are PDF documents posted online at the book s Companion Website (located at

"Charting the Course... Java Programming Language. Course Summary

CHAPTER 1 BOOLEAN ALGEBRA CONTENTS

BMEGUI Tutorial 1 Spatial kriging

The Verilog Hardware Description Language, Fifth Edition

INFORMATION HIDING IN COMMUNICATION NETWORKS

Integrated Algebra 2 and Trigonometry. Quarter 1

Course Title: C Programming Full Marks: Course no: CSC110 Pass Marks: Nature of course: Theory + Lab Credit hours: 3

PROGRAMMING AND ENGINEERING COMPUTING WITH MATLAB Huei-Huang Lee SDC. Better Textbooks. Lower Prices.

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

C# Programming: From Problem Analysis to Program Design. Fourth Edition

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Python Scripting for Computational Science

Employer Self Service (ESS) User Quick Guide

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Watcom FORTRAN 77. Language Reference. Edition 11.0c

Structured Parallel Programming

FAQ for PVRTV-305U. Copyright KWorld Computer Co., Ltd. All rights are reserved. November 9, 2007

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Absolute C++ Walter Savitch

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Excel Scientific and Engineering Cookbook

ME1107 Computing Y Yan.

Structured Parallel Programming Patterns for Efficient Computation

SAP' ABAP. Handbook. Kogent Learning Solutions, Inc. Sudbury, Massachusetts JONES AND BARTLETT PUBLISHERS BOSTON TORONTO LONDON SINUAPORI:

Safehome Project. Analysis Model Prepared by Inyoung Kim Donghan Jang <TEAM 6> KAIST CS350 Introduction to Software Engineering

Volunteer Registration Instructions

COURSE TITLE. Computer Programming C++ LENGTH. One Semester Grades DEPARTMENT. Computer Department Barbara O Donnell, Supervisor SCHOOL

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

Huei-Huang Lee. Programming with MATLAB2016 SDC ACCESS CODE. Better Textbooks. Lower Prices. UNIQUE CODE INSIDE

PTC Mathcad Prime 3.0

Scheme of work Cambridge International AS & A Level Computing (9691)

Fortran 77 Language Reference Manual

Model Viva Questions for Programming in C lab

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Numerical Modelling in Fortran: day 2. Paul Tackley, 2017

List of Modules with its Forms and Reports (IFW Campus ERP - EduTech)

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

Essential MATLAB for Engineers and Scientists

Contents. Introduction

С and the Programming for Multitasking. Thomas W. Schultz. Purdue University West Lafayette, Indiana

List of Figures. About the Authors. Acknowledgments

Contents. Chapter 1 SPECIFYING SYNTAX 1

Excel for Chemists. Second Edition

Survey questions. Winter Tracking Survey 2012 Final Topline 02/22/2012 Data for January 20 February 19, p e w i n t e r n e t.

3 Welcome Application 41 Introduction to Visual Programming

Introduction. Assessment Test. Part I The Programmer s Exam 1

RAJALAKSHMI ENGINEERING COLLEGE Thandalam, Chennai Department of Computer Science and Engineering CS17201 DATA STRUCTURES Unit-II-Assignment

EMBEDDED SYSTEMS: Jonathan W. Valvano INTRODUCTION TO THE MSP432 MICROCONTROLLER. Volume 1 First Edition June 2015

Preface. Features of the Third Edition

BFW-037 B.Sc. FOOTWEAR TECHNOLOGY (BSCFWT) Term-End Examination. December, 2014

GUIDE LINES FOR ONLINE SUBMISSION OF APPLICATIONS FOR COMPETITIVE EXAMINATION (CSS) 2011

IT 341 Introduction to System Administration Project I Installing Ubuntu Server on an Virtual Machine

Transcription:

David R. Brooks PROBLEM SOLVING WITH FORTRAN 90 FOR SCIENTISTS AND ENGINEERS Springer

Contents Preface v 1.1 Overview for Instructors v 1.1.1 The Case for Fortran 90 vi 1.1.2 Structure of the Text vii Introductory Material vii Presentation of the Language ix i. 1.3 Decisions About Content ix Program Layout x Data Types x Derived Data Types xi Pointers and Allocatable Arrays xi Recursion xii Explicit vs. Implicit Typing xiii COMMON Blocks xiii Program Modularization xiv Arrays xiv 1.1.4 Pedagogical Features xv A Formal Problem-Solving Process Is Followed Throughout the Text xv Some Applications and Problems Appear More Than Once xv Programs and Exercises xvi 1.1.5 The Compiler Used to Prepare Programs for This Text... xvii 1.2 Overview for Students xviii 1.2.1 The Purpose of This Text xviii 1.2.2 The Approach Taken by the Text xviii 1.2.3 What Does This Textbook Expect from You xx 1.2.4...and What Does It Offer in Return? xxi 1.3 Useful References for Fortran 90 xxii 1.4 Contacting the Author xxii 1.5 Obtaining Source Code and Data Files for Programs in this Text xxii 1.6 Acknowledgments xxiii 1. Computing Environments for Problem Solving 1 1.1 A Brief History of Electronic Computing 1 1.1.1 The First Generation 1 1.1.2 The Second and Third Generations 2 1.1.3 The Fourth Generation 3

xxvi Contents 1.2 The Academic Computing Environment 6 1.2.1 The Department-Based Computing Environment 8 1.2.2 The Personal Computing Environment 9 1.3 What Do You Need to Know About Your Computing Environment? 10 1.4 Fortran and the Personal Computing Environment 11 1.5 Is Programming Necessary Anymore? 14 1.6 Exercises 16 2. Solving Problems with a High-Level Programming Language 19 2.1 Structured Programming and Problem Solving 19 2.1.1 A High-Level Programming Language Provides the Tool.. 19 2.1.2 Developing a Formal Approach to Problem Solving 22 2.1.3 Beware of Logical Errors 32 2.2 Designing Algorithms for Procedural Programming Languages... 34 2.2.1 Getting Started 34 2.2.2 Executable Instructions 35 2.2.3 Data Types and Variables 37 Data types 37 Variables 39 2.2.4 Designing Algorithms for Solving Problems 40 Defining a pseudocode language 40 The three basic program control structures 44 Adding to your pseudocode vocabulary 45 Flowcharts: another way to visualize algorithms 46 2.3 Program Modularization 47 2.4 Applications 49 2.4.1 Maximum Deflection of a Beam Under Load 49 2.4.2 Oscillating Frequency of an LC Circuit 53 2.5 Debugging Your Algorithms 56 2.5.1 Algorithm Style 56 2.5.2 Problems with Your Algorithms 57 2.6 Exercises 57 2.6.1 Self-Testing Exercises 57 2.6.2 Basic Algorithm Development Exercises 59 2.6.3 Algorithm Development Applications 60 3. Getting Started with Fortran: Writing Simple Programs 69 3.1 A Simple Problem and a Fortran Program to Solve It 69 3.2 Program Layout 73 3.2.1 What Is "Source Code?" 73 3.2.2 Where Does Source Code Come From? 75 3.2.3 Why Do the Fortran Statements All Start in Column 7?... 77

Contents xxvii 3.2.4 Inserting Comments in Source Code Files 78 3.2.5 How Much of P-3.1 Is Absolutely Necessary and How Much Is a Matter of Style? 79 3.2.6 Fortran Keywords 80 3.2.7 What If a Statement Is Too Long to Fit on a Single Line? 81 3.2.8 Executable and Nonexecutable Statements 82 3.3 Declaring Variables and Defining Constants 83 3.3.1 Variable Names and Data Type Declarations 83 3.3.2 Intrinsic Data Types 85 Type declarations for intrinsic data types 85 Type declarations for numbers 86 Type declarations for logical variables 87 Type declarations for characters and strings of characters 88 The PARAMETER attribute and statement 89 Enforcing explicit typing 90 The implications of type declaration 91 Using nonexecutable statements in programs 24 3.3.3 Constants 92 Integers and real numbers 92 Logical constants 93 Character constants 94 3.3.4 Initializing Values with the DATA Statement 95 3.4 List-Directed Input and Output 96 3.4.1 Reading and Displaying Numerical Values 96 Displaying a prompt for user input 96 Getting input from the keyboard 97 Displaying output 101 3.4.2 Manipulating Text Information 102 3.5 Arithmetic Operators, Assignment Statements, and Calculations 104 3.5.1 Arithmetic Operators, Expressions, and the Assignment Operator 104 3.5.2 Assignment Statements That Increment Variables 107 3.5.3 Mixed-Mode Calculations 109 3.5.4 Using Compatible Constants 113 3.5.5 Operator Precedence 114 3.6 Program Termination 118 3.7 Compiling and Executing a Program 119 3.7.1 Source Code Portability 119 3.7.2 Compilation and Execution 120 3.7.3 Saving Output from Your Programs 121

xxviii Contents 3.8 Applications 122 3.8.1 Maximum Deflection of a Beam Under Load 122 3.8.2 Relativistic Mass and Speed of an Electron 125 3.9 Debugging Your Programs 128 3.9.1 Programming Style 128 3.9.2 Your Programs Will Often Contain Errors 129 3.9.3 Some Common Errors 129 Compilation Errors 130 Execution Errors 130 Logical Errors 131 3.9.4 Forcing Your Programs to Fail 133 3.10 Exercises 134 3.10.1 Self-Testing Exercises 134 3.10.2 Basic Programming Exercises 136 3.10.3 Programming Applications 137 4. Using Functions to Expand the Power of Fortran 147 4.1 Fortran Intrinsic Functions 147 4.1.1 Intrinsic Functions for Arithmetic Calculations: Arguments and Ranges 147 4.1.2 Intrinsic Functions for Character and String Manipulation 153 4.1.3 Examples of Calculations Using Intrinsic Functions 157 Polar/Cartesian conversions 157 Calculating the absolute value 161 The remainder from integer or real division 161 4.2 Fortran Statement Functions 162 4.3 Applications 166 4.3.1 Refraction of Light 166 4.3.2 Inverse Hyperbolic Functions 170 4.4 Debugging Your Programs 175 4.4.1 Programming Style 175 4.4.2 Problems with Programs 175 4.5 Exercises 177 4.5.1 Self-Testing Exercises 177 4.5.2 Basic Programming Exercises 178 4.5.3 Programming Applications 179 5. Gaining Control Over Program Output 187 5.1 The Formatted PRINT Statement 187 5.1.1 Introduction to Formatted Output 187

Contents xxix 5.1.2 Format Descriptors for Numbers and Character Strings.. 190 INTEGER descriptors 190 REAL descriptors 190 CHARACTER descriptors 193 LOGICAL descriptor 193 General descriptor 193 String constant descriptors 193 5.1.3 Control Descriptors 193 Carriage control 193 Skipping spaces 194 Sign display 194 New lines 195 Tabulation 195 Format termination 195 5.1.4 Repeating Groups of Descriptors 196 5.1.5 Producing Formatted Output Without a FORMAT Statement 196 5.2 The WRITE Statement 198 5.2.1 Syntax of the WRITE Statement and the Standard Output Unit 198 5.2.2 Format Descriptors for the WRITE Statement 199 5.3 Saving Program Output 199 5.4 Applications 203 5.4.1 Stellar Magnitude and Distance 203 5.4.2 Relativistic Mass and Speed of an Electron 205 5.5 Debugging Your Programs 207 5.5.1 Programming Style 207 5.5.2 Problems with Programs 207 5.6 Exercises 208 5.6.1 Self-Testing Exercises 208 5.6.2 Basic Programming Exercises 209 5.6.3 Programming Applications 210 6. Program Control: Branching and Repetitive Calculations 215 6.1 Using Program Control Structures 215 6.1.1 The IF...THEN...(ELSE...) Pseudocode Construct 215 6.1.2 The CHOOSE Pseudocode Command 217 6.1.3 The LOOP...END LOOP Pseudocode Command 219 Count-controlled loops 219 Conditional loops 220 6.2 Relational and Logical Operators and the Fortran IF... Construct 222

xxx Contents 6.2.1 Relational and Logical Operators 222 6.2.2 The IF... Construct 224 6.2.3 Using the IF... Construct in Programs 229 6.3 The SELECT CASE Construct 232 6.4 Fortran Loop Constructs 234 6.4.1 Count-Controlled (DO...) Loops 234 6.4.2 Pre- and Post-Test Conditional Loop Constructs 241 Pre-test loops 241 Post-test loops 244 6.4.3 Nested Loops 247 6.4.4 Loop Design Considerations 248 6.5 Using Implied DO... Loops in Output Statements 249 6.6 Applications 251 6.6.1 Refraction of Light 251 6.6.2 Oscillating Frequency of an LC Circuit 253 6.6.3 Calculating Radiation Exposures for a Materials Testing Experiment 255 6.6.4 Maximum Deflection of a Beam with Various Support/Loading Systems 260 6.7 Debugging Your Programs 262 6.7.1 Programming Style 262 6.7.2 Logical Problems 263 6.7.3 Syntax and Execution Problems 264 6.8 Exercises 264 6.8.1 Self-Testing Exercises 264 6.8.2 Basic Programming Exercises 265 6.8.3 Programming Applications 266 7. Program Modularization 277 7.1 Designing Modularized Algorithms with the CALL and SUBPROGRAM Pseudocode Commands 277 7.2 Fortran Subroutines 284 7.2.1 Using Subroutines 288 7.2.2 Controlling the Flow of Information 289 Expressing intent 289 Enforcing intent 293 7.2.3 More About SUBROUTINE Syntax 296 7.3 Fortran Functions 297 7.4 Using Subroutines and Functions 299 7.4.1 Using the MODULE Structure for Passing Information to Subroutines and Functions 299 7.4.2 Initializing Information in Subroutines and Functions... 302

Contents xxxi 7.4.3 Using Subroutines and Functions in Argument and Parameter Lists 303 7.4.4 Choosing Between Subroutines and Functions 308 7.5 Applications 310 7.5.1 Relativistic Mass and Speed of an Electron 310 7.5.2 A Function Library for Converting Units 312 7.5.3 A Simple Character-Based Function Plotter 317 7.6 Debugging Your Programs 321 7.6.1 Programming Style 321 7.6.2 Problems with Programs 322 7.7 Exercises 322 7.7.1 Self-Testing Exercises 322 7.7.2 Basic Programming Exercises 323 7.7.3 Programming Applications 324 8. Using Arrays to Organize Information 333 8.1 Arrays in Structured Programming 333 8.2 Basic Array Implementation 337 8.2.1 Example: Testing a Random Number Generator 337 8.2.2 Declaring Arrays 340 8.2.3 Assigning Values to Arrays 342 Assigning a value to an entire array or part of an array.. 343 Assigning array elements with count-controlled loops... 344 Assigning values to individual array elements 344 Using a DATA statement to initialize arrays 345 Using elemental intrinsic functions 346 8.2.4 Displaying the Contents of Arrays (Implied DO... Loops) 348 8.2.5 Example: Monthly Ozone Summary 350 8.3 Using Statically Allocated Arrays in Subprograms 353 8.4 Allocatable Arrays 358 8.5 Treating Strings of Characters as Arrays of Characters 368 8.6 The TYPE Statement, Records, and Arrays of Records 369 8.7 Applications 377 8.7.1 Vector Operations 377 8.7.2 Cellular Automata and Sierpinski Triangles 380 8.7.3 Probability Analysis for Quality Control of Manufacturing Processes 385 8.8 Debugging Your Programs 390 8.8.1 Programming Style 390 8.8.2 Problems with Programs That Use Arrays 390 8.9 Exercises 391 8.9.1 Self-Testing Exercises 391

xxxii Contents 8.9.2 Basic Programming Exercises 392 8.9.3 Programming Applications 395 9. Using Formatted Sequential Access and Internal Files 401 9.1 The Text File Concept 401 9.2 OPEN, READ, and CLOSE Statements for Sequential File Access 404 9.2.1 Reading a File Containing Student Names and Grades... 405 9.2.2 The OPEN and REWIND Statements 408 9.2.3 The READ and BACKSPACE Statements 410 9.2.4 The CLOSE Statement 413 9.3 Files and Arrays 414 9.4 More About Formatted READ Statements 420 9.4.1 FORMAT Statements and Standard Field Descriptors... 420 9.4.2 Reading Internal Files 426 9.5 Writing Text Files 429 9.6 Applications 431 9.6.1 Exponential Smoothing of Data 431 9.6.2 Billing Program for an Urban Water Utility 437 9.6.3 Merging Sorted Lists 447 9.6.4 Creating a "Quote-and-Comma-Delimited" Input File for Spreadsheets 454 9.7 Debugging Your Programs 456 9.7.1 Programming Style 456 9.7.2 Problems with Programs That Access External Data Files 456 9.8 Exercises 458 9.8.1 Self-Testing Exercises 458 9.8.2 Basic Programming Exercises 459 9.8.3 Programming Applications 461 10. Some Essential Programming Algorithms 471 10.1 Introduction 471 10.2 Searching Algorithms 473 10.2.1 Linear Searches 474 10.2.2 Binary Search. 478 10.2.3 Comparing Searching Algorithms 483 10.2.4 A Driver Program for Testing Searching Algorithms... 483 10.3 Sorting Algorithms 486 10.3.1 Selection Sort 487 10.3.2 Insertion Sort 490 10.3.3 Efficiency of Sorting Algorithms 493 10.3.4 A Driver Program for Testing Sorting Algorithms 494 10.4 Recursive Algorithms 496 10.5 The Recursive Quicksort Algorithm 501

Contents xxxiii 10.6 Applications 509 10.6.1 Keeping a List of Words in Alphabetical Order 509 10.6.2 Evaluating Legendre Polynomials 514 10.7 Debugging Your Programs 517 10.7.1 Programming Style 517 10.7.2 Problems with Programs 517 10.8 Exercises 518 10.8.1 Self-Testing Exercises 518 10.8.2 Basic Programming Exercises 519 10.8.3 Programming Applications 520 11. Basic Statistics and Numerical Analysis with Fortran 533 11.1 Introduction 533 11.2 Basic Descriptive Statistics 534 11.2.1 The Sample Mean and Standard Deviation 534 11.2.2 Linear Regression and the Linear Correlation Coefficient 536 11.3 Numerical Differentiation 545 11.3.1 Newton's and Stirling's Formulas 545 11.3.2 Application. Estimating the Speed of a Falling Object.. 546 11.4 Numerical Integration 551 11.4.1 Polynomial Approximation Methods 551 11.4.2 Application: Evaluating the Gamma Function 556 11.5 Solving Systems of Linear Equations. 562 11.5.1 Linear Equations and Gaussian Elimination 562 11.5.2 Application: Current Row in a DC Circuit with Multiple Resistive Branches 570 11.6 Finding the Roots of Equations 572 11.7 Numerical Solutions to Differential Equations 579 11.7.1 Motion of a Damped Mass and Spring 579 11.7.2 Application. Current Flow in a Series LRC Circuit... 582 11.8 Exercises 591 11.8.1 Basic Programming Exercises 591 11.8.2 Programming Applications 592 12. A Closer Look 597 12.1 Introduction 597 12.2 Using More Than One Program Unit 598 12.2.1 Merging Source Code 600 12.2.2 Merging Object Code 602

xxxiv Contents 12.3 The Internal Representation of Numbers and Extended Precision 604 12.3.1 Internal Representation of Numbers 604 Integers 604 Real Numbers 606 12.3.2 Specifying Precision for Numerical Variables 608 12.4 Array Operations and Array Inquiry and Reduction 613 12.4.1 Intrinsic Array Operations 613 12.4.2 Array Functions 614 Array multiplication functions 616 Array inquiry functions 616 Array reduction functions 616 12.5 Direct Access and Unformatted (Binary) Files 622 12.5.1 Introduction to File Types 622 12.5.2 Using Other File Types 627 12.5.3 Example: Binary Search of a File 631 12.6 The COMPLEX Data Type 633 12.7 Data Sharing With COMMON Blocks 636 Appendices 641 Appendix 1. Table of ASCII Characters for IBM-Compatible PCs.. 641 Appendix 2. Summary of Pseudocode Commands and Fortran Statement Syntax 643 Appendix 2.1 Pseudocode Commands 643 Appendix 2.2 Fortran Statement Syntax 644 Appendix 3. Source Code File Name Summary 655 Appendix 4. Accessing the System Time and Date 661 Glossary 661 Index 673