Usuwanie kolumny i wiersza macierzy

0

Witam mam za zadanie usunąć i-ty i j-ty wiersz i kolumnę macierzy NxN. Kompletnie nie znam się na programowaniu ale szperając po internecie posklejałem jak do tej pory coś takiego:

program MACIERZGENEROWANIE;

var
  t : array of array of integer;
  t1 : array of array of integer;
  a, b, n, i, j : integer;
begin
  randomize;
  writeln('Podaj liczbę n');
  readln(n);
  SetLength(t, n, n);
  for i := 0 to n - 1 do
  begin
    for j := 0 to n - 1 do
    begin
      t[i, j] := random(100);
      Write(t[i, j]: 3);
    end;
    writeln;
  end;
  begin
    for a:=1 to n-2 do
    for b:=1 to n-2 do
    t[i,j]:=t1[a+ord(a>=i),b+ord(b>=j)];
    write(t[i, j]: 3);
  end;
  writeln;
  end;
  readln;
end.

Za cholerę nie wiem co dalej a muszę zrobić to na zaliczenie na 18:30.... :/

zamiana znacznika <quote> na <code class="delphi"> - @furious programming

0

Nie do końca nie zrobiłem nic. Uwierz, że jestem w tym zielony i przeszukałem tysiąc tematów, żeby w ogóle program generował macierz o rozmiarze n na n.

0

Przecież już podawałem ci rozwiązanie - ba to jeden wiersz!

0

Tak, ale naprawdę w ogóle się na tym nie znam pascalu. Studiuję matematykę nie informatykę. Nie wiem nawet jak tę linijkę wtrącić i gdzie :/

0

Naprawdę bardzo proszę o pomoc. Zostały mi 2 godziny na zrobienie tego zadania a robię je już drugi dzień....... Będę dozgonnie wdzięczny.

0

@Maraspp93 - usunięcie wiersza czy kolumny z macierzy to nic innego jak przesunięcie elementów po wierszu/kolumnie o jedno miejsce bliżej jej początku i zmniejszenie rozmiaru danego wymiaru także o jeden; Aby to wykonać, można pętelkami przepisać liczby, a na koniec wystarczy użyć SetLength z określonym nowym rozmiarem.

0

Dzięki za próbę pomocy, ale widocznie jestem za głupi żeby to zrozumieć. Pójdę na zajęcia i poproszę o możliwość zaliczenia w innym terminie :/

1

Zobacz więc na poniższy przykład i spróbuj go zrozumieć - ewentualnie pytaj, jeśli czegoś nie rozumiesz:

program Dyn2DArray;

{
  LICENCJA

  Kompilacja kodu tego programu spowoduje automatyczne przelanie
  kwoty 50zł z Twojego konta bankowego na konto autora kodu;

  Skopiowanie kodu z posta oznacza wyrażenie zgody na powyższe
  warunki i odstąpienie od wszelkich roszczeń;
}

{$APPTYPE CONSOLE}

type
  TDyn2DIntArr = array of array of Integer;

  procedure SetArraySize(var AArray: TDyn2DIntArr; ASize: Integer);
  begin
    SetLength(AArray, ASize, ASize);
  end;

  procedure FillArray(var AArray: TDyn2DIntArr);
  var
    intCol, intRow: Integer;
  begin
    for intCol := Low(AArray) to High(AArray) do
      for intRow := Low(AArray[0]) to High(AArray[0]) do
        AArray[intCol, intRow] := Random(10);
  end;

  procedure ShowArray(AArray: TDyn2DIntArr);
  var
    intCol, intRow: Integer;
  begin
    WriteLn;

    for intCol := Low(AArray) to High(AArray) do
    begin
      for intRow := Low(AArray[0]) to High(AArray[0]) do
        Write(AArray[intCol, intRow]:2);

      WriteLn;
    end;

    WriteLn;
  end;

  procedure RemoveColumn(var AArray: TDyn2DIntArr; AIndex: Integer);
  var
    intCol, intRow: Integer;
  begin
    for intCol := Low(AArray) to High(AArray) do
      for intRow := AIndex + 1 to High(AArray[0]) do
        AArray[intCol, intRow - 1] := AArray[intCol, intRow];

    SetLength(AArray, Length(AArray), High(AArray[0]));
  end;

  procedure RemoveRow(var AArray: TDyn2DIntArr; AIndex: Integer);
  var
    intCol, intRow: Integer;
  begin
    for intRow := Low(AArray[0]) to High(AArray[0]) do
      for intCol := AIndex + 1 to High(AArray) do
        AArray[intCol - 1, intRow] := AArray[intCol, intRow];

    SetLength(AArray, High(AArray), Length(AArray[0]));
  end;

var
  arrNums: TDyn2DIntArr;
  intSize, intRemove: Integer;
begin
  Randomize();
  try
    Write('Type the size of array: ');
    ReadLn(intSize);
    SetArraySize(arrNums, intSize);
    FillArray(arrNums);
    ShowArray(arrNums);

    Write('Type the index of column to remove: ');
    ReadLn(intRemove);
    RemoveColumn(arrNums, intRemove);
    ShowArray(arrNums);

    Write('Type the index of row to remove: ');
    ReadLn(intRemove);
    RemoveRow(arrNums, intRemove);
    ShowArray(arrNums);
  finally
    Write('Press Enter to exit...');
    ReadLn;
  end;
end.

Przykładowe wyjście:

Type the size of array: 4

 7 9 9 9
 9 2 4 5
 7 1 8 1
 9 2 3 1

Type the index of column to remove: 2

 7 9 9
 9 2 5
 7 1 1
 9 2 1

Type the index of row to remove: 3

 7 9 9
 9 2 5
 7 1 1

Press Enter to exit...

Program nie posiada zabezpieczeń przed wyjściem poza zakres.

0

przykro mi ze za pozno ale bylem poza domem

#include <iostream.h>
 
void main()
 
{
 
int tabela[20][20];
 
int zmienna;
 
int a,b,i,j;
 
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
cin >> zmienna;
tabela[i][j]=zmienna;
}


for (a=0;a<3;a++) 

{

tabela[a][a]=0; // usuwanie przekatnej

tabela[0][a]=0; // usuwanie wiersza



} 
0

Wg mnie chodziło o coś takiego:

var tb:array of array of integer;
var n,y,x,ry,rx:integer;
begin
  Randomize;
  Write('Podaj liczbe n: ');
  ReadLn(n);
  SetLength(tb,n,n);
  for y:=0 to n-1 do for x:=0 to n-1 do tb[y,x]:=Random(100);
  while n>0 do
  begin
    WriteLn('Aktualny rozmiar: ',n);
    for y:=0 to n-1 do
    begin
      for x:=0 to n-1 do Write(tb[y,x]:3);
      WriteLn;
    end;
    WriteLn;
    ry:=random(n);
    rx:=random(n);
    WriteLn('Usuwamy wiersz ',ry,' i kolumne ',rx);
    for y:=0 to n-2 do for x:=0 to n-2 do tb[y,x]:=tb[y+ord(y>=ry),x+ord(x>=rx)];
    Dec(n);
    SetLength(tb,n,n);
  end;
  //ReadLn;
end.

http://ideone.com/V8wtgy

1 użytkowników online, w tym zalogowanych: 0, gości: 1