There is a task:

In the rectangular matrix in each row, swap the first element of the line and the element containing the minimum number in the line.

Filling and output matrix, I did. Then I created another one-dimensional array into which I rewrote the strings.

Now I want to compare the elements of the lines between themselves. But something again went to the wrong steppe. How to make each line considered separately? I seem to be looking for the minimum value in the whole matrix ...

And even if I find the minimum value in each row, how to change the first element with the minimum in the line? Probably need some more variable ...

function min_v_strokah:integer; var i,j : integer; {peremennie dlya cikla} min : integer; {dlya poiska minimalnogo znacheniya} begin writeln; for i:=1 to a1 do begin for j:=1 to b1 do begin sravnenie[j]:=matr[i,j]; {perepisivaem stroki v massiv sravnenie[j]} if sravnenie[j] > matr[i,j] then begin matr[i,j] := min; end; end; writeln;writeln; end; end; 
  • one
    This code will never be executed: begin matr [i, j]: = min; end; Because of the assignment sravnenie[j]:=matr[i,j]; the sravnenie[j] > matr[i,j] condition will always be false. - andrybak
  • And why, as in one of the previous questions , rewrite the elements into a separate array? - andrybak
  • :) For some reason it was clearer to me :) - elenavictory

2 answers 2

 for i := 1 to a1 do begin minj := 1; for j := 2 to b1 do if matr[i, j] < matr[i, minj] then minj := j; tmp := matr[i, 1]; {tmp - переменная для обмена} matr[i, 1] := matr[i, minj]; matr[i, minj] := tmp; end; 

    To change the 1st with the minimum, you need to create a variable that will also store the index of the minimum element. Do not forget the check, because the 1st element itself may be minimal!

      element:integer; for i:=1 to a1 do begin min:=0;//если все ч-а положительные index:=0; for j:=1 to b1 do begin if matr[i,j]>min then min:=matr[i,j]; index:=j; end; element:=matr[i,1]; matr[i,1]:=min; matr[i,index]:=element; end; end; 
    • This code probably needs to be wrapped in a begin-end min: = matr [i, j]; index: = j; in if condition is wrong - looking for a maximum. - andrybak