Показать сообщение отдельно
  #1  
Старый 06.01.2010, 19:51
Аватар для Lotles
Lotles Lotles вне форума
Прохожий
 
Регистрация: 05.09.2009
Адрес: г. Новокузнецк
Сообщения: 46
Репутация: -52
По умолчанию сумму положительных и отрицательных чисел матрицы

Здравствуйте !!!
Этот код считает сумму положительных и отрицательных чисел матрицы. Подскажите, почему у меня в сообщении ShowMessage сумма положительных и отрицательных чисел имеют огромные значения ?

Код:
type
  TIntMatrix = array of array of Integer;
 
function ReadMatrix(Grid: TStringGrid): TIntMatrix;
var
  r, c: Integer;
  Matrix: TIntMatrix;
begin
  SetLength(Matrix, StringGrid1.RowCount, Grid.ColCount);
  for r := 0 to StringGrid1.RowCount - 1 do
    for c := 0 to Grid.ColCount - 1 do
      Matrix[r,c] := StrToInt(StringGrid1.Cells[c,r]);
  Result := Matrix;
end;
 
procedure GetSum(Matrix: TIntMatrix; SumOtr, SumPol: Integer); 
var
  I, J: Integer;
begin
  SumOtr := 0;
  SumPol := 0;
  for I := 0 to High(Matrix) do
    for J := 0 to High(Matrix) do
      if Matrix[I,J] > 0 then
        SumPol := SumPol + Matrix[I,J]
      else
        if Matrix[I,J] < 0 then SumOtr := SumOtr + Matrix[I,J];
end;
 
 
 
procedure TMainForm.Button1Click(Sender: TObject);
var
  Matrica: TIntMatrix;
  SumOtr, SumPol: Integer;
begin
  Matrica := ReadMatrix(InputStringGrid);
  GetSum(Matrica, SumOtr, SumPol);
  ShowMessage('Сумма положительных SomPol = ' + IntToStr(SumPol) + '. Сумма отрицательных SumOtr = ' + IntToStr(SumOtr) + '.');
end;
 
end.
__________________
Skype: Lotles-XXXX
Ответить с цитированием