!------------------------------------------------------------------------------
!
!  Program name:
!
!    array_section
!
!  Purpose:
!
!    This program demonstrates the use of array sections.
!
!    It firstly reads in the matrix from the file matrix.dat, which is
!    generated by matrix.f90. Then it writes out:
!     	
!	(a) the first row
! 	(b) the fifth column
!       (c) every second element of each row and column, columnwise
!           ( a(1,1), a(3,1),...)
!       (d) every second element of each row and column, rowwise 
!           ( a1,1), a(1,3),...)
!       (e) the 3 non-overlapping 3x3 sub-matrices in columns 4 to 6
!
!------------------------------------------------------------------------------

PROGRAM array_section

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

  ! read matrix ia from the file matrix.dat generated by matrix.f90
  OPEN (UNIT = 1, FILE = 'matrix.dat')
  READ (1, *) ia
  CLOSE (1)

  WRITE(*,'(//"First row:"/)')
  WRITE(*,*) ia(1,:)

  WRITE(*,'(//"Fifth column:"/)')
  WRITE(*,*) ia(:,5)

  WRITE(*,'(//"Every second element in each row and column, columnwise:"/)')
  WRITE(*,'(5I5)') ia(::2,::2)

  WRITE(*,'(//"Every second element in each row and column, rowwise:"/)')
  WRITE(*,'(5I5)') (ia(i,::2), i = 1, n, 2)

  WRITE(*,'(//"3 non-overlapping 3x3 arrays in columns 4 to 6:"/)')
  DO i = 1, 9, 3
    WRITE(*,'(3I5)') (ia(j, 4:6), j = i, i+2)
    WRITE(*, *)
  END DO

END PROGRAM array_section



