For Programmers: Free Programming Magazines  


Home > Archive > Fortran > September 2004 > dimension query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author dimension query
news

2004-09-13, 9:03 am

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.
beliavsky@aol.com

2004-09-13, 9:03 am


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 =---
Michael Metcalf

2004-09-13, 9:03 am


"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:
indices[color=darkred]
>
> 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


Ken Plotkin

2004-09-13, 8:56 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

beliavsky@aol.com

2004-09-14, 3:59 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]
>
> 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.
glen herrmannsfeldt

2004-09-16, 5:01 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

Catherine Rees Lay

2004-09-21, 9:04 am

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]
>
>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".
Catherine Rees Lay

2004-09-22, 9:09 am

In article <RW82d.97838$3l3.71366@attbi_s03>, glen herrmannsfeldt
<gah@ugcs.caltech.edu> writes
>beliavsky@aol.com wrote:
>
>(snip)
>
>
>
>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".
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com