program MatrixSearch;
const
Rows = 4;
Cols = 5;
type
TMatrix = array[1..Rows, 1..Cols] of Integer;
procedure MSearch(matrix: TMatrix; len, target: Integer);
var
i, j: Integer;
begin
i := 1;
j := len;
while (i <= Rows) and (j >= 1) do
begin
if matrix[i, j] = target then
begin
WriteLn('Found: i = ', i, ' j = ', j);
Exit;
end;
if matrix[i, j] > target then
Dec(j)
else
Inc(i);
end;
WriteLn('Not found');
end;
var
matrix: TMatrix;
begin
matrix[1,1] := 2; matrix[1,2] := 3; matrix[1,3] := 5; matrix[1,4] := 7; matrix[1,5] := 8;
matrix[2,1] := 10; matrix[2,2] := 13; matrix[2,3] := 17; matrix[2,4] := 18; matrix[2,5] := 19;
matrix[3,1] := 25; matrix[3,2] := 26; matrix[3,3] := 30; matrix[3,4] := 37; matrix[3,5] := 38;
matrix[4,1] := 43; matrix[4,2] := 46; matrix[4,3] := 50; matrix[4,4] := 51; matrix[4,5] := 99;
MSearch(matrix, Cols, 37);
end.
(*
run:
Found: i = 3 j = 4
*)