evgeniy8705
@evgeniy8705
Повелитель вселенной

Как найти сумму элементов строки в матрице?

В общем есть программа. Ввожу количество строк и столбцов, рандомом заполняю таблицу(матрицу). Нажимаю "сумма", и в нулевых ячейках должна выводиться сумма каждой строки, но выходит ерунда...
6290c68d7707460faeb4e18ceb8f3a55.png
Подскажите в чем ошибка.
Вот код процедуры:
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;


Решение:
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;
  • Вопрос задан
  • 4886 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы