program TransposeMatrix;
{$mode objfpc}{$H+}
type
TMatrix = array of array of Integer;
function Transpose(const M: TMatrix): TMatrix;
var
rows, cols, i, j: Integer;
R: TMatrix; // local result
begin
rows := Length(M);
cols := Length(M[0]);
SetLength(R, cols, rows);
for i := 0 to rows - 1 do
for j := 0 to cols - 1 do
R[j][i] := M[i][j];
Result := R;
end;
procedure PrintMatrix(const M: TMatrix);
var
i, j: Integer;
begin
for i := 0 to High(M) do
begin
for j := 0 to High(M[i]) do
Write(M[i][j], ' ');
Writeln;
end;
end;
var
Matrix, T: TMatrix;
begin
Matrix := [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
T := Transpose(Matrix);
PrintMatrix(T);
end.
(*
run:
1 4 7
2 5 8
3 6 9
*)