An array is given, such as

0 0 1 1 1 0 1 1 1 

It is required to display the number of the row where the most zeros are. In the array can be present only ones or zeros. Here is my code, but the program does not work correctly. Help me to understand where I made a mistake.

 var i,j,n,m,max,l:integer; A:array [1..100,1..100] of integer; begin readln(n); readln(m); for i:=1 to n do begin for j:=1 to m do begin read(A[i,j]); if A[i,j]=0 then l:=l+1; if l > max then max:=i; end; end; writeln(max); end. 
  • I don’t quite understand how to implement clause 3, the task also says that there may be several such rows, where there is the same number of rows and you need to print the row number in order - Cog
  • and the array itself is where you? - Ale_x
  • @Ale_x, read(A[i,j]) not? - kot-da-vinci

2 answers 2

 var i, j, n, m: Integer; RowWithMaxZeroes: Integer; MaxZeroesInRow: Integer; ZeroesInRow: Integer; A: array [1..100, 1..100] of Integer; begin ReadLn(n); {Здесь нужно проверить, что n не больше 100 и что-то сделать, если оно больше} ReadLn(m); {Здесь нужно проверить, что m не больше 100 и что-то сделать, если оно больше} RowWithMaxZeroes := 0; MaxZeroesInRow := 0; for i := 1 to n do begin ZeroesInRow := 0; for j := 1 to m do begin Read(A[i, j]); if A[i, j] = 0 then ZeroesInRow := ZeroesInRow + 1; end; if ZeroesInRow > MaxZeroesInRow then begin MaxZeroesInRow := ZeroesInRow; RowWithMaxZeroes := i; end; end; WriteLn('Row with max zeroes: ', RowWithMaxZeroes); end. 

After entering m and n it would be good to check that they are not more than 100 :)

PS: l bad name for a variable, it is confused with i .

  • if max is the line number, why do you compare it with the number 0? - Grundy
  • @Grundy thanks for asking. This I copied a piece of code from the question without thinking. Corrected the code in the answer. Now flies apart from cutlets =) - kot-da-vinci
  • Yeah, I already saw it later, in the same question :) - Grundy
  1. Before passing through the row, you must reset the variable l
  2. Check whether the maximum number of l after passing in a row
  3. If this is the maximum, you need to remember the number of the series, then to display it