!------------------------------------------------------------------------------
!
!  Program name:
!
!    munro
!
!  Purpose:
!
!    This program demonstrates the use of intrinsic functions to generate 
!    pseudo random real number between 0 and 4404, and then to select 
!    all values greater than 3000 and find the number of them, the maximum, 
!    the minimum and the average. 
!    
!------------------------------------------------------------------------------

PROGRAM munro

  IMPLICIT NONE

  REAL, DIMENSION (5000) :: hills
  LOGICAL, DIMENSION (5000) :: bighills
  INTEGER, DIMENSION (1) :: iseed
  INTEGER :: nmunros, status

  REAL, ALLOCATABLE, DIMENSION (:) :: munros

  ! Generate pseudo random numbers.          

  iseed = 1
  CALL RANDOM_SEED (PUT=iseed)
  CALL RANDOM_NUMBER (hills(:))
  hills(:) = 4404.0*hills(:)

  ! Find number of Munros and allocate array munro:

  bighills = hills(:) > 3000.0                  ! Alternative to WHERE
  nmunros = COUNT(bighills)                     ! Number of Munros

  ALLOCATE (munros(nmunros), STAT = status)

  IF (status > 0) STOP 'Fail to allocate memory for allocatable array'

  ! Pack Munros into array munros:

  munros(:) = PACK(hills(:),bighills,munros(:))

  ! Print number of, highest, lowest and average heights of Munros:

  WRITE (*,'(''Number of Munros : '',I6)') nmunros
  WRITE (*,'(''Highest Munro    : '',F8.3)') MAXVAL(munros(:))
  WRITE (*,'(''Lowest Munro     : '',F8.3)') MINVAL(munros(:))
  WRITE (*,'(''Average Munro    : '',F8.3)') SUM(munros(:))/nmunros

  DEALLOCATE (munros)

END PROGRAM munro  







