 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Richard Guest
|
Posted: Mon Jan 09, 2006 3:10 pm Post subject: Sub-dimension of a multidimensional dynamic array? |
|
|
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
|
Posted: Mon Jan 09, 2006 3:33 pm Post subject: Re: Sub-dimension of a multidimensional dynamic array? |
|
|
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 |
|
 |
|
|
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
|
|