CSC 272 - Software II: Principles of Programming Languages

Dr. R. M. Siegfried

FORTRAN Program To Write A File of Integers

c
c     Declare the constant LenArray
c     Declare the array and the integer length
c     even though it is an integer by default
c
      parameter (LenArray = 20)
      integer array(LenArray), length
c
c     Call the procedure readfile 
      call readfile(array, length)
c
c     FOR i := 1 to Length DO Print Array[i]
      do 101 i = 1, length
        write(*, 501) i, array(i)
  501   format(' ', 'array[', i2, '] = ', i5)
  101 continue
      end
c
c
c     The procedure readfile
c
      subroutine  readfile(array, n)
c
c     We redeclare these because the subroutine has its 
c     own variables and constants
      parameter(LenArray = 20)
      integer array(LenArray), n
      character*30 filename
c
c     Get the file name and open for writing (its new)
c     because we don't wish to append - if it exists its an error
      write(*,*) 'Enter file name'
      read(*,601)filename
  601 format(a30)
      open (unit=2, file=filename, status = 'new')
      do 201 n = 1, LenArray
        write(*,*) 'Enter an integer'
        read(*,*) array(n)
c
c       if Array[n] = 0 get out of loop
c       it's legal to break out but not to break into a DO loop
c
        if (array(n).eq.0) go to 202
        write(2,602) array(n)
  602   format(' ', i5)
  201 continue
  202 continue
      n = n - 1
      close(unit = 2)
      return
      end

[Back to the FORTRAN Index]