2013-04-03 94 views
0

我有一个简单的Fortran代码,并且出现一个错误,我找不到解决方案。有谁知道如何解决这一问题?Gfortran做循环if语句错误

subroutine sort(A,A_done,N,P) 
! Sort a real array by algebraically increasing value and return the permutation that 
! rearranges the array 
    implicit none 

    Integer N, TEMP1, K, L, P(N), TEMP2 
    real(8), dimension(:) :: A_done 
    real(8), dimension(:) :: A 

    DO K=1, N-1 
    DO L=K+1, N 
     if A(K)>A(L) 
       TEMP1=A(K) 
      TEMP2=P(K) 
      A(K)=A(L) 
       P(K)=P(L) 
     A(L)=TEMP1 
      P(L)=TEMP2 
    end if 

    END DO 
    END DO 
    A_done=A 
    RETURN 
    END 

gfortran -Wall -Werror -fbounds检查-w -L -lm -o模拟readinput.for noutfile.for mean.for covariance.for correlation.for rperm.for simmain.for sort.for 在文件sort.for:13

 if A(K)>A(L) 
    1 

错误:在(1) 不可分类的语句在文件sort.for:20

end if 
     1 

错误:期待END在(1)DO语句化妆:* [模拟]错误1

感谢您的帮助

+0

对不起,关于这一点,我想到另一种语言,那里你知道,'如果其他'模式。 – fedvasu 2013-04-03 21:33:42

+0

那么,我假设你看了一个语言手册,了解if块的正确语法。它告诉你什么? – eriktous 2013-04-03 22:32:52

回答

1

你已经忘记了一对括号和 “再”:

if A(K)>A(L)必须键入if (A(K)>A(L)) then

除此之外,您的代码有多个问题:

  1. TEMP1=A(K)和类似的表达式,你将一个实数(8)值传递给一个整数变量
  2. 我不明白P变量的作用(你能描述一下吗?),但是你也在那里混合了实数(8)和整数。
  3. 您必须在子程序中指定数组的维数。 (我认为有一种方法不能通过使用模块来实现)
  4. 请记住,您更改A然后将其复制到A_done。为什么要这样做?你失去了原有的价值并消耗更多的记忆。

我做了一些更正,你可能想保留,你可以做更多。此代码编译并运行良好。

Program test 

    implicit none 
    integer N 
    real(8), allocatable :: A(:), A_done(:), P(:) 

    N=5 
    Allocate (A(N), A_done(N), P(N)) 

    A=(/5,3,6,1,9/) 
    call sort(A, A_done, N, P) 

    Write (*,*) A_done 

End 

subroutine sort(A,A_done,N,P) 
! Sort a real array by algebraically increasing value and return the permutation that 
! rearranges the array 
    implicit none 

    Integer N, K, L 
    real(8), dimension(N) :: A, A_done, P 
    real(8) :: TEMP1, TEMP2 

    DO K=1, N-1 
      DO L=K+1, N 
        if (A(K)>A(L)) then 
          TEMP1=A(K) 
          TEMP2=P(K) 

          A(K)=A(L) 
          P(K)=P(L) 

          A(L)=TEMP1 
          P(L)=TEMP2 
        end if 
      END DO 
    END DO 
    A_done=A 
    RETURN 
END