[comp.lang.fortran] variable dim arrays in subroutines

C63753@TRMETU.BITNET (SAOOD KEMAL ABASSI) (12/24/90)

I want to use arrays with variable dimensions in subroutines. In doing so I am
facing two problems:
1) Even when I pass ordinary arrays to subroutines the values of elements of
the arrays are changed.
2) sorry| I have forgotten the second one.
I am using following pattern:
PROGRAM MAIN
DIMENSION MATRIX(10,10),RMATRIX(10,10)
.
.
CALL TESTSUB(MATRIX,RMATRIX,N,M)
.
.
SUBROUTINE TESTSUB(MATRIX,RMATRIX,N,M)
DIMENSION MATRIX(N,M),RMATRIX(N,M)
.
Well I have just recalled that this problem encounters normally when I use
arrays with more than one dimension.
I have seen in a book and then used following syntax that is working success-
fully for one dimensional array.
SUBROUTINE TEST(ARR,N)
DIMENSION ARR(1)
This allows to pass matrices (vectors) with more than one element as opposed
to the declaration DIMENSION ARR(1). But it is not useful for two dimensional
arrays.
I could not find any clue to this problem and thus seek help from netnews
friends.
Briefly my problem is how to use variable dimension array in subroutines
and what is the reason that DIMENSION ARR(1) is a workable statement.
I am working on IBM 3090  using fortvs2 compiler.
********
Saood

john@ghostwheel.unm.edu (John Prentice) (12/24/90)

In article <90357.185759C63753@TRMETU.BITNET> C63753@TRMETU.BITNET (SAOOD KEMAL ABASSI) writes:
>I want to use arrays with variable dimensions in subroutines. In doing so I am
>facing two problems:
>1) Even when I pass ordinary arrays to subroutines the values of elements of
>the arrays are changed.
>PROGRAM MAIN
>DIMENSION MATRIX(10,10),RMATRIX(10,10)
>.
>.
>CALL TESTSUB(MATRIX,RMATRIX,N,M)
>.
>.
>SUBROUTINE TESTSUB(MATRIX,RMATRIX,N,M)
>DIMENSION MATRIX(N,M),RMATRIX(N,M)
>.

This should work fine in Fortran 77.  Should MATRIX be an integer
matrix however?  Apart from that, this is fine so long as N and M
are passed set to 10 (in this example) by the calling routine.

Your message was unclear to me about what was failing.  Perhaps if
you put a complete test program out so we could try it ourselves
it would be clearer.  Good luck!

John Prentice
john@unmfys.unm.edu

dik@cwi.nl (Dik T. Winter) (12/24/90)

In article <1990Dec23.203218.28723@ariel.unm.edu> john@ghostwheel.unm.edu (John Prentice) writes:
 > In article <90357.185759C63753@TRMETU.BITNET> C63753@TRMETU.BITNET (SAOOD KEMAL ABASSI) writes:
 > >I want to use arrays with variable dimensions in subroutines. In doing so I am
 > >facing two problems:
etc.
 > This should work fine in Fortran 77.  Should MATRIX be an integer
 > matrix however?  Apart from that, this is fine so long as N and M
 > are passed set to 10 (in this example) by the calling routine.
This is the gotcha; I presume N and M are not set to 10.  (The giveaway is
that the original author complains that the elements are in the wrong place.)

To make it working:
 > >PROGRAM MAIN
 > >DIMENSION MATRIX(10,10),RMATRIX(10,10)
 > >.
 > >CALL TESTSUB(MATRIX,RMATRIX,N,M)
--->CALL TESTSUB(MATRIX,RMATRIX,10,N,M)
 > >.
 > >.
 > >SUBROUTINE TESTSUB(MATRIX,RMATRIX,N,M)
--->SUBROUTINE TESTSUB(MATRIX,RMATRIX,LD,N,M)
 > >DIMENSION MATRIX(N,M),RMATRIX(N,M)
--->DIMENSION MATRIX(LD,M),RMATRIX(LD,M)

And now you can use the N*M sections of MATRIX and RMATRIX.  As all Fortran
old-timers know: *Always pass the leading dimension of a two-dimensional
array to a subroutine*.  Array's are not first-class citizens in Fortran.
An array is a piece of storage and a mapping function from indices to
address.  When passing an array in Fortran only the piece of storage is
passed, not the mapping function; the mapping function is locally constructed
in the subroutine based on information available.  (Fodder for flamage: Of
the languages I have used extensively C, Fortran and Pascal do not have
array's as first class citizens for various reasons; Algol 60, Algol 68 and
Ada do.)
--
dik t. winter, cwi, amsterdam, nederland
dik@cwi.nl