Sign in Join
Wiki copytemporary

Example: Multidimensional dynamic array/fr: what changed

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

OldNewLine
11### Code
2-```
2+
3+``` pascal
34program 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-```
835
84-
6+type
7+ TArr2D = array of array of Integer;
858
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- ...
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.
15282```
15383
84+### Sortie
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+
154151## Voir aussi
155152
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>)
153+- [[Dynamic array/fr|Tableaux dynamiques]]
154+- [Guide de référence Free Pascal : Tableaux dynamiques](http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1)
158155
159156---
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).*
157+*From the Free Pascal wiki, page "Example: Multidimensional dynamic array/fr", as it stood on 2020-02-14 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).*

Back to history