Advertisement
Guest User

Untitled

a guest
May 9th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. contains
  2.    subroutine sort_indexes(A, indexes)
  3.       integer, intent(inout)  :: A(:), indexes(:)
  4.       integer                 :: i, loc, N, sub
  5.  
  6.       N = size(A)
  7.  
  8.       do i = 1, N
  9.          !Сортировка значений в первой строке массива по возрастанию
  10.          loc      = MaxLoc(A(1:N-i+1), dim = 1)
  11.          sub      = A(N-i+1)
  12.          A(N-i+1) = A(loc)
  13.          A(loc)   = sub
  14.    
  15.          !Сортировка индексов строк массива по возрастанию элементов первой строки
  16.          sub            = indexes(N-i+1)
  17.          indexes(N-i+1) = indexes(loc)
  18.          indexes(loc)   = sub
  19.       end do
  20.  
  21.    end subroutine
  22.  
  23.  
  24. end module
  25.  
  26. PROGRAM EX7_29
  27.    use environment
  28.    use functions
  29.    implicit none
  30.  
  31.    character(*), parameter    :: input = "../data/input", output = "../data/output"
  32.    integer(R_)                :: in = 1, out = 2
  33.    
  34.    integer(R_), allocatable   :: A(:,:), indexes(:)
  35.    integer(R_)                :: N, i, j
  36.    character(:), allocatable  :: fmt
  37.  
  38.    open(newunit = in, file = input)
  39.       read(in,*) N
  40.       allocate(A(N,N))
  41.       read(in,*) (A(:,j), j = 1, N)
  42.    close(in)
  43.  
  44.    allocate(indexes(N))
  45.    indexes = [(i, i = 1, N)]
  46.    
  47.    write(*,*) indexes
  48.    call sort_indexes(A(:,1), indexes)
  49.    write(*,*)
  50.    write(*,*) indexes
  51.    write(*,"(4i4)") (A(:, indexes(i)), i = 1, N)
  52.    fmt = "("//N//"i4)"
  53.    open(newunit = out, file = output)
  54.      write(out, fmt) (A(:, indexes(i)), i = 1, N)
  55.    close(out)
  56. end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement