!------------------------------------------------------------------------------
!
!  Program name:
!
!    where_construct
!
!  Purpose:
!
!    This program demonstrates the use of WHERE construct.
!
!    It firstly reads in the matrix from the file matrix.dat, which is
!    generated by matrix.f90. Then it uses the WHERE construct
!    and the elemental function MOD to create an array containing all 
!    of the odd values and 0 elsewhere.
!
!------------------------------------------------------------------------------

PROGRAM where_construct

  IMPLICIT NONE
  INTEGER, PARAMETER :: n = 9
  INTEGER, DIMENSION (n,n) :: ia, ib
  INTEGER :: i

  ! read matrix ia from the file matrix.dat generated by matrix.f90
  WRITE(*,'(//"Read in array from the file matrix.dat:"/)')
  OPEN (UNIT = 1, FILE = 'matrix.dat')
  READ (1, *) ia
  CLOSE (1)

  ! Write out whole array, rowwise:
  WRITE(*,'(9i5)') (ia(i,:), i = 1, n)

  ! Array of odd values and zeroes using WHERE:
  WRITE(*,'(//"Array of odd values and zeroes using WHERE:"/)')

  WHERE ( MOD(ia,2) == 1)
    ib = ia
  ELSEWHERE
    ib = 0
  END WHERE

  ! Write out new array, rowwise:
  WRITE(*,'(9i5)') (ib(i,:), i = 1, n)

END PROGRAM where_construct

