I have the following task: Determine the number of the column of a rectangular matrix in which the number of elements larger than M is minimal. In this case, you need to enter the parameters of the matrix from the keyboard, and fill it in randomly. Further, in the function you need to implement just the main task of the program - to compare the elements in the column, find how many elements in it are more than the variable M, do the same with other columns and compare finally, where the number of elements greater than M is minimal.

I managed to enter the matrix and output it to the screen, not without the help of users HashCode :) Here you can see if anyone is interested here

Now I won’t figure out how to make it possible to work with the elements of an array precisely by columns in a function, where exactly their comparison with the variable M by columns will take place.

I think that we need to add a one-dimensional array to which values ​​from the columns will be copied.

What do you say?

Here is the code that I get. So far, stalled on the function.

program zadanie; uses crt; const a = 10; {dlya zadaniya strok matricij} b = 10; {dlya zadaniya stolbcov matricij} type mass = array [1..a, 1..b] of integer; stolbcij_matricij = array [1..b] of integer; {dlya zapisi znachenij elementov stolbcov v otdelnyu matricy} var matr : mass; {dlya obsheij matricij} stolb: stolbcij_matricij; {dlya zapisi znachenij stolbcov} m : integer; {znachenie dlya sravneniya} a1,b1 : integer; {dlya opredeleniya parametrov matricij} {*********************************************************************} procedure vvod_matricij; {procedyra dlya vivoda matricij na ekran} var i,j:integer; begin writeln('Vvedite a: '); readln(a1); writeln('Vvedite b: '); readln(b1); randomize; for i:=1 to a do begin for j:=1 to b do begin matr[i,j]:=random(10); end; end; end; {*********************************************************************} procedure matrica_na_ekrane; var i,j : integer; begin for i:=1 to a1 do begin for j:=1 to b1 do begin write(matr[i,j]:5); end; writeln;writeln; end; end; {*********************************************************************} function stolbec:integer; var m : integer; {peremennaya dlya sravneniya} j : integer; {znacheniya elementov v stolbcah} kolichestvo : integer; {peremennaya dlya podscheta kolichestva chisel bolshih, chem m} begin write('Vvedite peremennyu M: '); readln(m); kolichestvo := 0; end; {*********************************************************************} BEGIN clrscr; vvod_matricij; matrica_na_ekrane; stolbec; readln; END. 

What to do next?

  • In the procedure vvod_matricij in cycles instead of a and b should be a1 and b1 , although this does not really affect the filling of the matrix. for i: = 1 to a1 do begin for j: = 1 to b1 do begin matr [i, j]: = random (10); end; end; - andrybak

2 answers 2

Why copy to a separate array?

 {НС Π·Π°Π±Ρ‹Ρ‚ΡŒ ΠΎΠ±Π½ΡƒΠ»ΠΈΡ‚ΡŒ массив `kol` Π² этом мСстС} for i := 1 to a do for j := 1 to b do if matr[i, j] > M then {элСмСнты ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ большиС M} inc(kol[j]); {посчитаСм Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ столбцС} {ΠΈ Π½Π°ΠΉΠ΄Π΅ΠΌ ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ} answer := 1; for j := 2 to b do if kol[j] < kol[answer] then answer := j; {answer - искомый Π½ΠΎΠΌΠ΅Ρ€ столбца} 

Without kol array

 answer := 1; min := maxlongint; for j := 1 to b do begin kol := 0; for i := 1 to a do if matr[i, j] > M then {элСмСнты ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ‹ большиС M} inc(kol); {посчитаСм} {ΠΈ Π½Π°ΠΉΠ΄Π΅ΠΌ ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ} if kol < min then begin answer := j; min := kol; end; end; 
  • Can I write comments in the code, if not difficult? and the fact that something I didn’t quite understand :) - elenavictory
  • @elenavictory updated the answer - andrybak
  • inc () is a function, did I understand correctly? - elenavictory
  • inc(i) same as i := i + 1 , but faster (in theory) - andrybak
  • one
    how I want to program well :) I ask everything and ask stupid questions ... I try :) {and in the sense of asking questions and in the sense of learning programming :)} - elenavictory

Without the kol array (here kol is just an integer):

  for j := 1 to b do begin {подсчитываСм Π² Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌ столбцС:} kol := 0; for i:=1 to a do if matr[i, j] > M then inc(kol); {Ссли столбСц ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ, Ρ‚ΠΎ считаСм, Ρ‡Ρ‚ΠΎ Π² Π½Π΅ΠΌ минимальноС количСство} {Π° Ссли Π½Π΅ ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ, Ρ‚ΠΎ сравниваСм Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ количСство с ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΌ количСством:} if (j=1) or (kol<minkol) then begin minkol := kol; answer := j; end; end; 
  • minkol you need to set the correct initial value, if it is a local variable in a function or procedure, then there is garbage, possibly negative. Or - andrybak
  • if (j = 1) or ... then begin minkol: = kol; - insolor
  • I admit, I was stupid - andrybak