SUBROUTINE sortc (array, n)
!
!  Purpose:
!    To sort character array "array" into ascending order using
!    a selection sort.  This version of the subroutine sorts
!    according to the ASCII collating sequence.  It works for
!    character arrays with any number of elements, with array
!    elements of any length, and on processors regardless of
!    character set.
!
!  Record of revisions:
!      Date       Programmer          Description of change
!      ====       ==========          =====================
!    11/25/06    S. J. Chapman        Original code
! 1. 11/25/06    S. J. Chapman        Modified to work with lexical
!                                     fns and arbitrary element
!                                     lengths
IMPLICIT NONE

! Data dictionary: declare calling parameter types & definitions
INTEGER, INTENT(IN) :: n                ! Number of values
CHARACTER(len=*), DIMENSION(n), INTENT(INOUT) :: array
                                        ! Array to be sorted

! Data dictionary: declare local variable types & definitions
INTEGER :: i                   ! Loop index
INTEGER :: iptr                ! Pointer to smallest value
INTEGER :: j                   ! Loop index
CHARACTER(len=len(array)) :: temp  ! Temp variable for swaps

! Sort the array
outer: DO i = 1, n-1

   ! Find the minimum value in array(i) through array(n)
   iptr = i
   inner: DO j = i+1, n
      minval: IF ( LLT(array(j),array(iptr)) ) THEN
         iptr = j
      END IF minval
   END DO inner

   ! iptr now points to the minimum value, so swap array(iptr)
   ! with array(i) if i /= iptr.
   swap: IF ( i /= iptr ) THEN
      temp        = array(i)
      array(i)    = array(iptr)
      array(iptr) = temp
   END IF swap

END DO outer

END SUBROUTINE sortc


program my_first_thread

    implicit none

    integer, parameter :: n = 10
    integer :: i

    !$OMP PARALLEL DO PRIVATE(i)
    DO i=1,n
        PRINT *, "I am counter i = ", i
    ENDDO
    !$OMP END PARALLEL DO

end program my_first_thread


program bad_access
    implicit none
    integer, parameter :: default_n = 5
    integer :: i, n, sum
    integer, allocatable, dimension(:) :: a

    if (command_argument_count() == 1) then
        block
            character(len=1024) :: buffer
            call get_command_argument(1, buffer)
            read (buffer, '(I10)') n
        end block
    else
        n = default_n
    end if

    allocate(a(n))
    do i = 1, n
        a(i) = i
    end do

    sum = 0
    do i = n, 0, -1
        sum = sum + a(i)
    end do
    print '(A, I10)', sum

    deallocate(a)

end program bad_access