Introduction to Programming with Fortran: with coverage of Fortran 90, 95, 2003 and 77

Download Introduction to Programming with Fortran: with coverage of Fortran 90, 95, 2003 and 77

Post on 14-Dec-2016

249 views

Category:

Documents

17 download

Embed Size (px)

TRANSCRIPT

<ul><li><p>Introduction to Programming with Fortran</p></li><li><p>Ian D. Chivers and Jane Sleightholme</p><p>Introduction toProgramming withFortranWith Coverage of Fortran 90, 95, 2003, and 77</p></li><li><p>Ian D. Chivers, BSc, PGCEd, MSc, MBCS Jane Sleightholme, MSc, MBCSRhymney Consulting Kings College LondonUK UK</p><p>British Library Cataloguing in Publication DataA catalogue record for this book is available from the British Library</p><p>Library of Congress Control Number: 2005931518</p><p>ISBN-10: 1-84628-053-2 eISBN 1-84628-054-0 Printed on acid-free paperISBN-13: 978-1-84628-053-5</p><p> Springer-Verlag London Limited 2006</p><p>Apart from any fair dealing for the purposes of research or private study, or criticism or review,as permitted under the Copyright, Designs and Patents Act 1988, this publication may only bereproduced, stored or transmitted, in any form or by any means, with the prior permission inwriting of the publishers, or in the case of reprographic reproduction in accordance with theterms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproductionoutside those terms should be sent to the publishers.</p><p>The use of registered names, trademarks, etc. in this publication does not imply, even in theabsence of a specific statement, that such names are exempt from the relevant laws andregulations and therefore free for general use.</p><p>The publisher makes no representation, express or implied, with regard to the accuracy of theinformation contained in this book and cannot accept any legal responsibility or liability for anyerrors or omissions that may be made.</p><p>Printed in the United States of America (MVY)</p><p>9 8 7 6 5 4 3 2 1</p><p>Springer Science+Business Mediaspringeronline.com</p></li><li><p>Acknowlegement</p><p>The material in the book has evolved firstly from our combined experience ofworking in Computing Services within the University of London at</p><p> King's College, IDC (19862002) and JS (1985 to date)</p><p> Chelsea College, JS (19781985)</p><p> Imperial College, IDC (19781986)</p><p>in the teaching, advice and support of Fortran and related areas, and secondly inthe provision of commercial training courses. The following are some of the or-ganisations we've provided training for:</p><p> AWE, Aldermaston.</p><p> Centre for Ecology and Hydrology, Wallingford.</p><p> Environment Agency, Worthing.</p><p> The Met Office, Bracknell and Exeter.</p><p> QinetiQ, Farnborough.</p><p> Rolls Royce, Derby.</p><p> Veritas DGC Ltd., Crawley.</p><p> Westland Helicopters, Yeovil.</p><p>The examples in the book are based on what will work with compilers that supportthe Fortran 90 and 95 standards and also support ISO TR 15580 and 15581. At thetime of writing this book there are no compilers that fully support the Fortran 2003standard.</p><p>Thanks are due to:</p><p> The staff and students at King's College, Chelsea College and ImperialCollege.</p></li><li><p> The people who have attended the commercial courses. Its been great funteaching you and things have been very lively at times.</p><p> The people on the Fortran 90 list and comp.lang.fortran. Access to the ex-pertise of several hundred people involved in the use and development ofFortran on a daily basis across a wide range of disciplines is inestimable.</p><p> The people at NAG for the provision of the Fortran 95 compilers and NagTools on the enclosed cd.</p><p> The staff and facilities at PTR Associates. It is a pleasure training there.</p><p> The patience of our families during the time required to develop thecourses upon which this book is based and whilst preparing the cam-era-ready copy.</p><p> Finally Rebecca Mowat, Joanne Cooling, Helen Desmond and BeverleyFord at Springer for their enthusiasm and encouragement!</p><p>Our King's home page is:</p><p> http://www.kcl.ac.uk/fortran</p><p>All of the program examples can be found there.</p><p>If you would like to contact us our email addresses are:</p><p>Ian D Chivers: ian.chivers@chiversandbryan.co.uk</p><p>Jane Sleightholme: jane.sleightholme@kcl.ac.uk</p><p>vi Acknowledgement</p></li><li><p>Contents</p><p>1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1</p><p>2 Introduction to Computer Systems . . . . . . . . . . . . . . . . . . 92.1 The core of a computer system . . . . . . . . . . . . . . . . . . 102.1.1 Central processor unit CPU. . . . . . . . . . . . . . . . . . . 102.1.2 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.1.3 Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2 Other components of a computer system . . . . . . . . . . . . . . 112.2.1 Disks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.2.2 Others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.3 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.5 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 13</p><p>3 Introduction to Operating Systems . . . . . . . . . . . . . . . . . 153.1 History of operating systems. . . . . . . . . . . . . . . . . . . . 163.1.1 The 1940s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.2 The 1950s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.3 The 1960s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.4 The 1960s and 1970s . . . . . . . . . . . . . . . . . . . . . . . 163.1.5 The 1970s, 1980s, and 1990s . . . . . . . . . . . . . . . . . . . 173.2 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.3 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.4 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 18</p><p>4 Introduction to Using a Computer System. . . . . . . . . . . . . . 194.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.2 Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.3 Single-user systems . . . . . . . . . . . . . . . . . . . . . . . . 204.4 Networked systems . . . . . . . . . . . . . . . . . . . . . . . . 204.5 Multiuser systems. . . . . . . . . . . . . . . . . . . . . . . . . 214.6 Other useful things to know . . . . . . . . . . . . . . . . . . . . 21</p></li><li><p>4.7 Common methods of using computer systems to develop Fortranprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22</p><p>4.8 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 23</p><p>5 Introduction to Problem Solving . . . . . . . . . . . . . . . . . . 255.1 Natural language . . . . . . . . . . . . . . . . . . . . . . . . . 265.2 Artificial language . . . . . . . . . . . . . . . . . . . . . . . . 275.2.1 Notations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275.3 Resum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285.4.1 Top-down . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285.4.2 Bottom-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285.4.3 Stepwise refinement. . . . . . . . . . . . . . . . . . . . . . . . 295.4.4 Modular programming . . . . . . . . . . . . . . . . . . . . . . 295.4.5 Object oriented programming . . . . . . . . . . . . . . . . . . . 295.5 Systems analysis and design . . . . . . . . . . . . . . . . . . . . 305.5.1 Problem definition . . . . . . . . . . . . . . . . . . . . . . . . 305.5.2 Feasibility study and fact finding . . . . . . . . . . . . . . . . . 305.5.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315.5.4 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315.5.5 Detailed design . . . . . . . . . . . . . . . . . . . . . . . . . . 315.5.6 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . 315.5.7 Evaluation and testing. . . . . . . . . . . . . . . . . . . . . . . 315.5.8 Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . 325.6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 325.7 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325.8 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 33</p><p>6 Introduction to Programming Languages . . . . . . . . . . . . . . 356.1 Some early theoretical work . . . . . . . . . . . . . . . . . . . . 366.2 What is a programming language? . . . . . . . . . . . . . . . . . 366.3 Program language development and engineering . . . . . . . . . . 366.4 The early days . . . . . . . . . . . . . . . . . . . . . . . . . . 366.4.1 Fortran The Early Days . . . . . . . . . . . . . . . . . . . . 376.4.2 Fortran 77 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376.4.3 Cobol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376.4.4 Algol. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386.5 Chomsky and program language development . . . . . . . . . . . 396.6 Lisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396.7 Snobol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406.8 Second-generation languages . . . . . . . . . . . . . . . . . . . 406.8.1 PL/1 and Algol 68 . . . . . . . . . . . . . . . . . . . . . . . . 406.8.2 Simula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406.8.3 Pascal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41</p><p>viii Contents</p></li><li><p>6.8.4 APL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416.8.5 Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416.8.6 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416.9 Some other strands in language development . . . . . . . . . . . . 426.9.1 Abstraction, stepwise refinement and modules . . . . . . . . . . . 426.9.2 Structured programming. . . . . . . . . . . . . . . . . . . . . . 426.9.3 Standardisation . . . . . . . . . . . . . . . . . . . . . . . . . . 426.10 Ada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436.11 Modula. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436.12 Modula 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446.13 Other language developments . . . . . . . . . . . . . . . . . . . 446.13.1 Logo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446.13.2 Postscript, TeX and LaTeX . . . . . . . . . . . . . . . . . . . . 456.13.3 Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456.13.4 SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456.13.5 ICON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466.14 Object orientated programming OOP . . . . . . . . . . . . . . 466.14.1 Oberon and Oberon 2 . . . . . . . . . . . . . . . . . . . . . . . 466.14.2 Smalltalk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476.14.3 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486.14.4 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486.14.5 Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . 496.14.6 C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496.15 Fortran 90 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506.16 Fortran 1995 . . . . . . . . . . . . . . . . . . . . . . . . . . . 516.17 ISO technical reports TR15580 and TR15581 . . . . . . . . . . . 526.18 Fortran 2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . 526.19 DTR 19767 enhanced module facilities. . . . . . . . . . . . . . . 536.20 Internet resources . . . . . . . . . . . . . . . . . . . . . . . . . 546.20.1 Standards information . . . . . . . . . . . . . . . . . . . . . . . 546.20.2 Fortran discussion lists . . . . . . . . . . . . . . . . . . . . . . 556.20.3 Other sources . . . . . . . . . . . . . . . . . . . . . . . . . . . 556.21 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566.22 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 56</p><p>7 Introduction to Programming . . . . . . . . . . . . . . . . . . . . 637.1 Language strengths and weaknesses . . . . . . . . . . . . . . . . 647.2 Elements of a programming language . . . . . . . . . . . . . . . 647.2.1 Data description statements . . . . . . . . . . . . . . . . . . . . 657.2.2 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . 657.2.3 Data-processing statements . . . . . . . . . . . . . . . . . . . . 657.2.4 Input and output (I/O) statements . . . . . . . . . . . . . . . . . 657.3 Variables name, type and value . . . . . . . . . . . . . . . . . 68</p><p>Contents ix</p></li><li><p>7.4 Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707.5 Some more Fortran rules . . . . . . . . . . . . . . . . . . . . . 717.6 Fortran character set . . . . . . . . . . . . . . . . . . . . . . . 727.7 Good programming guidelines . . . . . . . . . . . . . . . . . . . 737.8 Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737.9 Program development . . . . . . . . . . . . . . . . . . . . . . . 747.10 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75</p><p>8 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778.1 Rounding and truncation . . . . . . . . . . . . . . . . . . . . . 818.2 Time taken for light to travel from the Sun to Earth. . . . . . . . . 838.3 The PARAMETER statement . . . . . . . . . . . . . . . . . . . 848.4 Range, precision and size of numbers . . . . . . . . . . . . . . . 858.5 Health warning: optional reading, beginners are advised to</p><p>leave until later . . . . . . . . . . . . . . . . . . . . . . . . . . 888.5.1 Selecting different INTEGER kind types . . . . . . . . . . . . . . 908.5.2 Selecting different REAL kind types . . . . . . . . . . . . . . . . 918.5.3 Specifying kind types for literal integer and real constants. . . . . . 918.5.4 Positional number systems. . . . . . . . . . . . . . . . . . . . . 928.5.5 Bit data type and representation model . . . . . . . . . . . . . . . 928.5.6 Integer data type and representation model . . . . . . . . . . . . . 938.5.7 Real data type and representation model . . . . . . . . . . . . . . 938.5.8 IEEE 754. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948.5.9 Testing the numerical representation of different kind types on a</p><p>system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948.5.10 Binary representation of different integer kind type numbers . . . . 988.5.11 Binary representation of a real number . . . . . . . . . . . . . . 1008.5.12 Summary of how to select the appropriate kind type . . . . . . . . 1018.6 Variable status. . . . . . . . . . . . . . . . . . . . . . . . . . 1018.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018.8 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028.9 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . 105</p><p>9 Arrays 1: Some Fundamentals . . . . . . . . . . . . . . . . . . . 1079.1 Tables of data . . . . . . . . . . . . . . . . . . . . . . . . . . 1089.1.1 Telephone directory . . . . . . . . . . . . . . . . . . . . . . . 1089.1.2 Book catalogue . . . . . . . . . . . . . . . . . . . . . . . . . 1089.1.3 Examination marks or results . . . . . . . . . . . . . . . . . . . 1099.1.4 Monthly rainfall . . . . . . . . . . . . . . . . . . . . . . . . . 1099.2 Arrays in Fortran . . . . . . . . . . . . . . . . . . . . . . . . 1109.3 The DIMENSION attr...</p></li></ul>