Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

array3c.f

PROGRAM ARRAY3C
c
c    What if I don't know how much array space I will need when I first
c    write my Fortran Program.  If I don't want to bother with ALLOCATE,
c    or my compiler doesn't support it, I can use a Fortran 77 PARAMETER
c    statement to establish the available array space in one place.  If
c    I need more later, I just change the one parameter value and recompile.
c
c    John Mahaffy
c
      IMPLICIT NONE
      INTEGER I,J,IERR,N,NDAT,NMAX
      PARAMETER (NMAX=10)
c
c    Note: Once declared a PARAMETER, NMAX can never appear on the left
c          side of an equals sign.  Try setting "NMAX=3" somewhere below
c          the OPEN(11,... statement.  You can do Fortran arithmetic within
c          a PARAMETER statement as long as only constants and other
c          parameters are involved.  Try replacing the above PARAMETER with:
c          "      PARAMETER (N2=2, N4=4, NMAX=N2*N4+6)".  Try breaking this
c          new statement into 2 statements, one setting N2 and N4 and the other
c          setting NMAX.  In either case you will have to declare the type
c          of N2 and N4.
c
c    Dimension A, B, and C to NMAX
c
      REAL   A(NMAX),B(NMAX),C(NMAX)
      REAL CSUM,CMAX,CMIN,AVERAGE,DUMMY
c
c   Get the amount of data and see if there is enough space.
c
      OPEN(11,FILE='array3b.in',ERR=400)
      READ(11,*,END=400) NDAT
      IF(NDAT.GT.NMAX) then
         print *, 'Increase NMAX to at least:', NDAT, ' and recompile'
         stop
      ENDIF
c
c   Set values of A and B
c
      DO 30 I=1,NDAT
      READ(11,*) A(I), B(I)
  30  CONTINUE
c
c   Process as before
c
      C(1:NDAT)=A(1:NDAT)+B(1:NDAT)
      CSUM=SUM(C(1:NDAT))
      CMIN=MINVAL(C(1:NDAT))
      CMAX=MAXVAL(C(1:NDAT))
C
      AVERAGE=CSUM/NDAT
      WRITE(*,'(//,'' RESULTS FOR ELEMENTS 1 THROUGH '',I3,'' OF C'')')
     & NDAT
      WRITE(6,2002)AVERAGE,CMIN,CMAX
 2002 FORMAT(' AVERAGE OF SELECTED ELEMENTS IN C = ', F8.3,/,
     &       ' MINIMUM OF SELECTED ELEMENTS IN C = ', F8.3,/,
     &       ' MAXIMUM OF SELECTED ELEMENTS IN C = ', F8.3)
      WRITE(6,2003) C(1:NDAT)
 2003 FORMAT(' C = ',/,(1P,8E10.2))
c
      STOP
 400  PRINT *,' Empty or missing input file: array3b.in'
      STOP
      END
Skip to content