c carray1.f c c
c

      PROGRAM ARRAY1
c
c
c    Simple exercises in using arrays for data analysis using Fortran 77
c
c    John Mahaffy   12/27/94
c
      IMPLICIT NONE
c
c    Declare A, B, and C to be arrays each containing 10 elements
c
      REAL A(10),B(10),C(10)
      INTEGER I
c
c    A, B, and C could have been declared arrays in the REAL statement
c    below  as "      REAL CSUM,CMAX,CMIN,AVERAGE,A(10),B(10),C(10)"
c
      REAL CSUM,CMAX,CMIN,AVERAGE
c
c    I've introduced the DATA statement below as a way of initializing
c    A and B.  Note that the values are loaded into A and B at Compile
c    time, and involve no computational work when the program is executed.
c
      DATA  A/1.,2.,3.,4.,5.,6.,7.,8.,9.,10./,B/3*1.,4*2.,3*3./
c
c    Now we set up a loop to set each element of C to be the sum of the
c    corresponding elements in A and B.  At the same time we will be summing
c    the elements in C to get an average value, and obtaining the maximum
c    and minimum values of all elements in C.
c
      CSUM=0.
      CMAX=-1.E38
      CMIN=1.E38
      DO 100 I=1,10
         C(I)=A(I)+B(I)
c     
         CMAX=MAX(C(I),CMAX)
c     
c     
         CMIN=MIN(C(I),CMIN)
c     
 100     CSUM=CSUM+C(I)
C
      AVERAGE=CSUM/10
C
c    The next write statement illustrates use of * for the unit number.
c    It represents the default unit, which is the terminal.
c
      WRITE(*,*) ' RESULTS FOR FULL C ARRAY'
c
c    The format associated with the following write spreads the output
c    of the three values (AVERAGE, CMIN, and CMAX) over 3 output lines.
c
      WRITE(6,2000)AVERAGE,CMIN,CMAX
c
 2000 FORMAT(' AVERAGE OF ALL ELEMENTS IN C = ', F8.3,/,
     &       ' MINIMUM OF ALL ELEMENTS IN C = ', F8.3,/,
     &       ' MAXIMUM OF ALL ELEMENTS IN C = ', F8.3)
c
c
c   Look carefully at the results of the following formatted write.  After
c   the first 8 elements of C are printed on one line, the end of the format
c   specification is reached, so a new line is started.  Format information
c   for the remaining 2 elements is obtained by looping back to the last left
c   parenthesis and in this case reusing the 8E10.2 format.  Fortran is
c   undisturbed by the fact that you run out of elements in C before you use
c   all of the 8E10.2.  It just stops writing, and moves on to the next
c   statement in the program.
c
      WRITE(6,2001) C
 2001 FORMAT(' C = ',/,(8E10.2))
C
C    Often you do not want to work with all elements of an array.  This is
c    simple for our case.  The bounds on "I" are simply altered in the DO
c    statement.
c
      CSUM=0.
      CMAX=-1.E38
      CMIN=1.E38
      C(1)=0.
      C(10)=0.
      DO 200 I=2,9
         C(I)=A(I)+B(I)
         CMAX=MAX(C(I),CMAX)
         CMIN=MIN(C(I),CMIN)
 200     CSUM=CSUM+C(I)
c    
      AVERAGE=CSUM/8
c
c    Below is another example of imbedding a format in a WRITE statement.
c    Note the effect of the double slashes (//).  Also note the doubled
c    single quotes surrounding the character string "RESULTS FOR EL...".
c    This practice is necessary when using a quoted character string within
c    another quoted string.
c
      WRITE(*,'(//,'' RESULTS FOR ELEMENTS 2 THROUGH 9 OF C'')')
c
      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)
c
c  The write below is an example of the use of what is called an implied
c  do loop.  In this case it is equivalent to using:
c   WRITE(6,2003)C(1),C(2),C(3),C(4),C(5),C(6),C(7),C(8),C(9)
c  I've included a "1P" here, but didn't the first time we printed C.  Note
c  the difference.
c
      WRITE(6,2003) (C(I),I=2,9)
 2003 FORMAT(' C = ',/,(1P,8E10.2))
c
c   Very frequently it is necessary to take special action depending on the
c   value of an array elements.  No real surprises here, but take a look at
c   how this is handled in "array2.f".
c   To see what Fortran does if you replace "LOG(A(I))" with "LOG(I)".
c   For intrinsic functions it warns you if you mess up argument types.
c
      A(4)=-1.0
      DO 300 I=1,10
         IF(A(I).GT.1.0) THEN
                C(I)=LOG(A(I))
         ELSE
            C(I)=0.
         ENDIF
  300    CONTINUE
c
c   Another way to get a blank line is given below.
c
      PRINT *
      WRITE(6,*) ' RESULTS OF LOG(A)'
      WRITE(6,2004) C
 2004 FORMAT(' C = ',/,(1P,8E10.2))
      STOP
c    
c
c c