Question:
How do you define a two dimensional jagged array in Free Pascal Compiler?
polycoder
2011-03-02 06:12:06 UTC
I have an project, i need to define two dimensional 'jagged' arrays in FPC pls? I tried several ways, none work, i just get errors when i initialise it. Can anyone pls tell me how this is done pls.?

thnx
Five answers:
?
2011-03-02 07:46:29 UTC
Hi there,



Try something like the following:



na



program JagArrDemo;



type

TDynArray = Array of Integer;

TJaggedArray = Array of TDynArray;



var

JagArray: TJaggedArray;

i,j: integer;



procedure SizeJagArray(Size: Integer; var JArray: TJaggedArray);

begin

//set the initial size of a jagged array

SetLength(JArray,Size);

end; {SizeJagArray}



procedure SizeJagArrayElement(Indx, Size: Integer; var JArray: TJaggedArray);

begin

//set the size of an element in a jagged array

if (Indx>-1) and (Indx<=High(JArray)) then

SetLength(JArray[Indx],Size)

else

//display "index out of bounds" error

WriteLn('Index out of bounds "',Indx,'".');

end; {SizeJagArrayElement}



procedure initJagArray(var JArray: TJaggedArray);

var i,j: integer;

begin

//initialise a jagged array

for i:=Low(JArray) to High(JArray) do

for j:=Low(JArray[i]) to High(JArray[i]) do

JArray[i,j]:=0;

end; {initJagArray}



begin

//initialise

SizeJagArray(10,JagArray);

//set jagged ellement sizes

for i:=Low(JagArray) to High(JagArray) do

SizeJagArrayElement(i,i+1,JagArray);

//initialise jagged array

initJagArray(JagArray);

//populate array

for i:=Low(JagArray) to High(JagArray) do

for j:=Low(JagArray[i]) to High(JagArray[i]) do

JagArray[i,j]:=j;

//finally check array

for i:=Low(JagArray) to High(JagArray) do

begin

WriteLn('JagArray[',i,'] has ',High(JagArray[i])+1,' elements.');

for j:=Low(JagArray[i]) to High(JagArray[i]) do

WriteLn(' - JagArray[',i,',',j,']=',JagArray[i,j]);

end;

end.
cosimo
2011-03-02 06:36:07 UTC
I haven't used Pascal for years, and not the Free Pascal compiler, but this question intrigued me so I had a look. I found a couple of links that might help so hope they're useful (see below).



It sounds like you'd need to declare an array of pointers, then use getmem on each one to allocate memory for them (and give them something to point at). So, e.g.



type

ptrarray = array [1..100] of pointer;

var

i : integer;

begin

for i := 1 to 100 do

getmem(ptrarray[i], i);

end;



I'm not sure that's gone far enough for what you want though however, just had a second glance and it looks like you might be able to use dynamic array, see the third link. So you'd probably have something like:



type

dynarray = array of Integer;

var

jaggedarray : array [1..100] of dynarray;

i : integer;

begin

for i := 1 to 100 do

setlength(jaggedarray[i], i);

:

:

etc

:

end;



Hope this is of some use.
2016-10-04 03:56:21 UTC
Define Jagged
wiltrout
2016-10-27 12:12:10 UTC
some years decrease back I saved a Praying Mantis as a puppy, and they in elementary words eat stay food! therefore, I obtained the flexibility of gently capturing stay flies in flight, or off any floor, so as that i might want to feed my Mantis unhurt and lively prey. maximum shifting incident replaced into overdue one evening at the same time as on arrival residing house i got here across my Mantis laying off its pores and skin. I watched, enthralled, for over an hour and that i shall in no way ignore seeing my Mantis steadily improve and spread its sparkling rainbow-shimmering wings. next day I released it onto a grapevine in my backyard, an similar backyard in which I therefore got here across a swarm of very tiny, presently hatched mantids! they are the cutest little creatures you may want to ever imagine.
SALEM M
2011-03-02 09:47:09 UTC
program JaggedArray;



Const

Row = 5;

Col = 10;

var

M : Array [1 .. Row, 1 .. Col] of String;

I, J : Integer;

Key : Char;



begin



repeat

for I := 1 to Row do

for J := 1 to Col do

M[I, J] := '#';



Randomize;



for I := 1 to (Row) do begin

for J := 1 to Random(Col) + 1 do

Write(M[I, J]);

WriteLn; end;



WriteLn('--------------------');



I := 1;

while I <= Row do begin

for J := 1 to Random(Col) + 1 do

Write(M[I, J]);

WriteLn;

I := I + 1; end;



WriteLn('--------------------');

J := 1;

while J <= Col do begin

for I := 1 to Random(Row) + 1 do

Write(M[I, J]);

WriteLn;

J := J + 1; end;



WriteLn('--------------------');



WriteLn;

WriteLn('Other visualizations ?. y / n');

Readln(Key);

until Key = 'n';



WriteLn;

WriteLn('Press any Key to exit');

ReadLn;



end.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...