Re: dimension question



sc wrote:
Hi everybody,
I'm newbie at fortran. So I have a question about 'dimention'. Here is
an example of code to show problem.
ccccccccccccccccccccccccccccccccccccccccccccccccc
program dimtest
c
integer n, maxn
parameter(maxn=100)
real a
dimension a(maxn, maxn)
c
n = 2
a(1,1) = 1
a(1,2) = 2
a(2,1) = 3
a(2,2) = 4
call test(a, n)
end
ccccccccccccccccccccccccccccccccccccccccccccccccc
subroutine test(a, n)
integer n
real a
c problem is in the next string
dimension a(n, n)
c but in this case everithing is fine
c dimension a(100, n)
print *, a(1,1), a(1,2), a(2,1), a(2,2)
end
ccccccccccccccccccccccccccccccccccccccccccccccccc
My expected output is:
1.000000 2.000000 3.000000 4.000000
But in above test program output is like this:
1.000000 0.000000 3.000000 0.000000
So my first question is "why is it so critical to declare the first
dimension of a multidimension array as big as initial declaration
was?"

In the main program the array elements are stored in
10,000 elements in order a(1,1), a(2,1), a(3,1) ...
a(100,1), a(1,2), a(2,2), a(3,2), ... etc.

If the subroutine DIMENSION statement is a(2,2) the array
is expected to be stored a(1,1), a(2,1), a(1,2), a(2,2),
such that array elements map into the first four elements
in the main program, a(1,1), a(2,1), a(3,1), and a(4,1).

I used f95 and f77 compilers from gcc suite on linux.
The second question is "what the best practice to solve such
problems?"

The Fortran 77 solution is to supply another argument to
the subroutine indicating the actual dimension for the
first subscript:

subroutine test(a, n, norig)
integer n
real a
c problem is in the next string
dimension a(norig, n)

Use n when referencing the array as n by n, but
norig such that the right array elements are referenced.

Another possible solution for f95, but not f77, is to
use assumed shape for the subroutine. For assumes shape,
the actual shape is automatically sent to the subroutine.
If you want to use less than the whole array, you still
pass n as a subroutine argument.

-- glen

.



Relevant Pages

  • Re: Loading matrix from file? [fortran 77]
    ... > Is there any way to load matrix (array) from file, ... You need to allocate enough space for the largest possible matrix at ... You had to know at least the first dimension of the matrix before you ... subroutine sub ...
    (comp.lang.fortran)
  • Re: Neq Max Limit
    ... you are passing incorrect arguments to a subroutine. ... One may be the array size and another an array. ... dimension y,dy ... PRINT *,i at the top of the outer loop may tell you if the program is ...
    (comp.lang.fortran)
  • Re: g95 wish list
    ... (big snip on dimension of (1) ... > have a bounds check which fails if the subroutine references X. ... allowing the full dimension information to be ... > the possibility of a check as to whether you passed an array of size 2 ...
    (comp.lang.fortran)
  • Re: Variable Length Arrays
    ... > subroutine S ... In FORTRAN 77 the notion of dimension(*) was introduced, ... It indicates that the programmer knows that the array has ...
    (comp.lang.fortran)
  • Re: VB .Net and Intel Visual Fortran 9
    ... having is that the VB program cannot load the DLL. ... subroutine) to accept an array from VB - in my case a 2D array. ... Declare Sub mmult Lib "test.dll" (ByRef i As Double, ByRef j As Double, ...
    (comp.lang.fortran)