2012-11-08 66 views
2

我在這個程序的子程序中遇到了一個問題,它在程序的最後。我在子程序中得到了一個數組的劃分錯誤,它說「不可分類的語句」。希望有人能幫助!帶數組的FORTRAN子程序

PROGRAM subroutine1 
IMPLICIT NONE 

INTEGER:: err, ierr, counter, y ,i ,j 
!INTEGER, ALLOCATABLE:: gamenum(:) 
CHARACTER(30):: fname 
REAL, ALLOCATABLE:: AB(:), H(:), TB(:), BA (:), SP(:) 

100 Format (A) 
200 Format (I2) 
300 Format (F9.3) 

! 1.Open file 

WRITE(*,100)"Please enter a filename:" 
READ(*,*) fname 

OPEN (UNIT=10, FILE=fname, STATUS="OLD", ACTION="READ", IOSTAT=err) 
IF(err.NE.0) STOP "An error occured when opening the file." 

! 2.Count Lines 

READ(10,*) 

counter=0 
DO 
    READ(10,*,IOSTAT=ierr) 
    IF(ierr .NE. 0) EXIT 
    counter=counter+1 
END DO 

!WRITE(*,200) counter 

! 3. allocate array 

ALLOCATE(AB(counter)) 
ALLOCATE(H(counter)) 
ALLOCATE(TB(counter)) 
ALLOCATE(BA (counter)) 
ALLOCATE(SP(counter)) 

! 4. read in data 

REWIND(10) 

READ(10,*) 

DO i=1,counter 
    READ(10,*) AB(i), H(i), TB(i) 
END DO 

REWIND(10) 

! 5. Call subroutine 

    CALL arraycalc(counter,AB,H,TB,BA,SP) 

! 6. Write out 
WRITE(*,100)"Game AB  H  TB  BA  SP" 

DO i=1,counter 
    WRITE(*,200,ADVANCE="NO") i 
    WRITE(*,'(5F9.3)') AB(i), H(i), TB(i), BA(i), SP(i) 
END DO 

END PROGRAM subroutine1 

!HERE IS THE PART WHERE I'M HAVING TROUBLE 

SUBROUTINE arraycalc(counter,AB,H,TB,BA,SP) 
IMPLICIT NONE 

INTEGER, INTENT(IN)::counter 
INTEGER::i 
REAL,INTENT(INOUT)::AB,H,TB 
REAL,INTENT(INOUT):: BA,SP 

DO i=1,counter 
    BA(i)=H(i)/AB(i) 
END DO 

DO i=1,counter 
    SP(i)=TB(i)/AB(i) 
END DO 

END SUBROUTINE 

回答

2

子程序內部,ABHTBBASP不聲明爲數組,這樣的確給出了錯誤的說法是沒有意義的。它們是標量,所以不能被索引。

+0

DIMENSION(:)應該添加到它們在例程中的定義arraycalc – DaveP

+1

如果將過程放入模塊並「使用」該模塊,則將避免其他錯誤。不這樣做是Stackoverflow上Fortran問題的常見原因。 –

+0

@DaveP,具有假定形狀參數的過程(即'DIMENSION(:)')在調用代碼中也必須有明確的接口(或者是模塊過程)。 –