
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.
Get two integers from the user, then create a two-dimensional array where the two dimensions have the sizes given by those numbers, and which can be accessed in the most natural way possible. Write some element of that array, and then output that element. Finally destroy the array if not done by the language itself.

program Project1;
 
{$APPTYPE CONSOLE}
 
uses
  SysUtils;
var
  matrix:array of array of Byte;
  i,j:Integer;
begin
  Randomize;
  //Finalization is not required in this case, but you have to do
  //so when reusing the variable in scope
  Finalize(matrix);
  //Init first dimension with random size from 1..10
  //Remember dynamic arrays are indexed from 0
  SetLength(matrix,Random(10) + 1);
  //Init 2nd dimension with random sizes too
  for i := Low(matrix) to High(matrix) do
    SetLength(matrix[i],Random(10) + 1);
 
  //End of code, the following part is just output
  Writeln(Format('Total amount of columns = %.2d',[Length(matrix)]));
  for i := Low(matrix) to High(matrix) do
    Writeln(Format('Column %.2d = %.2d rows',[i,Length(matrix[i])]));
 
  Readln;
end.