Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

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

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

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum

  ! 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

  REAL, DIMENSION(k, k**2) :: c

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

The point that I am trying to make is that I would like to create an array c that is k-by-k**2 in size, and k is only determined by other computations within the code; k is not known at the very beginning.

But, the above code gives me the following error message at compile time:

mytest.f90:13.31:

  REAL, DIMENSION(k, k**2) :: c
                               1
Error: Unexpected data declaration statement at (1)

where line 13 in my code is the line where I finally declare c: REAL, DIMENSION(k, k**2) :: c.

On the other hand, if I instead declare k and specify its dimensions upfront,

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(3,9) :: c

  ! 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(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

I get the correct output:

 k=           3
 k**2=           9

   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0

But, since I don't know k beforehand, I can't do exactly this in my actual code. Is there some way to "declare" the array c initially, without specifying its dimensions, and then later specify the dimensions once the value of k is known?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...