Difference between revisions 2551320 and 2554031 on enwikibooks

{{split}}
[[File:CCBYSA yellow.png]]

=Preface=
The goal of this Fortran tutorial is to give a quick introduction to the most common features of the Fortran 77 programming language. A companion tutorial introduces the key enhancements found in Fortran 90. It is not a complete reference! Many details have been omitted. The presentation focuses on scientific computations, mainly linear algebra. The outline of this tutorial was inspired by the book "Handbook for Matrix Computations" by T.F. Coleman a(contracted; show full)

==Example==

You are given a data file with xyz coordinates for a bunch of points. The number of points is given on the first line. The file name of the data file is points.dat. The format for each coordinate is known to be F10.4 (We'll learn about FORMAT statements in a later lesson). Here is a short program that reads the data into 3 arrays x,y,z:
<source lang="fortran">
       program inpdat

  c
  c  This program reads n points from a data file and stores them in 
  c  3 arrays x, y, z.
  c
       integer nmax, u
       parameter (nmax=1000, u=20)
       real x(nmax), y(nmax), z(nmax)
 
  c  Open the data file
       open (u, FILE='points.dat', STATUS='OLD')
 
  c  Read the number of points
       read(u,*) n
       if (n.GT.nmax) then
          write(*,*) 'Error: n = ', n, 'is larger than nmax =', nmax
          goto 9999
       endif
 
  c  Loop over the data points
       do 10 i= 1, n
          read(u,100) x(i), y(i), z(i)
    10 enddocontinue
   100 format (3(F10.4))
 
  c  Close the file
       close (u)
 
  c  Now we should process the data somehow...
  c  (missing part)
 
  9999 stop
       end</source>

=Simple I/O=
An important part of any computer program is to handle input and output. In our examples so far, we have already used the two most common Fortran constructs for this: read and write. Fortran I/O can be quite complicated, so we will only describe some simpler cases in this tutorial.

(contracted; show full)96. It has been modified by Sarah T. Whitlock and Paul H. Hargrove for use in the Fortran courses which have been offered under different course numbers each subsequent year. The original source of the material is here http://www.stanford.edu/class/me200c/tutorial_77/ Stanford university has re-released the material under a creative commons 3.0 attribution license. The tutorial was transferred to mediawiki format by Houraa Daher.

{{Subjects|Fortran programming language}}
{{alphabetical|F}}
{{status|100%}}