PROGRAM ARRAY3 c c What if I don't know how much array space I will need when I first c write my Fortran Program c c John Mahaffy 2/3/95 c IMPLICIT NONE INTEGER I,J,IERR,N,NDAT c c Declare A, B, and C to be arrays to be ALLOCATABLE c REAL, ALLOCATABLE :: A(:),B(:),C(:) REAL CSUM,CMAX,CMIN,AVERAGE,DUMMY c c Another way to decide on how much space you need is provide directly c in the input c OPEN(11,FILE='array3b.in',ERR=400) READ(11,*,END=400) NDAT c c ALLOCATE (A(1:NDAT),B(NDAT),C(NDAT),STAT=IERR) IF (IERR.NE.0) then PRINT *,'Problems allocating A, B, and C in Main Program' stop ELSE PRINT *, ' A, B, and C allocated' 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