günther lamprecht (auth.) introduction to fortran 77 1986

Download Günther Lamprecht (Auth.) Introduction to FORTRAN 77 1986

Post on 05-Dec-2015

214 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

Günther Lamprecht (Auth.) Introduction to FORTRAN 77 1986

TRANSCRIPT

  • G. Lamprecht

    Introduction to FORTRAN 77

  • G. Lamprecht Introduction to SIMULA 67

    W. Werum and H. Windauer Introduction to PEARL

  • GUnther Lamprecht

    Introduction to FORTRAN 77

    Friedr. Vieweg & Sohn Braunschweig / Wiesbaden

  • CIP-Kurztitelaufnahme der Deutschen Bibliothek

    Lamprecht Giinther: Introduction to FORTRAN 77 / Giinther Lamprecht. -Braunschweig; Wiesbaden: Vieweg, 1986. ISBN-13: 978-3-528-03360-6 e-ISBN-13: 978-3-322-89421-2 DOl: 10.1007/978-3-322-89421-2

    1986

    All rights reserved Friedr. Vieweg &; Sohn Verlagsgesellschaft mbH, Braunschweig 1986

    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photo-copying, recording or otherwise, without prior permission of the copyright holder.

    Produced by Lengericher Handelsdruckerei, Lengerich

  • Preface

    Fortran is one of the most common programming languages in the field of science and Fortran compilers are available for almost all computers and are being increasingly used by personal computers. The initial standard version of Fortran IV has been extended enormously due to the large range of computers available and the numerous requirements demanded of them. As a result of this there are at present a vast number of Fortran "language dialects" which impairs the exchange of programs. Fortran 77 is a new language standard which includes many of the previous extensions. Furthermore, Fortran 77 subset has been developed which takes into consideration the limited possibilities of small computers. This book is intended, by means of examples, to introduce the reader to the programming language Fortran 77, whereby the liminations of Fortran 77 subset will be taken into consideration. The examples and exercises have been chosen so that the solutions can be arrived at with a minimum of specialized knowledge. The reader will thus, with the ex-ception of a few statements, be able to become acquainted with all possibilities of Fortran 77. I would like to take this opportunity to thank G. Parker for his translation of this book from German into English, Dr. S. Bartnitzke for critical reading of the manuscript and Mrs. U. Kleinschmidt for typing the text.

    Bremen, February 1986 GUnther Lamprecht

  • Contents

    Introduction ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    1 A Simple Example ........................................ 4 2 The Formation of Arithmetic Expressions ........................ 10 3 Loop Control, Logical Items ................................. 16 4 Polynomials; Vectors, Matrices .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 27 5 Output on the Printer or the Terminal ........................... 38 6 The Input of Data ........................................ 52 7 Internal Representation of Characters,

    Initialization of Variables ................................... 60 8 Subprogram Techniques: Function Subprograms .................... 71 9 Subprogram Techniques: Subroutines; Vectors and

    Matrices as Parameters ..................................... 80 10 Parameter Transfer by Means of the COMMON Statement .. . . . . . . . . . . .. 90 11 The Data Type COMPLEX .................................. 96 12 File Access ............................................. 98

    Solutions to the Examples and the Exercises ...... . . . . . . . . . . . . . . . . . . .. 110

    Appendix A: Internal Representation of Numbers ..................... 133 Appendix B: DOloop........................................ 137 Appendix C: Supplied Function Subprograms .. . . . . . . . . . . . . . . . . . . . . .. 140 Appendix D: Summary of all Statements ........................... 143 Appendix E: ASCII Character Code .............................. 145

    Index ................................................... 146

  • Inttoduction The following illustration shows the different stages that have to be processed from the initial formulation of a problem up to its final solution:

    If, out of the different means available, one decides to use a computer then the solution has to be written in a precise manner. Further-more, all possible special cases have to be recognized and taken in-to account. Only then can the solu-tion path, for instance, in the language Fortran, be programmed.

    After the program has been prepared in detail (at the office desk) the sequence of statements can then be input to the computer by, for example, a terminal.

    The computer accepts the program which is formulated in a so-called problem-oriented language. It then translates the individual statements with the aid of a special program, the so-called compiler, into a lan-

    guage which can be directly understood by the machine ("machine-oriented language"). It is at this stage that the computer recognized all offences against the rules of the problem-oriented language and informs the programmer.

    After the program has been tested, i.e. all formal errors have been removed and the program supplies the calculated values, the results have to be subject to a critical assessment. This assessment determines whether

    - the solution should be written in another manner, - the means used should be changed

    or - the problem should be formulated in another way.

    It is only after obtaining the desired results that the problem under consideration can be thought of as being solved.

  • 2

    The particular configuration of the computer being used has no significant effect on the formal structure of the programming language Fortran. However, for general information it is advis-able to know something about the arrangement of the computer system. The following diagram is designed to illustrate this.

    working storage

    printer

    central r.;;=~;1processing~--~~ ~ ________ ~. unit ~ ______ ~

    ,

    ~------~: ,

    ~ ________ Jl-:.;:::;.=1 __________ --1~: : L _______ ~ The central processing unit operates all devices and can be thought of as being the heart of the computer. The programs (and if necessary corresponding data) can be input to the com-puter by means of the keyboard. At the same time programs can be displayed on a terminal for checking purposes. In general, the sequence of the statements are stored under a specific name on either a floppy disk, a magnetic tape or a magnetic disk and are thus available for use at a later stage. The printer can be used for the output of a list of programming statements, for documentation purposes, or to list the results of a program which has been executed.

    The program remains in the working storage for the total duration of the execution and, furthermore, all storage places required by the program ar~ reserved here.

    The above diagram applies, on the one hand, to large computer systems which are in a position to process a great number of user programs at the same time and, on the other hand, to small computers that are only used by one user ("personal computer").

    Fortran 77 is designed as a uniform programming language capable solving different problems independent of the particular computer being used. As there is a considerable difference be-tween the performance of large computer systems on the one hand,

  • *)

    3

    and small systems on the other hand, a modified language of the full language Fortran 77, namely Fortran 77 subset, has been developed.

    With respect to the interchangeability of programs it would certainly be desirable if all programs were kept in two groups according to the definition of the standards. Reality shows, however, that the computer manufacturers offer various extensions of the language Fortran 77 in order to exploit the different possibilities of their computers to a maximum. If

    and to what extent use is made of the language extension in a program, depends on whether the program is to be used on a different computer. In general, it is recommended to adhere to the language standard as much as possible, i.e. if possible not to exploit language extensions.

    Fortran 77 is capable of dealing with variables and constants, having different characteristics, which include

    - integer items - real items - complex items

    logical items - character items

    (INTEGER) (REAL and DOUBLE PRECISION) (COMPLEX) (LOGICAL) (CHARACTER)

    DOUBLE PRECISION and COMPLEX data types are not available in Fortran 77 subset.

    The individual basic elements can be connected to form larger units (e.g. arithmetic expressions, Boolean expressions, character expressions), their values can be obtained as an out-put (WRITE statement) or values can be transferred to the variables by means of assignment or input statements (READ) from external storage devices.

    Over and above this, a program can be structured using loops, subprograms and also blocks. The various language elements will be described using simple examples in the following chapters. In doing so the aim is not to provide a complete description of the language - this can be obtained in the handbooks -but rather to provide an understanding of the language.*) Nevertheless, apart from a few statements that are hardly ever used in practice, all Fortran 77 instructions have been described; cf. Appendix D.

  • 4

    1 A Simple EDmple In this chapter we will calculate the mean value m of two numbers a and b with the aid of a computer.

    m -a+b -2- a .. 1.4, b .. 2.1

    If the above problem is solved mentally, the value 1.75 is obtained. The problem is, therefore, not to determine the value of m, but how the problem can be written in Fortran, i.e. "programmed". The program will first of all be presented a