addToIntArray Subroutine

public subroutine addToIntArray(arr, ind, elem)

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), allocatable :: arr(:)
integer, intent(in) :: ind
integer, intent(in) :: elem

Contents

Source Code


Source Code

    subroutine addToIntArray(arr, ind, elem)
        integer, intent(inout), allocatable :: arr(:)
        integer, intent(in) :: ind, elem

        integer, allocatable :: tmp(:)
        integer :: nelems

        if(allocated(arr)) then
            nelems = size(arr)

            if(ind > nelems) then
                ! resize the array
                allocate(tmp(nelems))
                tmp = arr
                deallocate(arr)
                allocate(arr(ind), source=0)
                arr(1:nelems) = tmp(1:nelems)
            endif
        else
            allocate(arr(ind), source=0)
        endif

        arr(ind) = elem

    end subroutine addToIntArray