Introduction to Programming with Fortran Ian D. Chivers and Jane Sleightholme Introduction to Programming with Fortran With Coverage of Fortran 90, 95, 2003, and 77 Ian D. Chivers, BSc, PGCEd, MSc, MBCS Jane Sleightholme, MSc, MBCS Rhymney Consulting Kings College London UK UK British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library Library of Congress Control Number: 2005931518 ISBN-10: 1-84628-053-2 eISBN 1-84628-054-0 Printed on acid-free paper ISBN-13: 978-1-84628-053-5 © Springer-Verlag London Limited 2006 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 be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. The use of registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Printed in the United States of America (MVY) 9 8 7 6 5 4 3 2 1 Springer Science+Business Media springeronline.com Acknowlegement The material in the book has evolved firstly from our combined experience of workinginComputingServiceswithintheUniversityofLondonat • King'sCollege,IDC(1986–2002)andJS(1985todate) • ChelseaCollege,JS(1978–1985) • ImperialCollege,IDC(1978–1986) in the teaching, advice and support of Fortran and related areas, and secondly in the provision of commercial training courses. The following are some of the or- ganisationswe'veprovidedtrainingfor: • AWE,Aldermaston. • CentreforEcologyandHydrology,Wallingford. • EnvironmentAgency,Worthing. • TheMetOffice,BracknellandExeter. • QinetiQ,Farnborough. • RollsRoyce,Derby. • VeritasDGCLtd.,Crawley. • WestlandHelicopters,Yeovil. The examples in the book are based on what will work with compilers that support the Fortran 90 and 95 standards and also support ISO TR 15580 and 15581. At the time of writing this book there are no compilers that fully support the Fortran 2003 standard. Thanksaredueto: • The staff and students at King's College, Chelsea College and Imperial College. vi Acknowledgement • The people who have attended the commercial courses. Its been great fun teachingyouandthingshavebeenverylivelyattimes. • 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 of Fortranonadailybasisacrossawiderangeofdisciplinesisinestimable. • The people at NAG for the provision of the Fortran 95 compilers and Nag Toolsontheenclosedcd. • ThestaffandfacilitiesatPTRAssociates. Itisapleasuretrainingthere. • The patience of our families during the time required to develop the courses upon which this book is based and whilst preparing the cam- era-readycopy. • Finally Rebecca Mowat, Joanne Cooling, Helen Desmond and Beverley FordatSpringerfortheirenthusiasmandencouragement! OurKing'shomepageis: • http://www.kcl.ac.uk/fortran Alloftheprogramexamplescanbefoundthere. Ifyouwouldliketocontactusouremailaddressesare: IanDChivers:[email protected] JaneSleightholme:[email protected] Contents 1 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 IntroductiontoComputerSystems . . . . . . . . . . . . . . . . . . 9 2.1 Thecoreofacomputersystem . . . . . . . . . . . . . . . . . . 10 2.1.1 Centralprocessorunit—CPU. . . . . . . . . . . . . . . . . . . 10 2.1.2 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.1.3 Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.2 Othercomponentsofacomputersystem . . . . . . . . . . . . . . 11 2.2.1 Disks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2.2 Others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.3 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.5 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3 IntroductiontoOperatingSystems . . . . . . . . . . . . . . . . . 15 3.1 Historyofoperatingsystems. . . . . . . . . . . . . . . . . . . . 16 3.1.1 The1940s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.1.2 The1950s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.1.3 The1960s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.1.4 The1960sand1970s . . . . . . . . . . . . . . . . . . . . . . . 16 3.1.5 The1970s,1980s,and1990s . . . . . . . . . . . . . . . . . . . 17 3.2 Networking. . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.3 Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.4 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4 IntroductiontoUsingaComputerSystem. . . . . . . . . . . . . . 19 4.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 4.2 Editors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 4.3 Single-usersystems. . . . . . . . . . . . . . . . . . . . . . . . 20 4.4 Networkedsystems . . . . . . . . . . . . . . . . . . . . . . . . 20 4.5 Multiusersystems. . . . . . . . . . . . . . . . . . . . . . . . . 21 4.6 Otherusefulthingstoknow . . . . . . . . . . . . . . . . . . . . 21 viii Contents 4.7 CommonmethodsofusingcomputersystemstodevelopFortran programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 4.8 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 5 IntroductiontoProblemSolving . . . . . . . . . . . . . . . . . . 25 5.1 Naturallanguage . . . . . . . . . . . . . . . . . . . . . . . . . 26 5.2 Artificiallanguage . . . . . . . . . . . . . . . . . . . . . . . . 27 5.2.1 Notations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5.3 Resumé . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 5.4.1 Top-down . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 5.4.2 Bottom-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 5.4.3 Stepwiserefinement. . . . . . . . . . . . . . . . . . . . . . . . 29 5.4.4 Modularprogramming . . . . . . . . . . . . . . . . . . . . . . 29 5.4.5 Objectorientedprogramming . . . . . . . . . . . . . . . . . . . 29 5.5 Systemsanalysisanddesign. . . . . . . . . . . . . . . . . . . . 30 5.5.1 Problemdefinition . . . . . . . . . . . . . . . . . . . . . . . . 30 5.5.2 Feasibilitystudyandfactfinding . . . . . . . . . . . . . . . . . 30 5.5.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.5.4 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.5.5 Detaileddesign. . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.5.6 Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.5.7 Evaluationandtesting. . . . . . . . . . . . . . . . . . . . . . . 31 5.5.8 Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5.6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5.7 Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 5.8 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 6 IntroductiontoProgrammingLanguages . . . . . . . . . . . . . . 35 6.1 Someearlytheoreticalwork. . . . . . . . . . . . . . . . . . . . 36 6.2 Whatisaprogramminglanguage?. . . . . . . . . . . . . . . . . 36 6.3 Programlanguagedevelopmentandengineering . . . . . . . . . . 36 6.4 Theearlydays . . . . . . . . . . . . . . . . . . . . . . . . . . 36 6.4.1 Fortran— TheEarlyDays . . . . . . . . . . . . . . . . . . . . 37 6.4.2 Fortran77 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 6.4.3 Cobol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 6.4.4 Algol. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 6.5 Chomskyandprogramlanguagedevelopment . . . . . . . . . . . 39 6.6 Lisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 6.7 Snobol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 6.8 Second-generationlanguages . . . . . . . . . . . . . . . . . . . 40 6.8.1 PL/1andAlgol68 . . . . . . . . . . . . . . . . . . . . . . . . 40 6.8.2 Simula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 6.8.3 Pascal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Contents ix 6.8.4 APL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.8.5 Basic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.8.6 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.9 Someotherstrandsinlanguagedevelopment. . . . . . . . . . . . 42 6.9.1 Abstraction,stepwiserefinementandmodules . . . . . . . . . . . 42 6.9.2 Structuredprogramming. . . . . . . . . . . . . . . . . . . . . . 42 6.9.3 Standardisation . . . . . . . . . . . . . . . . . . . . . . . . . . 42 6.10 Ada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6.11 Modula. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6.12 Modula2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6.13 Otherlanguagedevelopments . . . . . . . . . . . . . . . . . . . 44 6.13.1 Logo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6.13.2 Postscript,TeXandLaTeX . . . . . . . . . . . . . . . . . . . . 45 6.13.3 Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 6.13.4 SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 6.13.5 ICON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 6.14 Objectorientatedprogramming—OOP . . . . . . . . . . . . . . 46 6.14.1 OberonandOberon2. . . . . . . . . . . . . . . . . . . . . . . 46 6.14.2 Smalltalk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.14.3 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 6.14.4 Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 6.14.5 VisualBasic . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.14.6 C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.15 Fortran90 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.16 Fortran1995 . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.17 ISOtechnicalreportsTR15580andTR15581 . . . . . . . . . . . 52 6.18 Fortran2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.19 DTR19767enhancedmodulefacilities. . . . . . . . . . . . . . . 53 6.20 Internetresources. . . . . . . . . . . . . . . . . . . . . . . . . 54 6.20.1 Standardsinformation. . . . . . . . . . . . . . . . . . . . . . . 54 6.20.2 Fortrandiscussionlists . . . . . . . . . . . . . . . . . . . . . . 55 6.20.3 Othersources. . . . . . . . . . . . . . . . . . . . . . . . . . . 55 6.21 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.22 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 7 IntroductiontoProgramming. . . . . . . . . . . . . . . . . . . . 63 7.1 Languagestrengthsandweaknesses . . . . . . . . . . . . . . . . 64 7.2 Elementsofaprogramminglanguage . . . . . . . . . . . . . . . 64 7.2.1 Datadescriptionstatements . . . . . . . . . . . . . . . . . . . . 65 7.2.2 Controlstructures. . . . . . . . . . . . . . . . . . . . . . . . . 65 7.2.3 Data-processingstatements . . . . . . . . . . . . . . . . . . . . 65 7.2.4 Inputandoutput(I/O)statements . . . . . . . . . . . . . . . . . 65 7.3 Variables—name,typeandvalue. . . . . . . . . . . . . . . . . 68 x Contents 7.4 Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 7.5 SomemoreFortranrules . . . . . . . . . . . . . . . . . . . . . 71 7.6 Fortrancharacterset . . . . . . . . . . . . . . . . . . . . . . . 72 7.7 Goodprogrammingguidelines. . . . . . . . . . . . . . . . . . . 73 7.8 Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 7.9 Programdevelopment. . . . . . . . . . . . . . . . . . . . . . . 74 7.10 Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 8 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 8.1 Roundingandtruncation . . . . . . . . . . . . . . . . . . . . . 81 8.2 TimetakenforlighttotravelfromtheSuntoEarth. . . . . . . . . 83 8.3 ThePARAMETERstatement . . . . . . . . . . . . . . . . . . . 84 8.4 Range,precisionandsizeofnumbers . . . . . . . . . . . . . . . 85 8.5 Healthwarning:optionalreading,beginnersareadvisedto leaveuntillater . . . . . . . . . . . . . . . . . . . . . . . . . . 88 8.5.1 SelectingdifferentINTEGERkindtypes. . . . . . . . . . . . . . 90 8.5.2 SelectingdifferentREALkindtypes. . . . . . . . . . . . . . . . 91 8.5.3 Specifyingkindtypesforliteralintegerandrealconstants. . . . . . 91 8.5.4 Positionalnumbersystems. . . . . . . . . . . . . . . . . . . . . 92 8.5.5 Bitdatatypeandrepresentationmodel. . . . . . . . . . . . . . . 92 8.5.6 Integerdatatypeandrepresentationmodel . . . . . . . . . . . . . 93 8.5.7 Realdatatypeandrepresentationmodel . . . . . . . . . . . . . . 93 8.5.8 IEEE754. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 8.5.9 Testingthenumericalrepresentationofdifferentkindtypesona system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 8.5.10 Binaryrepresentationofdifferentintegerkindtypenumbers . . . . 98 8.5.11 Binaryrepresentationofarealnumber . . . . . . . . . . . . . . 100 8.5.12 Summaryofhowtoselecttheappropriatekindtype. . . . . . . . 101 8.6 Variablestatus. . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.8 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 8.9 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . 105 9 Arrays1:SomeFundamentals . . . . . . . . . . . . . . . . . . . 107 9.1 Tablesofdata . . . . . . . . . . . . . . . . . . . . . . . . . . 108 9.1.1 Telephonedirectory . . . . . . . . . . . . . . . . . . . . . . . 108 9.1.2 Bookcatalogue . . . . . . . . . . . . . . . . . . . . . . . . . 108 9.1.3 Examinationmarksorresults. . . . . . . . . . . . . . . . . . . 109 9.1.4 Monthlyrainfall . . . . . . . . . . . . . . . . . . . . . . . . . 109 9.2 ArraysinFortran . . . . . . . . . . . . . . . . . . . . . . . . 110 9.3 TheDIMENSIONattribute. . . . . . . . . . . . . . . . . . . . 110 9.4 Anindex . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 9.5 Controlstructure. . . . . . . . . . . . . . . . . . . . . . . . . 111 9.6 Monthlyrainfall . . . . . . . . . . . . . . . . . . . . . . . . . 111 Contents xi 9.6.1 Example1:Rainfall . . . . . . . . . . . . . . . . . . . . . . . 112 9.7 People'sweights. . . . . . . . . . . . . . . . . . . . . . . . . 113 9.7.1 Example2:Settingarraysizewithaparameter . . . . . . . . . . 114 9.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 9.9 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 10 Arrays2:FurtherExamples . . . . . . . . . . . . . . . . . . . . 119 10.1 Varyingthearraysizeatruntime . . . . . . . . . . . . . . . . 120 10.2 Higher-dimensionarrays . . . . . . . . . . . . . . . . . . . . . 121 10.2.1 Amap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 10.2.2 Example3:Sensibletabularoutput . . . . . . . . . . . . . . . . 123 10.2.3 Example4:Averageofthreesetsofvalues . . . . . . . . . . . . 124 10.2.4 Example5:Bookingarrangementsinatheatreorcinema . . . . . 125 10.3 AdditionalformsoftheDIMENSIONattributeandDO loopstatement. . . . . . . . . . . . . . . . . . . . . . . . . . 126 10.3.1 Example6:Voltagefrom (cid:1)20to+20volts . . . . . . . . . . . . 126 10.3.2 Example7:Longitudefrom (cid:1)180to+180. . . . . . . . . . . . . 127 10.3.3 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 10.4 TheDOloopandstraightrepetition. . . . . . . . . . . . . . . . 127 10.4.1 Example8:Tableoftemperatures . . . . . . . . . . . . . . . . 127 10.4.2 Example9:Meansandstandarddeviations . . . . . . . . . . . . 128 10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 10.6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 11 WholeArrayandAdditionalArrayFeatures. . . . . . . . . . . . 133 11.1 Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.1 Rank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.2 Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.3 Extent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.4 Size. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.5 Shape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.6 Conformable . . . . . . . . . . . . . . . . . . . . . . . . . . 134 11.1.7 Arrayelementordering. . . . . . . . . . . . . . . . . . . . . . 134 11.2 Wholearraymanipulation . . . . . . . . . . . . . . . . . . . . 135 11.2.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 11.2.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 11.3 Arraysections. . . . . . . . . . . . . . . . . . . . . . . . . . 138 11.3.1 Rank1arrayexample . . . . . . . . . . . . . . . . . . . . . . 138 11.3.2 Rank2arrayexample . . . . . . . . . . . . . . . . . . . . . . 138 11.4 Arrayconstructors. . . . . . . . . . . . . . . . . . . . . . . . 140 11.4.1 Rank1arrayexample—explicitvalues . . . . . . . . . . . . . 140 11.4.1.1 Rank1arrayexampleandimpliedDOloop. . . . . . . . . . . . 141 11.4.1.2 Rank1arrayexampleandtheDOT_PRODUCTintrinsic . . . . . 141 11.4.2 Rank1examplewithstepsizeof2inimpliedDOloop . . . . . . 143