!
      PROGRAM P56
!
!     Program to process student grades and find top three
!
      IMPLICIT NONE
      INTEGER :: I,ID(500),Ass(500),Mid(500),Fin(500),Top_Index
      INTEGER :: J,NSTUD,N
      REAL :: Mark(500),Topmrk
      CHARACTER (LEN=25) :: Name(500)
!
!
      PRINT *, 'This is Program >> P56  - Class grades and top 3'
!
!     Tell program where data for  READ   is coming from
      OPEN(UNIT=5, FILE='P56.DAT')      ! UNIT=5 is the default input
!
      Print *,'ID       Name               Assign  Mid Exam  Total'
      Nstud = 0
      READ *,N
L1:   Do I=1,N
         Read *, Id(i),Name(i),Ass(i),mid(i),fin(i)
         IF(ID(i) == 9999999) go to 100
         Mark(i)=Ass(i)*.3 + Mid(i)*0.2 + Fin(i)*0.5 
         Nstud = Nstud + 1
         Print 4,ID(i),Name(i),Ass(i),Mid(i),Fin(i),Mark(i)
4        Format(' ',I7,'  ',A25,3I5,F7.1)
      End DO L1
      Print *,'Too much data - Check IDs or increase array size'
      STOP
!
100   Print *  ! A blank line
      Print *,'Top students are:-'
L2:   Do i=1,3
         Top_Index=1
         TopMrk = Mark(1)
L3:      Do J=2,Nstud
            If(Mark(j) > TopMrk) then
               Top_Index = j   ! Pointer to Top slot
               TopMrk = Mark(j)
            End if
         End Do L3
         Print 5,I,TopMrk,ID(Top_Index),Name(Top_Index)
5        Format(I3,F7.1,I9,'  ',A25)
         Mark(Top_Index) = 0          ! Top only Once !!
      End Do L2
      STOP
      END PROGRAM P56