带有数组的Fortran子例程

时间:2012-11-08 01:26:06

标签: arrays fortran subroutine

我遇到了这个程序子程序的问题,该程序在程序的最后。我在子程序中对数组的划分出错了,它表示“无法分类的语句”。希望有人可以帮忙!

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

1 个答案:

答案 0 :(得分:2)

在子例程中,ABHTBBASP未声明为数组,因此提供错误的语句确实毫无意义。它们是标量,因此无法编入索引。