Sign in Join
Wiki copytemporary

Example: Multidimensional dynamic array/es

English · Español · Français · 日本語

Seleccione Proyecto -> Nuevo proyecto -> Aplicación de consola, pegue el siguiente código y ejecútelo para obtener el resultado de salida.

Código

pascal
program EjemploMatricesDnamicas;

type
  TMatriz2D = Array of Array of Integer;

procedure  RellenaMatriz2D(Matriz: TMatriz2D);
var
  i, j: Integer;
begin
  for i:=Low(Matriz) to High(Matriz) do
    for j:=Low(Matriz[i]) to High(Matriz[i]) do
      Matriz[i][j] := i*10 + j;
end;

procedure  MostrarMatriz2D(Matriz: TMatriz2D);
var
  i, j: Integer;
begin
  for i:=Low(Matriz) to High(Matriz) do
  begin
    Write('Matriz[', i, ']:  ');
    for j:=Low(Matriz[i]) to High(Matriz[i]) do
      Write(Matriz[i][j]:2, '  ');
    WriteLn;
  end;
end;

procedure Pausa;
begin
  WriteLn;
  Write('...');
  ReadLn;
  WriteLn;
end;

var
  Matriz2D: TMatriz2D;

begin
  SetLength(Matriz2D, 5, 3);  // redimensionar a dos dimensiones
    WriteLn('Matriz dinámica rectangular después de redimensionar');
    WriteLn('-----------------------------------------');
    MostrarMatriz2D(Matriz2D);
    Pausa;

  RellenaMatriz2D(Matriz2D);
    WriteLn('Matriz dinámica rectangular después de rellenar:');
    WriteLn('-----------------------------------------');
    MostrarMatriz2D(Matriz2D);
    Pausa;

  SetLength(Matriz2D, 10);  // redimensionar a una dimensión
    WriteLn('Matriz dinámica no rectangular después de redimensionar a una dimensión:');
    WriteLn('----------------------------------------------------------------');
    MostrarMatriz2D(Matriz2D);
    Pausa;

  SetLength(Matriz2D[0], 9);
  SetLength(Matriz2D[1], 7);
  SetLength(Matriz2D[2], 6);
  SetLength(Matriz2D[3], 9);
  SetLength(Matriz2D[4], 5);
  SetLength(Matriz2D[5], 8);
  SetLength(Matriz2D[6], 5);
  SetLength(Matriz2D[7], 3);
  SetLength(Matriz2D[8], 1);
  SetLength(Matriz2D[9], 7);
    WriteLn('Matriz dinámica no rectangular después de rellenar y redimensionar a una dimensión:');
    WriteLn('----------------------------------------------------------------------------');
    MostrarMatriz2D(Matriz2D);
    Pausa;

  RellenaMatriz2D(Matriz2D);
    WriteLn('Matriz dinámica no rectangular después de rellenar:');
    WriteLn('--------------------------------------------');
    MostrarMatriz2D(Matriz2D);
    Pausa;
end.
pascal
=== Salida ===
pascal
Matriz din??mica rectangular despu??s de redimensionar
-----------------------------------------
Matriz[0]:   0   0   0  
Matriz[1]:   0   0   0  
Matriz[2]:   0   0   0  
Matriz[3]:   0   0   0  
Matriz[4]:   0   0   0
pascal
...
pascal
Matriz din??mica rectangular despu??s de rellenar:
-----------------------------------------
Matriz[0]:   0   1   2  
Matriz[1]:  10  11  12  
Matriz[2]:  20  21  22  
Matriz[3]:  30  31  32  
Matriz[4]:  40  41  42
pascal
...
pascal
Matriz din??mica no rectangular despu??s de redimensionar a una dimensi??n:
----------------------------------------------------------------
Matriz[0]:   0   1   2  
Matriz[1]:  10  11  12  
Matriz[2]:  20  21  22  
Matriz[3]:  30  31  32  
Matriz[4]:  40  41  42  
Matriz[5]:  
Matriz[6]:  
Matriz[7]:  
Matriz[8]:  
Matriz[9]:
pascal
...
pascal
Matriz din??mica no rectangular despu??s de rellenar y redimensionar a una dimensi??n:
----------------------------------------------------------------------------
Matriz[0]:   0   1   2   0   0   0   0   0   0  
Matriz[1]:  10  11  12   0   0   0   0  
Matriz[2]:  20  21  22   0   0   0  
Matriz[3]:  30  31  32   0   0   0   0   0   0  
Matriz[4]:  40  41  42   0   0  
Matriz[5]:   0   0   0   0   0   0   0   0  
Matriz[6]:   0   0   0   0   0  
Matriz[7]:   0   0   0  
Matriz[8]:   0  
Matriz[9]:   0   0   0   0   0   0   0
pascal
...
pascal
Matriz din??mica no rectangular despu??s de rellenar:
--------------------------------------------
Matriz[0]:   0   1   2   3   4   5   6   7   8  
Matriz[1]:  10  11  12  13  14  15  16  
Matriz[2]:  20  21  22  23  24  25  
Matriz[3]:  30  31  32  33  34  35  36  37  38  
Matriz[4]:  40  41  42  43  44  
Matriz[5]:  50  51  52  53  54  55  56  57  
Matriz[6]:  60  61  62  63  64  
Matriz[7]:  70  71  72  
Matriz[8]:  80  
Matriz[9]:  90  91  92  93  94  95  96
pascal
...

Ver también


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.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.