Quote
Sheryl Coyle wrote in message <35230D13.4...@iprolink.co.nz>...
>I am writing a program in Delphi and I need to use arrays. My problem is
that I will be running my program on
Quote
>many different data sets, each with different sizes/numbers of particular
variables. What I am having to do
Quote
>at the moment is declare my arrays at the beginning of my program such that
it can handle any particular data
Quote
>set that it encounters.
>for example, I have to declare:
>myArray = array[1..largestPossibleA, 1..largestPossibleB,
1..largestPossibleC] of integer;
Quote
>This is making my arrays far to big however. A lot of the time it is
wasting space also, for example the
Quote
>current data set might need a lot of the 1..largestPossibleA's but not many
of the 1..largestPossibleB's and
Quote
>C's (I hope I that last sentence makes sense :) )
If you're new at this, I'd recommend mastering a 2D case before starting on
the 3D case. In C for a 2D array, you can setup an array of pointers, with
each of these pointers pointing to a dynamically allocated array. (For 3D,
this first array of pointers would point to a 2D matrix of values).
Quote
>What I want is to be able to dynamically change/increase/decrease the size
of the array as my program is
Quote
>running (similar to what can be done in C++ I am told). So for example I
would read in my data and see that
Quote
>the biggest A=10 and the biggest B = 15 and the biggest C = 10 for this
data set, so I dynamically create an
Quote
>array which is array[1..10, 1..15, 1..10] of integer/real/whatever.
Unless you're using Delphi 1, you have few memory limitations.
The memory requirements for a 10 x 15 x 10 array of number is not very
great.
Quote
>I didnt think it was possible to do this in Pascal but someone told me that
Delphi has something called
Quote
>dynamic arrays. I looked it up in the help but it was not very helpful at
all. Can someone help me with this
Quote
>please???? (if you can help me it would be great if you were quite specific
in your instructions as I am new
Study this example:
==============================================
// Note: the following looks indented properly with a fixed-width font
// Delphi 3 Example of 2-Dimensional Dynamic Array
CONST
ArrayMaxCount = 65536;
TYPE
pDoubleArray = ^TDoubleArray;
TDoubleArray = ARRAY[0..ArrayMaxCount-1] OF DOUBLE;
pMatrix = ^TMatrix;
TMatrix = ARRAY[0..ArrayMaxCount-1] OF pDoubleArray;
procedure TForm1.Button1Click(Sender: TObject);
CONST
iCountMax = 500; // columns
jCountMax = 1000; // rows
VAR
i : 0..iCountMax-1;
j : 0..jCountMax-1;
k : INTEGER;
// Let's make Matrix 1000 rows of 500 columns =
// 500,000 elements = 4,000,000 bytes
Matrix: pMatrix;
Sum : DOUBLE;
begin
// Allocate the pointers to each row
GetMem(Matrix, jCountMax*SizeOf(pMatrix));
TRY
// Allocate each row of DOUBLEs
FOR j := 0 TO jCountMax-1 DO
GetMem(Matrix[j], iCountMax*SizeOf(DOUBLE));
TRY
// Assign value to each Matrix element
k := 1;
FOR j := 0 TO jCountMax - 1 DO
BEGIN
FOR i := 0 TO iCountMax -1 DO
BEGIN
Matrix[j]^[i] := k;
INC(k);
END;
END;
// Add up all values in Matrix. The sum of 1..N = N*(N+1)/2, so
// the sum should be (iCountMax*jCountMax)*(iCountMax*jCountMax
+ 1) / 2,
// or in this case SUM(1..500,000) = 125,000,250,000.
Sum := 0;
FOR j := 0 TO jCountMax - 1 DO
FOR i := 0 TO iCountMax -1 DO
Sum := Sum + Matrix[j]^[i];
Button1.Caption := FloatToStr(Sum);
FINALLY
// Free each row of DOUBLEs
FOR j := jCountMax-1 DOWNTO 0 DO
FreeMem(Matrix[j]);
END
FINALLY
// Free array of pointers
FreeMem(Matrix)
END
end;
Normally, I'd suggest that you also look at Items D-8 and D-10 in my Delphi
Math
FAQ and Links page. (My web server is down right now, however.)
efg
_________________________________________
efg's Computer Lab: http://infomaster.net/external/efg
Earl F. Glynn E-Mail: EarlGl...@att.net
MedTech Research Corporation, Lenexa, KS USA