dimension query

  • Follow


hi there,
I have what is probably a very stupid question :
why use DIMENSION statements to define arrays, when simply writing indices
seems to be sufficient ?
or is it ?

thanks
G.
0
Reply tperzo (4) 9/13/2004 1:52:05 PM

news <tperzo@wanadoo.fr> wrote:
>hi there,
>I have what is probably a very stupid question :
>why use DIMENSION statements to define arrays, when simply writing indices
>seems to be sufficient ?
>or is it ?

I generally agree with you and rarely use DIMENSION statements. But they
can be convenient if you have several arrays with the same shape:

real, dimension(2,5) :: a,b,c,d

is more concise and easier to modify than

real :: a(2,5),b(2,5),c(2,5),d(2,5)

One could use DIMENSION when there are several ALLOCATABLE arrays with the
same number of dimensions, writing the slightly shorter

real, allocatable, dimension(:,:,:) :: a,b,c

instead of

real, allocatable :: a(:,:,:),b(:,:,:),c(:,:,:)

I think Walt Brainerd, one of the creators of the F (Fortran 95 subset) language,
which requires DIMENSION, has said that writing DIMENSION explicitly makes
it easier to find array declarations in code.



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
0
Reply beliavsky (2207) 9/13/2004 12:13:38 PM


"beliavsky@aol.com" <beliavsky@127.0.0.1:7501> wrote in message
news:41458ef2$1_1@127.0.0.1...
>
> news <tperzo@wanadoo.fr> wrote:
> >hi there,
> >I have what is probably a very stupid question :
> >why use DIMENSION statements to define arrays, when simply writing
indices
> >seems to be sufficient ?
> >or is it ?
>
> I generally agree with you and rarely use DIMENSION statements. But they
> can be convenient if you have several arrays with the same shape:
>
> real, dimension(2,5) :: a,b,c,d
>
etc.

Correct, but that's not a DIMENSION statement, that's a DIMENSION attribute.
I think OP may have meant

DIMENSION  a(2,5),b(2,5),c(2,5),d(2,5)

which, as we all know, in the absence of an IMPLICIT NONE statement, can
lead one to forget the type specification, with potentially disastrous
consequences.

Regards,

Mike Metcalf


0
Reply michael.metcalf (122) 9/13/2004 12:47:28 PM

On Mon, 13 Sep 2004 13:47:28 +0100, "Michael Metcalf"
<michael.metcalf@t-online.de> wrote:

[snip]
>DIMENSION  a(2,5),b(2,5),c(2,5),d(2,5)
>
>which, as we all know, in the absence of an IMPLICIT NONE statement, can
>lead one to forget the type specification, with potentially disastrous
>consequences.

But in the absence of IMPLICIT NONE, those are all real.  No need to
look back at the top of the program to see what they are.

Ken Plotkin

0
Reply kplotkin (368) 9/13/2004 11:42:48 PM

Ken Plotkin <kplotkin@nospam-cox.net> wrote in message news:<31cck0t3d3aesbkg1tmcqeotto62p4q7tk@4ax.com>...
> On Mon, 13 Sep 2004 13:47:28 +0100, "Michael Metcalf"
> <michael.metcalf@t-online.de> wrote:
> 
> [snip]
> >DIMENSION  a(2,5),b(2,5),c(2,5),d(2,5)
> >
> >which, as we all know, in the absence of an IMPLICIT NONE statement, can
> >lead one to forget the type specification, with potentially disastrous
> >consequences.
> 
> But in the absence of IMPLICIT NONE, those are all real.  No need to
> look back at the top of the program to see what they are.
> 
> Ken Plotkin

For the program

integer a
dimension a(2)
end

you DO need to look at the top of the program to know what kind of
variable 'a' is. By writing

integer a(2)
end

the information about 'a' is in one place. One wouldn't use 'a' as an
integer variable in a real program, but I think it is better to
localize information by using REAL, INTEGER, CHARACTER, or LOGICAL in
array declarations.
0
Reply beliavsky (2207) 9/14/2004 1:00:56 PM

In article <31cck0t3d3aesbkg1tmcqeotto62p4q7tk@4ax.com>, Ken Plotkin 
<kplotkin@nospam-cox.net> writes
>On Mon, 13 Sep 2004 13:47:28 +0100, "Michael Metcalf"
><michael.metcalf@t-online.de> wrote:
>
>[snip]
>>DIMENSION  a(2,5),b(2,5),c(2,5),d(2,5)
>>
>>which, as we all know, in the absence of an IMPLICIT NONE statement, can
>>lead one to forget the type specification, with potentially disastrous
>>consequences.
>
>But in the absence of IMPLICIT NONE, those are all real.  No need to
>look back at the top of the program to see what they are.
>
>Ken Plotkin
>
The compiler knows they are real. We have no way of telling, other than 
by trusting the programmer to have self-imposed coding standards for 
variable names, whether they should have been double precision, logical 
or even character. It's much easier to forget to put a declaration in 
than it is to type the wrong one, and without IMPLICIT NONE, almost 
impossible to detect.

Catherine.
-- 
Catherine Rees Lay
To email me, use my first name in front of the "at".
0
Reply spamtrap9533 (138) 9/15/2004 8:40:07 AM

beliavsky@aol.com wrote:

(snip)

> integer a(2)

> the information about 'a' is in one place. One wouldn't use 'a' as an
> integer variable in a real program, but I think it is better to
> localize information by using REAL, INTEGER, CHARACTER, or LOGICAL in
> array declarations.

Well, a could be an INTEGER variable in a program that only
used integer variables.  I know a program that started out

       INTEGERA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

There is some advantage to using I though N to start
variable names, even in languages without Fortran's type rules.

-- glen

0
Reply gah (12236) 9/16/2004 4:48:50 AM

In article <RW82d.97838$3l3.71366@attbi_s03>, glen herrmannsfeldt 
<gah@ugcs.caltech.edu> writes
>beliavsky@aol.com wrote:
>
>(snip)
>
>> integer a(2)
>
>> the information about 'a' is in one place. One wouldn't use 'a' as an
>> integer variable in a real program, but I think it is better to
>> localize information by using REAL, INTEGER, CHARACTER, or LOGICAL in
>> array declarations.
>
>Well, a could be an INTEGER variable in a program that only
>used integer variables.  I know a program that started out
>
>      INTEGERA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
>
>There is some advantage to using I though N to start
>variable names, even in languages without Fortran's type rules.
>
>-- glen
>
I first learnt to program aged about 12, in BASIC on a ZX81. At the time 
I thought it strange that all the sample programs used

FOR I = 1 TO N

It took another 9 years before I found out where it came from!

Catherine.
-- 
Catherine Rees Lay
To email me, use my first name in front of the "at".
0
Reply spamtrap9533 (138) 9/21/2004 10:09:41 AM

7 Replies
31 Views

(page loaded in 0.133 seconds)

Similiar Articles:













7/25/2012 3:36:58 AM


Reply: