// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements
program ToeplitzMatrixProgram;
type
TArray2D = array[0..4, 0..3] of Integer;
function isToeplitz(matrix: TArray2D): Boolean;
var
rows, cols, i, j: Integer;
begin
rows := High(matrix);
cols := High(matrix[0]);
for i := 1 to rows do
begin
for j := 1 to cols do
begin
WriteLn(i, ' ', j, ' - ', matrix[i][j], ' : ', matrix[i - 1][j - 1]);
if matrix[i][j] <> matrix[i - 1][j - 1] then
begin
Exit(False);
end;
end;
WriteLn('-----------');
end;
isToeplitz := True;
end;
var
matrix: TArray2D = (
(2, 7, 9, 8),
(4, 2, 7, 9),
(3, 4, 2, 7),
(0, 3, 4, 2),
(6, 0, 3, 4)
);
begin
if isToeplitz(matrix) then
begin
WriteLn('Matrix is a Toeplitz');
end
else
begin
WriteLn('Matrix is not a Toeplitz');
end;
end.
(*
run:
1 1 - 2 : 2
1 2 - 7 : 7
1 3 - 9 : 9
-----------
2 1 - 4 : 4
2 2 - 2 : 2
2 3 - 7 : 7
-----------
3 1 - 3 : 3
3 2 - 4 : 4
3 3 - 2 : 2
-----------
4 1 - 0 : 0
4 2 - 3 : 3
4 3 - 4 : 4
-----------
Matrix is a Toeplitz
*)