G
G
gleendo2015-02-23 20:13:56
Delphi
gleendo, 2015-02-23 20:13:56

How to find the sum of the elements of a row in a matrix?

In general, there is a program. I enter the number of rows and columns, randomly fill in the table (matrix). I press "sum", and in the zero cells the sum of each line should be displayed, but nonsense comes out ...
6290c68d7707460faeb4e18ceb8f3a55.png
Tell me what's wrong.
Here is the procedure code:

var
  i, j, countCols, countRows, result1: integer;
  result : array[1..100] of integer;
begin
  countCols := StrToInt(Form1.Edit2.text);
  countRows := StrToInt(Form1.Edit1.text);
  Form1.Label3.Caption := IntToStr(countCols) + ' ' + IntToStr(countRows);
    for i := 1 to countRows do
    begin
      for j := 1 to countCols do
        begin
          result[i] := result[i] + StrToInt(StringGrid1.Cells[j, i]);
        end;
      StringGrid1.Cells[0, i] := IntToStr(result[i]);
    end;
end;

Decision:
var
  i, j, countCols, countRows, sum: integer;
  sumArr: array[1..50] of integer;
begin

  countCols := StrToInt(Form1.Edit2.text);
  countRows := StrToInt(Form1.Edit1.text);
  Form1.Label3.Caption := IntToStr(countCols) + ' ' + IntToStr(countRows);
    for i := 1 to countRows do
      begin
      sum := 0;
        for j := 1 to countCols do
          begin
            sum := sum + StrToInt(StringGrid1.Cells[j, i]);
          end;
        sumArr[i] := sum;
        StringGrid1.Cells[0, i] := IntToStr(sumArr[i]);
      end;

    for j := 1 to countCols do
      begin
      sum := 0;
        for i := 1 to countRows do
          begin
            sum := sum + StrToInt(StringGrid1.Cells[j, i]);
          end;
        sumArr[i] := sum;
        StringGrid1.Cells[j, 0] := IntToStr(sumArr[i]);
      end;
end;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question