!------------------------------------------------------------------------------
!
!  Program name:
!
!    spread
!
!  Purpose:
!
!    This program shows how to use the array intrinsic function SPREAD to 
!    assign an array with values 1.0/REAL(i+j+1), where i is the row number 
!    and j is the column number. It also shows how to use array constructor 
!    to get the same result.
!
!------------------------------------------------------------------------------

PROGRAM spread2

  IMPLICIT NONE
  INTEGER, PARAMETER :: n = 5
  INTEGER :: i, j
  REAL, DIMENSION (n,n) :: ra

  WRITE(*, '("Using SPREAD:"//)')
  ra = 1.0 / REAL( SPREAD( (/ (i, i = 1, n) /), DIM = 1, NCOPIES = n)  &
           + SPREAD( (/ (j, j = 1, n) /), DIM = 2, NCOPIES = n) + 1 )
  WRITE(*, '(5F8.2)') (ra(:, j), j = 1, n)

  ! Using Array Constructor:

  WRITE(*,'(//"Using Array Constructor:"//)')
  ra = RESHAPE ( (/ ((1.0/(i+j+1), i=1, n), j=1, n) /), SHAPE=(/ n, n /) )
  WRITE(*,'(5F8.2)') (ra(:, j), j = 1, n)


  ! Note the simpler method with FORALL statement in Fortran 95:
  !  ??? FORALL (i = 1, n, j = 1, n) ra(i, j) = 1.0/(i+j+1)

END PROGRAM spread2

