在 Fortran 中不使用 maxval 的数组最大值

Maximum of an array without using maxval in Fortran

我想在代码的最后两部分(标记为 **)中不使用 maxval 来找到数组 T 的最大值。不幸的是,它不起作用。它向我展示了仅使用 if 条件验证的所有数字,而没有找到它的最大值。 if 条件只取第一个数字并与其他数字进行比较,如果经过验证,则显示所有我找不到我的错误。

Program exo2

Implicit None 
Real, Dimension (:,:), Allocatable   :: D    
integer                              :: i,Z,A,B,ok
Real                                 :: no_esc_max=1 , no_esc_min=1
Real, Dimension(:)   , Allocatable   :: T                            

print*, "entrez le nombre etudies"
read*, A
print*, "entrez le nombre de mesures pour chaque escargot"
read*, B

Allocate(D(A,B), STAT=ok)
Allocate(T(A), STAT=ok)

if (ok/=0) then
    print* , "allocation a echoue"
    Stop
end if

Do i=1,A
   Do z=1,B
    Print*, "Escargot",i
    Print*,"entrez la vitesse lors de la mesure",z
    Read*, D(i,z)
   end do
end do

Do i=1,A
   print*, D(i,:)                                
end do                          

Do i=1,A
   Do z=1,B
   T(i)=Sum(D(i,:))/z
   end do
   print*, "moyenne escargot", i , T(i)
end do


! (**) This block seems to have the problem
no_esc_max=T(1)
do i=2,A
   if (no_esc_max<T(i)) then
      T(i)=no_esc_max
   end if
   print* , "escargot",i, "est le plus rapide"
end do

no_esc_min=T(1)
do i=2,A
  if (no_esc_min>T(i)) then 
     T(i)=no_esc_min
  end if 
  print*, "escargot", i, "est le moins rapide"
end do
! (**) End of the block

Deallocate (D)
Deallocate (T)

End Program exo2

要打印正确的 i,您应该跟踪引用您的 max/min 值的索引。此外,将您的 print*,'..' 命令放在 do-loop 之外。否则,好像你只是打印所有这些。