Untitled

mail@pastecode.io avatar
unknown
fortran
a year ago
1.0 kB
3
Indexable
program lab72
integer, allocatable:: arr(:)
integer:: seed, nl,nr,key,r,i, z,k,l, left, right, mid
integer, allocatable:: karr(:)

call srand(seed)
nl=1
nr=100
allocate(arr(nl:nr))
do i= nl,nr
    r=aint(rand()*10)
    arr(i)=r
end do

do k=1,nr-1
  do l=k+1,nr
     if (arr(k)>arr(l)) then
          z=arr(k)
          arr(k)=arr(l)
          arr(l)=z
       end if
     end do
end do

left=1
right= nr-1
mid=0
print*,'введите ключ от 1 до 10'
read(*,*) key
allocate(karr(nl:nr))
do i=nl,nr
 do while (left <= right)
     mid = ((right-left)/2)+left
     if (key== arr(mid)) then
          karr(i)=mid
     else if (key < arr(mid)) then
            right=mid -1
     else
          left = mid +1
      end if
  end do
end do


write(*, 10) (arr(i), i=nl,nr)
10 format(10(2x,i3))
deallocate(arr)
print*,'индексы ключевых элементов'
do i=nl,nr
    if (karr(i)/=0) then
         write(*,*) karr(i)
      end if
end do
deallocate(karr)

end program lab72