Re: dimension question
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Sun, 25 Feb 2007 04:52:38 -0800
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
.
- References:
- dimension question
- From: sc
- dimension question
- Prev by Date: Re: pictures as comments
- Next by Date: Re: dimension question
- Previous by thread: dimension question
- Next by thread: Re: dimension question
- Index(es):
Relevant Pages
|
|