Top.Mail.Ru
Ответы

ПОМОГИТЕ Создать динамические массивы на паскаль

используя указатели. Дан двумерный массив размером (n⋅m). Заполнить одномерный массив, найдя произведение положительных элементов в каждом столбце матрицы.

По дате
По рейтингу
Аватар пользователя
1мес
Изменено
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
type
  PPInteger = ^PInteger;
  PInteger  = ^Integer;

function GetIntValue(APtr: PInteger; AInd: Integer): Integer;
begin
  Inc(APtr, AInd);
  GetIntValue := APtr^;
end;

procedure SetIntValue(APtr: PInteger; AInd: Integer; AValue: Integer);
begin
  Inc(APtr, AInd);
  APtr^ := AValue;
end;

function GetMatrixCols(APtr: PPInteger; AInd: Integer): PInteger;
begin
  Inc(APtr, AInd);
  GetMatrixCols := APtr^;
end;

procedure SetMatrixCols(APtr: PPInteger; AInd: Integer; AValue: PInteger);
begin
  Inc(APtr, AInd);
  APtr^ := AValue;
end;

var
  LMatrixRows: PPInteger;
  LMatrixCols, LArray, LPtr: PInteger;
  n, m, i, j, k: Integer;

begin
  Randomize;
  Readln(n, m);

  GetMem(LMatrixRows, SizeOf(PInteger) * n);
  for i := 0 to n - 1 do
  begin
    GetMem(LMatrixCols, SizeOf(Integer) * m);
    SetMatrixCols(LMatrixRows, i, LMatrixCols);
    for j := 0 to m - 1 do
      SetIntValue(LMatrixCols, j, Random(21) - 10);
  end;

  GetMem(LArray, SizeOf(Integer) * m);
  for i := 0 to m - 1 do
  begin
    LPtr := LArray;
    Inc(LPtr, i);
    LPtr^ := 0;
    for j := 0 to n - 1 do
    begin
      k := GetIntValue(GetMatrixCols(LMatrixRows, j), i);
      if k > 0 then
      begin
        if LPtr^ = 0 then LPtr^ := 1;
        LPtr^ := LPtr^ * k;
      end;
    end;
  end;

  for i := 0 to n - 1 do
  begin
    for j := 0 to m - 1 do
      Write(GetIntValue(GetMatrixCols(LMatrixRows, i), j):4);
    Writeln;
  end;
  Writeln;
  for i := 0 to m - 1 do Writeln(GetIntValue(LArray, i));

  for i := 0 to n - 1 do FreeMem(GetMatrixCols(LMatrixRows, i));
  FreeMem(LMatrixRows);
  FreeMem(LArray);
end.
Аватар пользователя
Искусственный Интеллект
1мес

Динамические массивы на сермяжном паскале создаются при помощи функции GetMem. Одномерные массивы создаются с полпинка, пример двумерного:

function AllocateIntMatrix(n, m: Integer): Pointer;

var

a: Pointer;

i: Integer;

begin

a := GetMem(m * SizeOf(Pointer));

for i := 0 to m - 1 do

a^[i] := GetMem(n * SizeOf(Integer));

AllocateIntMatrix := a;

end;