program arrayplay;
{$MODE OBJFPC}{$H+}
procedure PlayWithDynamicArrays;
var
// Declare variable with 1 dimension
OneDimension : array of integer;
// Declare variable with 2 dimensions
TwoDimensions : array of array of integer;
// Declare variable with 3 dimensions
ThreeDimensions : array of array of array of integer;
// declare some index variables to acces individual items inside array
i,j,k : integer;
begin
// =================
// Initialize arrays
// =================
// Initialize 1 dimensional array (5 items)
SetLength(OneDimension, 5);
// Initialize 2 dimensional array (5*4 = 20 items)
SetLength(TwoDimensions, 5, 4);
// Initialize 3 dimensional array (5*4*3 = 60 items)
SetLength(ThreeDimensions, 5, 4, 3);
// =========================
// Display array information
// =========================
// Display size of array
// ---------------------
// Note that when using function SizeOf() on dynamic arrays that this
// function will actually return the size of an individual item from the
// array, expressed in bytes.
// It will not return the actual bytes occupied by the complete array.
WriteLn;
WriteLn('SizeOf(integer) = ', SizeOf(Integer) , ' bytes');
WriteLn('SizeOf(OneDimension) = ', SizeOf(OneDimension) , ' bytes');
WriteLn('SizeOf(TwoDimensions) = ', SizeOf(TwoDimensions) , ' bytes');
WriteLn('SizeOf(ThreeDimensions) = ', SizeOf(ThreeDimensions) , ' bytes');
// Display size of dimensions and individual items
// -----------------------------------------------
// Note that it doesn't matter which and how many indices are used: the size
// in bytes will be the same for each entry/dimension in the array.
WriteLn;
WriteLn('SizeOf(OneDimension[1]) = ', SizeOf(OneDimension[1]) , ' bytes');
WriteLn('SizeOf(TwoDimensions[1]) = ', SizeOf(TwoDimensions[1]) , ' bytes');
WriteLn('SizeOf(TwoDimensions[1,1]) = ', SizeOf(TwoDimensions[1,1]) , ' bytes');
WriteLn('SizeOf(ThreeDimensions[1]) = ', SizeOf(ThreeDimensions[1]) , ' bytes');
WriteLn('SizeOf(ThreeDimensions[1,1]) = ', SizeOf(ThreeDimensions[1,1]) , ' bytes');
WriteLn('SizeOf(ThreeDimensions[1,1,1]) = ', SizeOf(ThreeDimensions[1,1,1]), ' bytes');
// Determine/display length of array
// ---------------------------------
// Note that only the length of the first dimension of the array is being
// displayed here
WriteLn;
WriteLn('Length(OneDimension) = ', Length(OneDimension) , ' items');
WriteLn('Length(TwoDimensions) = ', Length(TwoDimensions) , ' items');
WriteLn('Length(ThreeDimensions) = ', Length(ThreeDimensions), ' items');
// Determine/display length of dimensions of array
// -----------------------------------------------
// Note that the dimensions of the array are taken into account here.
//
// Note2 that it is not valid to invoke function Length() on a individual
// item inside the array, as function Length() only operates on arrays.
//
// So, Length(ThreeDimensions[1,1,1]) will result in a type mismatch -> an
// indidividual item is an integer and its size is determined by SizeOf (see
// also code above). (unless, of course, an individual item in the array is
// also an array itself).
WriteLn;
WriteLn('Length(OneDimension) = ', Length(OneDimension) , ' items');
WriteLn('Length(TwoDimensions[1]) = ', Length(TwoDimensions[1]) , ' items');
WriteLn('Length(ThreeDimensions[1,1]) = ', Length(ThreeDimensions[1,1]), ' items');
// ================================================
// Determine "range" of arrray (e.g. valid indices)
// ================================================
// Note that this will only determine the range of the first dimension
WriteLn;
WriteLn('Low(OneDimension) = ', Low(OneDimension));
WriteLn('High(OneDimension) = ', High(OneDimension));
WriteLn('Low(TwoDimensions) = ', Low(TwoDimensions));
WriteLn('High(TwoDimensions) = ', High(TwoDimensions));
WriteLn('Low(ThreeDimensions) = ', Low(ThreeDimensions));
WriteLn('High(ThreeDimensions) = ', High(ThreeDimensions));
// Determine the range of other dimensions (where applicable)
// ----------------------------------------------------------
WriteLn;
WriteLn('Low(OneDimension) = ', Low(OneDimension));
WriteLn('High(OneDimension) = ', High(OneDimension));
WriteLn('Low(TwoDimensions[1]) = ', Low(TwoDimensions[1]));
WriteLn('High(TwoDimensions[1]) = ', High(TwoDimensions[1]));
WriteLn('Low(ThreeDimensions[1,1]) = ', Low(ThreeDimensions[1,1]));
WriteLn('High(ThreeDimensions[1,1]) = ', High(ThreeDimensions[1,1]));
// =================================
// Intialize arrays with some values
// =================================
// Set values for 1 dimensional array
for i := Low(OneDimension) to High(OneDimension)
do OneDimension[i] := i;
// Set values for 2 dimensional array
for i := Low(TwoDimensions) to High(TwoDimensions) do
for j := Low(TwoDimensions[i]) to High(TwoDimensions[i]) do
TwoDimensions[i,j] := i*j;
// Set values for 3 dimensional array
for i := Low(ThreeDimensions) to High(ThreeDimensions) do
for j := Low(ThreeDimensions[i]) to High(ThreeDimensions[i]) do
for k := Low(ThreeDimensions[i,j]) to High(ThreeDimensions[i,j]) do
ThreeDimensions[i,j,k] := i*j*k;
// ===============================
// Display values stored in arrays
// ===============================
// Display 1 dimensional array values
WriteLn;
WriteLn('Values inside one dimensional array:');
for i := Low(OneDimension) to High(OneDimension) do
WriteLn('Item[', i, '] := ', OneDimension[i]);
// Display 2 dimensional array values
WriteLn;
WriteLn('Values inside two dimensional array:');
for i := Low(TwoDimensions) to High(TwoDimensions) do
for j := Low(TwoDimensions[i]) to High(TwoDimensions[i]) do
WriteLn('Item[', i, ',' , j, '] := ', TwoDimensions[i, j]);
// Display 3 dimensional array values
WriteLn;
WriteLn('Values inside three dimensional array:');
for i := Low(TwoDimensions) to High(TwoDimensions) do
for j := Low(TwoDimensions[i]) to High(TwoDimensions[i]) do
for k := Low(ThreeDimensions[i,j]) to High(ThreeDimensions[i,j]) do
WriteLn('Item[', i, ',' , j, ',' , k, '] := ', ThreeDimensions[i,j,k]);
// ==========================================
// Clear Arrays (e.g. release used resources)
// ==========================================
SetLength(OneDimension, 0);
SetLength(TwoDimensions, 0, 0);
SetLength(ThreeDimensions, 0, 0, 0);
end;
Procedure PlayWithStaticArray;
Var
OneDimension : array[15..30] of Word;
i : Integer;
begin
// ==========================================
// Display size of array (expressed in bytes)
// ==========================================
// Note that with a static declared array, function SizeOf() will return the
// number of bytes actually occupied.
//
// This is different when applied to dynamic arrays, which will return
// the size of an individual item inside the array instead. This is because
// function SizeOf() determines these sizes at compile time, not runtime.
WriteLn;
WriteLn('SizeOf(Word) = ', SizeOf(Word) , ' bytes');
WriteLn('SizeOf(OneDimension) = ', SizeOf(OneDimension) , ' bytes');
// Display size of a single item in array
// --------------------------------------
WriteLn;
WriteLn('SizeOf(OneDimension[15]) = ', SizeOf(OneDimension[15]) , ' bytes');
// Determine length of array
// -------------------------
// Note that the length of the first dimension is determined.
WriteLn;
WriteLn('Length(OneDimension) = ', Length(OneDimension), ' items');
// ========================
// determine range of array
// ========================
WriteLn('Low(OneDimension) = ', Low(OneDimension));
WriteLn('High(OneDimension) = ', High(OneDimension));
// ================================
// Intialize array with some values
// ================================
// Set values for 1 dimensional array
for i := Low(OneDimension) to High(OneDimension)
do OneDimension[i] := i;
// =======================
// Display values in array
// =======================
// Display 1 dimensional array values
WriteLn;
WriteLn('Values inside one dimensional array:');
for i := Low(OneDimension) to High(OneDimension) do
WriteLn('Item[', i, '] := ', OneDimension[i]);
end;
procedure PassAlong(AnArray: Array of Integer);
var
i: Integer;
begin
// Display length of array
WriteLn;
WriteLn('Length(AnArray) = ', Length(AnArray), ' items');
// Display range of array
WriteLn;
WriteLn('Low(AnArray) = ', Low(AnArray));
WriteLn('High(AnArray) = ', High(AnArray));
// display array values
WriteLn;
WriteLn('Array values are:');
For i := Low(AnArray) to High(AnArray) do
WriteLn('Item[', i, '] := ', AnArray[i]);
end;
Procedure PassAlongStaticArray;
var
AnArray : array[200..205] of Integer;
i : Integer;
begin
// Display length of array
WriteLn;
WriteLn('Length(AnArray) = ', Length(AnArray), ' items');
// Display range of array
WriteLn;
WriteLn('Low(AnArray) = ', Low(AnArray));
WriteLn('High(AnArray) = ', High(AnArray));
// fill array with values
For i := Low(AnArray) to High(AnArray) do AnArray[i] := i;
// display array values
WriteLn;
WriteLn('Array values are:');
For i := Low(AnArray) to High(AnArray) do
WriteLn('Item[', i, '] := ', AnArray[i]);
// Pass along the array to another function/procedure
// Note the different numbers used for index)
WriteLn;
WriteLn('Passing the array to another function/procedure');
WriteLn('Note the difference in index values used');
PassAlong(AnArray);
end;
begin
WriteLn('====================================');
WriteLn('Playing with arrays');
WriteLn('====================================');
WriteLn;
WriteLn('--------------');
WriteLn('Dynamic arrays');
WriteLn('--------------');
PlayWithDynamicArrays;
WriteLn;
WriteLn('------------');
WriteLn('Static array');
WriteLn('------------');
PlayWithStaticArray;
WriteLn;
WriteLn('-------------');
WriteLn('Pass an array');
WriteLn('-------------');
PassAlongStaticArray;
WriteLn;
WriteLn('====================================');
WriteLn('done');
WriteLn('====================================');
end.