There is a matrix (two-dimensional array), which is displayed in a StringGrid. It is necessary for each row of the matrix to determine the number of negative elements.

I tried to write like this myself, but gives the error "range check error":

procedure TForm1.N8Click(Sender: TObject); var d: integer; begin for i:=0 to m do //счет идет с ноля for j:=0 to n do x[i,j]:=StrToFloat(StringGrid1.Cells[j,i]); m:=strtoint(edit1.Text); n:=strtoint(edit2.Text); for i:=0 to m do begin d:=0; for j := 0 to n do if x[i,j]<0 then d:=d+1; StringGrid2.Cells[0,j]:= FormatFloat('0.',d); end; end; 

enter image description here

    2 answers 2

    What is m and n in the first cycle? All cycles must be up to n - 1 or m - 1 . Perhaps it would be right to do this:

     procedure TForm1.N8Click(Sender: TObject); var d: integer; begin m := StrToInt(edit1.Text); n := StrToInt(edit2.Text); for i := 0 to m - 1 do //счет идет с ноля for j := 0 to n - 1 do x[i,j] := StrToFloat(StringGrid1.Cells[j,i]); for i := 0 to m - 1 do begin d := 0; for j := 0 to n - 1 do if x[i,j] < 0 then d := d + 1; StringGrid2.Cells[0,j] := IntToStr(d); end; end; 

    And note that in edit1.Text and edit2.Text do not need to be just numbers. To check the correctness of the input, you can use the TryStrToInt function or use StrToIntDef .

      Apparently you need to write in the loop "for i: = 0 to m-1 do" (almost always in Delphi, the upper limit with "-1").

      Additive: you need to carefully monitor the boundaries of the cycles. If the lower bound is "0", then for m elements the upper bound will be "m-1" and you need to write "for i: = 0 to m-1 do"

      • "almost always in Delphi with" -1 "" This does not apply to Delphi, but applies to all languages ​​and even just enumerations of N elements. If we went through 0..N, then the number of enumerated elements would be N + 1 ;-) - Kromster