Sign in Join
Wiki copytemporary

Example: Multidimensional dynamic array/es: what changed

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

OldNewLine
1-Seleccione **Proyecto - > Nuevo proyecto -> Aplicación de consola**, pegue el siguiente código y ejecútelo para obtener el resultado de salida.
1+Seleccione **Proyecto -\> Nuevo proyecto -\> Aplicación de consola**, pegue el siguiente código y ejecútelo para obtener el resultado de salida.
22
33### Código
4-```
4+
5+``` pascal
56program EjemploMatricesDnamicas;
6-
7- type
8- TMatriz2D = Array of Array of Integer;
9-
10- procedure RellenaMatriz2D(Matriz: TMatriz2D);
11- var
12- i, j: Integer;
13- begin
14- for i:=Low(Matriz) to High(Matriz) do
15- for j:=Low(Matriz[i]) to High(Matriz[i]) do
16- Matriz[i][j] := i*10 + j;
17- end;
18-
19- procedure MostrarMatriz2D(Matriz: TMatriz2D);
20- var
21- i, j: Integer;
22- begin
23- for i:=Low(Matriz) to High(Matriz) do
24- begin
25- Write('Matriz[', i, ']: ');
26- for j:=Low(Matriz[i]) to High(Matriz[i]) do
27- Write(Matriz[i][j]:2, ' ');
28- WriteLn;
29- end;
30- end;
31-
32- procedure Pausa;
33- begin
34- WriteLn;
35- Write('...');
36- ReadLn;
37- WriteLn;
38- end;
39-
40-
41- var
42- Matriz2D: TMatriz2D;
43-
44- begin
45- SetLength(Matriz2D, 5, 3); // redimensionar a dos dimensiones
46- WriteLn('Matriz dinámica rectangular después de redimensionar');
47- WriteLn('-----------------------------------------');
48- MostrarMatriz2D(Matriz2D);
49- Pausa;
50-
51- RellenaMatriz2D(Matriz2D);
52- WriteLn('Matriz dinámica rectangular después de rellenar:');
53- WriteLn('-----------------------------------------');
54- MostrarMatriz2D(Matriz2D);
55- Pausa;
56-
57- SetLength(Matriz2D, 10); // redimensionar a una dimensión
58- WriteLn('Matriz dinámica no rectangular después de redimensionar a una dimensión:');
59- WriteLn('----------------------------------------------------------------');
60- MostrarMatriz2D(Matriz2D);
61- Pausa;
62-
63- SetLength(Matriz2D[0], 9);
64- SetLength(Matriz2D[1], 7);
65- SetLength(Matriz2D[2], 6);
66- SetLength(Matriz2D[3], 9);
67- SetLength(Matriz2D[4], 5);
68- SetLength(Matriz2D[5], 8);
69- SetLength(Matriz2D[6], 5);
70- SetLength(Matriz2D[7], 3);
71- SetLength(Matriz2D[8], 1);
72- SetLength(Matriz2D[9], 7);
73- WriteLn('Matriz dinámica no rectangular después de rellenar y redimensionar a una dimensión:');
74- WriteLn('----------------------------------------------------------------------------');
75- MostrarMatriz2D(Matriz2D);
76- Pausa;
77-
78- RellenaMatriz2D(Matriz2D);
79- WriteLn('Matriz dinámica no rectangular después de rellenar:');
80- WriteLn('--------------------------------------------');
81- MostrarMatriz2D(Matriz2D);
82- Pausa;
83- end.
84-```
7+
8+type
9+ TMatriz2D = Array of Array of Integer;
10+
11+procedure RellenaMatriz2D(Matriz: TMatriz2D);
12+var
13+ i, j: Integer;
14+begin
15+ for i:=Low(Matriz) to High(Matriz) do
16+ for j:=Low(Matriz[i]) to High(Matriz[i]) do
17+ Matriz[i][j] := i*10 + j;
18+end;
19+
20+procedure MostrarMatriz2D(Matriz: TMatriz2D);
21+var
22+ i, j: Integer;
23+begin
24+ for i:=Low(Matriz) to High(Matriz) do
25+ begin
26+ Write('Matriz[', i, ']: ');
27+ for j:=Low(Matriz[i]) to High(Matriz[i]) do
28+ Write(Matriz[i][j]:2, ' ');
29+ WriteLn;
30+ end;
31+end;
32+
33+procedure Pausa;
34+begin
35+ WriteLn;
36+ Write('...');
37+ ReadLn;
38+ WriteLn;
39+end;
40+
41+var
42+ Matriz2D: TMatriz2D;
43+
44+begin
45+ SetLength(Matriz2D, 5, 3); // redimensionar a dos dimensiones
46+ WriteLn('Matriz dinámica rectangular después de redimensionar');
47+ WriteLn('-----------------------------------------');
48+ MostrarMatriz2D(Matriz2D);
49+ Pausa;
50+
51+ RellenaMatriz2D(Matriz2D);
52+ WriteLn('Matriz dinámica rectangular después de rellenar:');
53+ WriteLn('-----------------------------------------');
54+ MostrarMatriz2D(Matriz2D);
55+ Pausa;
56+
57+ SetLength(Matriz2D, 10); // redimensionar a una dimensión
58+ WriteLn('Matriz dinámica no rectangular después de redimensionar a una dimensión:');
59+ WriteLn('----------------------------------------------------------------');
60+ MostrarMatriz2D(Matriz2D);
61+ Pausa;
62+
63+ SetLength(Matriz2D[0], 9);
64+ SetLength(Matriz2D[1], 7);
65+ SetLength(Matriz2D[2], 6);
66+ SetLength(Matriz2D[3], 9);
67+ SetLength(Matriz2D[4], 5);
68+ SetLength(Matriz2D[5], 8);
69+ SetLength(Matriz2D[6], 5);
70+ SetLength(Matriz2D[7], 3);
71+ SetLength(Matriz2D[8], 1);
72+ SetLength(Matriz2D[9], 7);
73+ WriteLn('Matriz dinámica no rectangular después de rellenar y redimensionar a una dimensión:');
74+ WriteLn('----------------------------------------------------------------------------');
75+ MostrarMatriz2D(Matriz2D);
76+ Pausa;
77+
78+ RellenaMatriz2D(Matriz2D);
79+ WriteLn('Matriz dinámica no rectangular después de rellenar:');
80+ WriteLn('--------------------------------------------');
81+ MostrarMatriz2D(Matriz2D);
82+ Pausa;
83+end.
8584```
86-=== Salida ===
87-
85+
86+ === Salida ===
87+
8888 Matriz dinámica rectangular después de redimensionar
8989 -----------------------------------------
9090 Matriz[0]: 0 0 0
9191 Matriz[1]: 0 0 0
9292 Matriz[2]: 0 0 0
9393 Matriz[3]: 0 0 0
9494 Matriz[4]: 0 0 0
95-
95+
9696 ...
97-
97+
9898 Matriz dinámica rectangular después de rellenar:
9999 -----------------------------------------
100100 Matriz[0]: 0 1 2
101101 Matriz[1]: 10 11 12
102102 Matriz[2]: 20 21 22
103103 Matriz[3]: 30 31 32
104104 Matriz[4]: 40 41 42
105-
105+
106106 ...
107-
107+
108108 Matriz dinámica no rectangular después de redimensionar a una dimensión:
109109 ----------------------------------------------------------------
110110 Matriz[0]: 0 1 2
111111 Matriz[1]: 10 11 12
112112 Matriz[2]: 20 21 22
113113 Matriz[3]: 30 31 32
114114 Matriz[4]: 40 41 42
115115 Matriz[5]:
116116 Matriz[6]:
117117 Matriz[7]:
118118 Matriz[8]:
119119 Matriz[9]:
120-
120+
121121 ...
122-
122+
123123 Matriz dinámica no rectangular después de rellenar y redimensionar a una dimensión:
124124 ----------------------------------------------------------------------------
125125 Matriz[0]: 0 1 2 0 0 0 0 0 0
126126 Matriz[1]: 10 11 12 0 0 0 0
127127 Matriz[2]: 20 21 22 0 0 0
128128 Matriz[3]: 30 31 32 0 0 0 0 0 0
129129 Matriz[4]: 40 41 42 0 0
130130 Matriz[5]: 0 0 0 0 0 0 0 0
131131 Matriz[6]: 0 0 0 0 0
132132 Matriz[7]: 0 0 0
133133 Matriz[8]: 0
134134 Matriz[9]: 0 0 0 0 0 0 0
135-
135+
136136 ...
137-
137+
138138 Matriz dinámica no rectangular después de rellenar:
139139 --------------------------------------------
140140 Matriz[0]: 0 1 2 3 4 5 6 7 8
141141 Matriz[1]: 10 11 12 13 14 15 16
142142 Matriz[2]: 20 21 22 23 24 25
143143 Matriz[3]: 30 31 32 33 34 35 36 37 38
144144 Matriz[4]: 40 41 42 43 44
145145 Matriz[5]: 50 51 52 53 54 55 56 57
146146 Matriz[6]: 60 61 62 63 64
147147 Matriz[7]: 70 71 72
148148 Matriz[8]: 80
149149 Matriz[9]: 90 91 92 93 94 95 96
150-
150+
151151 ...
152-```
153152
154153## Ver también
155154
156- * [[Dynamic array|Matriz dinámica]]
157- * [Guía de referencia de Free Pascal: Matrices dinámicas](<http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1>)
155+- [[Dynamic array|Matriz dinámica]]
156+- [Guía de referencia de Free Pascal: Matrices dinámicas](http://www.freepascal.org/docs-html/current/ref/refsu15.html#x39-520003.3.1)
158157
159158---
160-*From the Free Pascal wiki, page "Example: Multidimensional dynamic array/es", as the Internet Archive kept it on 2024-11-25. 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).*
159+*From the Free Pascal wiki, page "Example: Multidimensional dynamic array/es", as it stood on 2022-03-09 after a change by Iskraelectrica, 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).*
160+Category: Examples

Back to history