Allocate dynamic array with interdependent dimensions

后端 未结 2 1395
星月不相逢
星月不相逢 2021-01-18 18:21

This is a bit complicated; I\'d welcome any comments on how to improve the clarity of the question.

Ok, say I have an array:

real, allocatable :: A(:         


        
2条回答
  •  情话喂你
    2021-01-18 19:11

    I think you can do this simply by allocating/deallocating the array

    Program test
        Implicit none
        Real, dimension(:,:,:), allocatable :: A
        Integer  :: i,N
        Write(*,*)"Enter N"; Read(*,*)N
        Do i = 1, N
        if(Allocated(A)) then 
        deallocate(A);Allocate(A(i,i,i*i))
        else
        allocate(A(i,i,i*i))
        end if
        Write(*,*)Shape(A)
        End do
    end program test
    

    Compiling the program using gfortran gives:

     Enter N
    5
               1           1           1
               2           2           4
               3           3           9
               4           4          16
               5           5          25
    

提交回复
热议问题