BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sub-dimension of a multidimensional dynamic array?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Richard
Guest





PostPosted: Mon Jan 09, 2006 3:10 pm    Post subject: Sub-dimension of a multidimensional dynamic array? Reply with quote



I would like to iterate over a sub-dimension of a multidimensional
dynamic array.

The compiler complains about incompatible types.
------------------
type
Ttwod = array of array of longword;

procedure foo (twod:Ttwod);
var
oned: array of longword;
i, j, sum, floormean : longword;
begin
for i := low(twod) to high(twod) do begin
oned := twod[i];
for j := low(oned) to high(oned) do begin
// do stuff
Inc (sum, oned[j]);
end;
floormean := sum div j;
end;
end;
------------------

In this case I would sum one[j] in the hopes that it compiles to code
that is more efficient than if it were summing twod[i,j].

The presumption is that there would be fewer array address calculations
going on 'behind the scenes' when using [j] instead [i,j]

Back to top
alanglloyd@aol.com
Guest





PostPosted: Mon Jan 09, 2006 3:33 pm    Post subject: Re: Sub-dimension of a multidimensional dynamic array? Reply with quote



The array of longword in TTWod is not the same type as the array of
longword declared for oned.

For example declared variables ..

A : array of longword;
B : array of longword;

.... are of two different types. Read the Delphi Object Pascal Language
Guide.

If you want to do what you appear to want, you must declare ...

TOneWord = array of longword;

TTwoWord = array of TOneWord;

Oned : TOneWord;

..... then it would compile.

I doubt if you would see any difference in speed from using ...

Sum := 0;
for i := Low(TwoD) to High(TwoD) do
for j := Low(TwoD[1]) to High(TwoD[1]) do
inc(Sum, TwoD[i, j]);

.... maybe its even slightly faster <g>.

You have also forgotten to initialise Sum.

Alan Lloyd

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.