Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable size array 1

Status
Not open for further replies.

fanta2

Technical User
Apr 10, 2005
78
CA
I have an array x(?). I don't know the dimension of the array before hand. While I am going through a loop I know the size.
say

do i=1, n

z = f()

if z > 0 then

x(?) = z

end if

end do

I want to start x(dimension) = 1

increase every time by 1

like


j=0
do i=1, n

z = f()

if z > 0 then

j = j+1

Re dimenion x by j

x(?) = z

end if

end do

Can any body help me how I can do this?
 
Thanks again xwb! I appreciate your help by going through the code and then giving corrections!!!!

When I run the code, I have encountered some problems.

1) During compilation it gives me three warnings like "Warning: In the call to LINKED_LIST, actual argument #3 does not match the type and kind of the corresponding dummy argument."

2) I continued with the execution despite the warnings and it couldn't run it says array bounds exceded at line 86 ... the line --- X(J) = CURRENT%VALUE.

Could you please continue your help?
 
1) The problem is in the way you've written the program. If you write it top down, you need an interface block to tell the compiler what it should expect for linked_list. If you'd written it bottom up, that wouldn't be necessary.

2) I think your compiler cannot handle passing null pointers.

Never mind - try this
Code:
SUBROUTINE CALCULATE
   !XWB Tell the compiler what to expect
   INTERFACE
      SUBROUTINE LINKED_LIST(FUNC, K, X)
         INTEGER, INTENT(IN)::FUNC
         INTEGER, INTENT(OUT):: K
         INTEGER, INTENT(OUT) :: X(:)
      END SUBROUTINE
   END INTERFACE

    IMPLICIT NONE

    INTEGER :: J
    INTEGER, PARAMETER:: LLADD=1, LLGET=2, LLSIZE=3
    INTEGER, ALLOCATABLE :: X(:)
    
    !XWB Allocate something to keep runtime happy
    ALLOCATE (X(1))

    ! Fill up the array    
    CALL LINKED_LIST(LLADD, J, X)

    ! Find out how big the array is
    CALL LINKED_LIST (LLSIZE, J, X)
    
    !XWB Deallocate first
    DEALLOCATE (X)
    ! Allocate an array for the result
    ALLOCATE (X(J))
    
    ! Extract the array
    CALL LINKED_LIST (LLGET, J, X)
    
    ! Do whatever you want with X
    
    
    ! Release X
    DEALLOCATE (X)

END SUBROUTINE


 
Thank you very much xwb for all your helps from the very beginning!!! I appreciate it!!!

Now the problem is resolved successfully with your previous suggestion!


1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top