Привет. Помогите решить задачу.
Дана матрица размером 5*4. Поменять местами первую строку и строчку, в которой находится первый нулевой элемент.
Заранее спасибо!
i := 1; pos := 0;
while (i<=n) and (pos=0) do begin
j := 1;
while(j<=n) and (x[i,j]<>0) do inc(j);
if j<=n then pos := i;
inc(i);
end;
i := 1; j := 1;
while(i<=n) and (x[i,j]<>0) do begin
j := 1;
while(j<=m) and (x[i,j] <> 0) do
inc(j);
if x[i,j] <> 0 then
inc(i);
end;
pos := 0;
if i<=n then
pos := i;
function contains_zero(var mx: tmatrix; row: integer): boolean;
var
j: integer;
found: boolean;
begin
j := 1; found := false;
while (j <= size_col) and (not found) do begin
found := (mx[row][j] = 0); inc(j)
end;
contains_zero := found;
end;
...
{ Заполнение матрицы }
i := 1;
while (i <= size_row) do
if contains_zero(mx, i) then begin
swap_rows(mx, 1, i);
i := succ(size_row);
end
else inc(i);
{ вывод результата }
...
before:
3 10 7 12
14 5 8 14
3 6 0 13
3 1 10 14
0 6 3 9
after:
3 6 0 13
14 5 8 14
3 10 7 12
3 1 10 14
0 6 3 9
program seven_two;
uses crt;
const
size_row = 5;
size_col = 4;
type
tvector = array[1 .. size_col] of integer;
tmatrix = array[1 .. size_row] of tvector;
procedure swap_rows(var mx: tmatrix;
const i, j: integer);
var T: tvector;
begin
T := mx[i]; mx[i] := mx[j]; mx[j] := T
end;
procedure print(var mx: tmatrix);
var i, j: integer;
begin
for i := 1 to size_row do
begin
for j := 1 to size_col do
write(mx[i][j]:4);
writeln
end;
end;
function contains_zero(var mx: tmatrix; row: integer): boolean;
var
j:integer;
found: boolean;
begin
j:=1; found:=false;
while (j<=size_col) and (not found) do begin
found:= (mx[row][j]=0); inc(j);
end;
contains_zero:=found;
end;
var
mx: tmatrix;
i, j: integer;
begin
clrscr;
for i := 1 to size_row do
for j := 1 to size_col do
mx[i][j] := random(15);
writeln('before:'); print(mx);
i:=1;
while (i <= size_row) do
if contains_zero(mx, i) then begin
swap_rows(mx, 1, i);
i := succ(size_row);
end
else inc(i);
writeln('after:'); print(mx);
readln;
end.