Sign in Join
Wiki copytemporary

Example: Multidimensional dynamic array: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Example: Multidimensional dynamic array

OldNewLine
1+### Code
2+
3+``` pascal
4+program project1;
5+
6+type
7+ TArr2D = array of array of Integer;
8+
9+procedure FillArr2D(Arr: TArr2D);
10+var
11+ i, j: Integer;
12+begin
13+ for i:=Low(Arr) to High(Arr) do
14+ for j:=Low(Arr[i]) to High(Arr[i]) do
15+ Arr[i][j] := i*10 + j;
16+end;
17+
18+procedure ShowArr2D(Arr: TArr2D);
19+var
20+ i, j: Integer;
21+begin
22+ for i:=Low(Arr) to High(Arr) do
23+ begin
24+ Write('Arr[', i, ']: ');
25+ for j:=Low(Arr[i]) to High(Arr[i]) do
26+ Write(Arr[i][j]:2, ' ');
27+ WriteLn;
28+ end;
29+end;
30+
31+procedure Pause;
32+begin
33+ WriteLn;
34+ Write('...');
35+ ReadLn;
36+ WriteLn;
37+end;
38+
39+var
40+ Arr2D: TArr2D;
41+
42+begin
43+ SetLength(Arr2D, 5, 3); // resize on two dimensions
44+ WriteLn('Rectangular dynamic array after resizing:');
45+ WriteLn('-----------------------------------------');
46+ ShowArr2D(Arr2D);
47+ Pause;
48+
49+ FillArr2D(Arr2D);
50+ WriteLn('Rectangular dynamic array after filling:');
51+ WriteLn('-----------------------------------------');
52+ ShowArr2D(Arr2D);
53+ Pause;
54+
55+ SetLength(Arr2D, 10); // resize on one dimension
56+ WriteLn('Non-rectangular dynamic array after resizing on first dimension:');
57+ WriteLn('----------------------------------------------------------------');
58+ ShowArr2D(Arr2D);
59+ Pause;
60+
61+ SetLength(Arr2D[0], 9);
62+ SetLength(Arr2D[1], 7);
63+ SetLength(Arr2D[2], 6);
64+ SetLength(Arr2D[3], 9);
65+ SetLength(Arr2D[4], 5);
66+ SetLength(Arr2D[5], 8);
67+ SetLength(Arr2D[6], 5);
68+ SetLength(Arr2D[7], 3);
69+ SetLength(Arr2D[8], 1);
70+ SetLength(Arr2D[9], 7);
71+ WriteLn('Non-rectangular dynamic array after individual resizing on second dimension:');
72+ WriteLn('----------------------------------------------------------------------------');
73+ ShowArr2D(Arr2D);
74+ Pause;
75+
76+ FillArr2D(Arr2D);
77+ WriteLn('Non-rectangular dynamic array after filling:');
78+ WriteLn('--------------------------------------------');
79+ ShowArr2D(Arr2D);
80+ Pause;
81+end.
82+```
83+
84+### Output
85+
86+`Rectangular dynamic array after resizing:`\
87+`-----------------------------------------`\
88+`Arr[0]:   0   0   0`\
89+`Arr[1]:   0   0   0`\
90+`Arr[2]:   0   0   0`\
91+`Arr[3]:   0   0   0`\
92+`Arr[4]:   0   0   0`\
93+
94+`...`\
95+
96+`Rectangular dynamic array after filling:`\
97+`-----------------------------------------`\
98+`Arr[0]:   0   1   2`\
99+`Arr[1]:  10  11  12`\
100+`Arr[2]:  20  21  22`\
101+`Arr[3]:  30  31  32`\
102+`Arr[4]:  40  41  42`\
103+
104+`...`\
105+
106+`Non-rectangular dynamic array after resizing on first dimension:`\
107+`----------------------------------------------------------------`\
108+`Arr[0]:   0   1   2`\
109+`Arr[1]:  10  11  12`\
110+`Arr[2]:  20  21  22`\
111+`Arr[3]:  30  31  32`\
112+`Arr[4]:  40  41  42`\
113+`Arr[5]:`\
114+`Arr[6]:`\
115+`Arr[7]:`\
116+`Arr[8]:`\
117+`Arr[9]:`\
118+
119+`...`\
120+
121+`Non-rectangular dynamic array after individual resizing on second dimension:`\
122+`----------------------------------------------------------------------------`\
123+`Arr[0]:   0   1   2   0   0   0   0   0   0`\
124+`Arr[1]:  10  11  12   0   0   0   0`\
125+`Arr[2]:  20  21  22   0   0   0`\
126+`Arr[3]:  30  31  32   0   0   0   0   0   0`\
127+`Arr[4]:  40  41  42   0   0`\
128+`Arr[5]:   0   0   0   0   0   0   0   0`\
129+`Arr[6]:   0   0   0   0   0`\
130+`Arr[7]:   0   0   0`\
131+`Arr[8]:   0`\
132+`Arr[9]:   0   0   0   0   0   0   0`\
133+
134+`...`\
135+
136+`Non-rectangular dynamic array after filling:`\
137+`--------------------------------------------`\
138+`Arr[0]:   0   1   2   3   4   5   6   7   8`\
139+`Arr[1]:  10  11  12  13  14  15  16`\
140+`Arr[2]:  20  21  22  23  24  25`\
141+`Arr[3]:  30  31  32  33  34  35  36  37  38`\
142+`Arr[4]:  40  41  42  43  44`\
143+`Arr[5]:  50  51  52  53  54  55  56  57`\
144+`Arr[6]:  60  61  62  63  64`\
145+`Arr[7]:  70  71  72`\
146+`Arr[8]:  80`\
147+`Arr[9]:  90  91  92  93  94  95  96`\
148+
149+`...`
150+
151+## See also
152+
153+- [[Dynamic array]]
154+- [Free Pascal Reference guide: Dynamic arrays](http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1)
155+
156+---
157+*From the Free Pascal wiki, page "Example: Multidimensional dynamic array", as it stood on 2019-06-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
158+Category: Examples

Back to history