In Fortran 90, do array dimensions have to be declared beforehand?

前端 未结 1 1050
有刺的猬
有刺的猬 2020-12-04 02:15

Is it necessary to declare array dimensions before any other code? For example, I have written the following simplified example code:

PROGRAM mytest
  IMPLI         


        
相关标签:
1条回答
  • 2020-12-04 02:54

    You want to use allocatable arrays:

    PROGRAM mytest
      IMPLICIT NONE
      INTEGER :: i, j, k, mysum
      REAL, DIMENSION(:,:), allocatable :: c   !<-  c is allocatable, rank 2
    
      ! Let array c be a k-by-k**2 array
      ! Determine k within the program by some means...for example,
      mysum=0
      DO i=1, 3
        mysum=mysum+1
      END DO
      k=mysum
    
      WRITE(*,*) "k=", k
      WRITE(*,*) "k**2=", k**2
      WRITE(*,*)
    
      allocate(c(k,k**2))                  ! <-- allocate array c with supplied shape
    
      DO i=1,size(c,1)
        WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
      END DO
    
      deallocate(c)                        ! <-- deallocate when done
    END PROGRAM mytest
    
    0 讨论(0)
提交回复
热议问题