Sign in Join
Wiki copytemporary

Example: Multidimensional dynamic array/fr: what changed

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

Title: Example: Multidimensional dynamic array/fr

OldNewLine
1+### Code
2+```
3+program project1;
4+
5+ type
6+ TArr2D = array of array of Integer;
7+
8+ procedure FillArr2D(Arr: TArr2D);
9+ var
10+ i, j: Integer;
11+ begin
12+ for i:=Low(Arr) to High(Arr) do
13+ for j:=Low(Arr[i]) to High(Arr[i]) do
14+ Arr[i][j] := i*10 + j;
15+ end;
16+
17+ procedure ShowArr2D(Arr: TArr2D);
18+ var
19+ i, j: Integer;
20+ begin
21+ for i:=Low(Arr) to High(Arr) do
22+ begin
23+ Write('Arr[', i, ']: ');
24+ for j:=Low(Arr[i]) to High(Arr[i]) do
25+ Write(Arr[i][j]:2, ' ');
26+ WriteLn;
27+ end;
28+ end;
29+
30+ procedure Pause;
31+ begin
32+ WriteLn;
33+ Write('...');
34+ ReadLn;
35+ WriteLn;
36+ end;
37+
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+
85+
86+### Sortie
87+```
88+Rectangular dynamic array after resizing:
89+ -----------------------------------------
90+ Arr[0]: 0 0 0
91+ Arr[1]: 0 0 0
92+ Arr[2]: 0 0 0
93+ Arr[3]: 0 0 0
94+ Arr[4]: 0 0 0
95+
96+ ...
97+
98+ Rectangular dynamic array after filling:
99+ -----------------------------------------
100+ Arr[0]: 0 1 2
101+ Arr[1]: 10 11 12
102+ Arr[2]: 20 21 22
103+ Arr[3]: 30 31 32
104+ Arr[4]: 40 41 42
105+
106+ ...
107+
108+ Non-rectangular dynamic array after resizing on first dimension:
109+ ----------------------------------------------------------------
110+ Arr[0]: 0 1 2
111+ Arr[1]: 10 11 12
112+ Arr[2]: 20 21 22
113+ Arr[3]: 30 31 32
114+ Arr[4]: 40 41 42
115+ Arr[5]:
116+ Arr[6]:
117+ Arr[7]:
118+ Arr[8]:
119+ Arr[9]:
120+
121+ ...
122+
123+ Non-rectangular dynamic array after individual resizing on second dimension:
124+ ----------------------------------------------------------------------------
125+ Arr[0]: 0 1 2 0 0 0 0 0 0
126+ Arr[1]: 10 11 12 0 0 0 0
127+ Arr[2]: 20 21 22 0 0 0
128+ Arr[3]: 30 31 32 0 0 0 0 0 0
129+ Arr[4]: 40 41 42 0 0
130+ Arr[5]: 0 0 0 0 0 0 0 0
131+ Arr[6]: 0 0 0 0 0
132+ Arr[7]: 0 0 0
133+ Arr[8]: 0
134+ Arr[9]: 0 0 0 0 0 0 0
135+
136+ ...
137+
138+ Non-rectangular dynamic array after filling:
139+ --------------------------------------------
140+ Arr[0]: 0 1 2 3 4 5 6 7 8
141+ Arr[1]: 10 11 12 13 14 15 16
142+ Arr[2]: 20 21 22 23 24 25
143+ Arr[3]: 30 31 32 33 34 35 36 37 38
144+ Arr[4]: 40 41 42 43 44
145+ Arr[5]: 50 51 52 53 54 55 56 57
146+ Arr[6]: 60 61 62 63 64
147+ Arr[7]: 70 71 72
148+ Arr[8]: 80
149+ Arr[9]: 90 91 92 93 94 95 96
150+
151+ ...
152+```
153+
154+## Voir aussi
155+
156+ * [[Dynamic array/fr|Tableaux dynamiques]]
157+ * [Guide de référence Free Pascal : Tableaux dynamiques](<http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1>)
158+
159+---
160+*From the Free Pascal wiki, page "Example: Multidimensional dynamic array/fr", as the Internet Archive kept it on 2023-03-22. 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).*

Back to history