Sign in Join
Wiki copytemporary

Example: Multidimensional dynamic array/es: what changed

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

Title: Example: Multidimensional dynamic array/es

OldNewLine
1+Seleccione **Proyecto - > Nuevo proyecto -> Aplicación de consola**, pegue el siguiente código y ejecútelo para obtener el resultado de salida.
2+
3+### Código
4+```
5+program 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+```
85+```
86+=== Salida ===
87+
88+ Matriz dinámica rectangular después de redimensionar
89+ -----------------------------------------
90+ Matriz[0]: 0 0 0
91+ Matriz[1]: 0 0 0
92+ Matriz[2]: 0 0 0
93+ Matriz[3]: 0 0 0
94+ Matriz[4]: 0 0 0
95+
96+ ...
97+
98+ Matriz dinámica rectangular después de rellenar:
99+ -----------------------------------------
100+ Matriz[0]: 0 1 2
101+ Matriz[1]: 10 11 12
102+ Matriz[2]: 20 21 22
103+ Matriz[3]: 30 31 32
104+ Matriz[4]: 40 41 42
105+
106+ ...
107+
108+ Matriz dinámica no rectangular después de redimensionar a una dimensión:
109+ ----------------------------------------------------------------
110+ Matriz[0]: 0 1 2
111+ Matriz[1]: 10 11 12
112+ Matriz[2]: 20 21 22
113+ Matriz[3]: 30 31 32
114+ Matriz[4]: 40 41 42
115+ Matriz[5]:
116+ Matriz[6]:
117+ Matriz[7]:
118+ Matriz[8]:
119+ Matriz[9]:
120+
121+ ...
122+
123+ Matriz dinámica no rectangular después de rellenar y redimensionar a una dimensión:
124+ ----------------------------------------------------------------------------
125+ Matriz[0]: 0 1 2 0 0 0 0 0 0
126+ Matriz[1]: 10 11 12 0 0 0 0
127+ Matriz[2]: 20 21 22 0 0 0
128+ Matriz[3]: 30 31 32 0 0 0 0 0 0
129+ Matriz[4]: 40 41 42 0 0
130+ Matriz[5]: 0 0 0 0 0 0 0 0
131+ Matriz[6]: 0 0 0 0 0
132+ Matriz[7]: 0 0 0
133+ Matriz[8]: 0
134+ Matriz[9]: 0 0 0 0 0 0 0
135+
136+ ...
137+
138+ Matriz dinámica no rectangular después de rellenar:
139+ --------------------------------------------
140+ Matriz[0]: 0 1 2 3 4 5 6 7 8
141+ Matriz[1]: 10 11 12 13 14 15 16
142+ Matriz[2]: 20 21 22 23 24 25
143+ Matriz[3]: 30 31 32 33 34 35 36 37 38
144+ Matriz[4]: 40 41 42 43 44
145+ Matriz[5]: 50 51 52 53 54 55 56 57
146+ Matriz[6]: 60 61 62 63 64
147+ Matriz[7]: 70 71 72
148+ Matriz[8]: 80
149+ Matriz[9]: 90 91 92 93 94 95 96
150+
151+ ...
152+```
153+
154+## Ver también
155+
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>)
158+
159+---
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).*

Back to history