program ColumnsUniqueNumbers;
const
Rows = 3;
Cols = 3;
type
TMatrix = array[1..Rows, 1..Cols] of Integer;
function ColumnsHaveUniqueNumbers(const matrix: TMatrix; rows, cols: Integer): Boolean;
var
j, i, k: Integer;
columnSet: array of Integer;
found: Boolean;
begin
if (rows = 0) or (cols = 0) then
Exit(True);
for j := 1 to cols do // 1-based indexing
begin
SetLength(columnSet, 0); // Reset columnSet for each column
for i := 1 to rows do // 1-based indexing
begin
found := False;
for k := 0 to High(columnSet) do
begin
if columnSet[k] = matrix[i, j] then
begin
found := True;
Break;
end;
end;
if found then
Exit(False);
SetLength(columnSet, Length(columnSet) + 1);
columnSet[High(columnSet)] := matrix[i, j];
end;
end;
Exit(True);
end;
var
matrix1: TMatrix = (
(1, 4, 7),
(2, 5, 8),
(3, 6, 9)
);
matrix2: TMatrix = (
(1, 4, 7),
(2, 4, 8),
(3, 6, 9)
);
begin
WriteLn('Matrix 1 columns have unique numbers: ', ColumnsHaveUniqueNumbers(matrix1, Rows, Cols));
WriteLn('Matrix 2 columns have unique numbers: ', ColumnsHaveUniqueNumbers(matrix2, Rows, Cols));
end.
(*
run:
Matrix 1 columns have unique numbers: TRUE
Matrix 2 columns have unique numbers: FALSE
*)