unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids,Mxarrays;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Button2: TButton;
    StringGrid2: TStringGrid;
    Button3: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
Nmax=10;
Type
Mas1 = array[1..Nmax,1..Nmax] of integer;
mas2 = array[1..Nmax] of integer;

var
Form1: TForm1;
A : Mas1;
S: mas2;
N, M, i, j : integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 N:=3;       // Число строк в массиве
 M:=3;       // Число столбцов в массиве
      Edit1.Text:=FloatToStr(N);
      Edit2.Text:=FloatToStr(M);
   StringGrid1.RowCount:=N+1;
   StringGrid1.ColCount:=M+1;
   StringGrid2.RowCount:=N+1;
   StringGrid2.ColCount:=2;

   StringGrid1.Cells[0,0]:='Массив A';
   StringGrid2.Cells[0,0]:='Массив S';
   for i:=1 to N do begin
    StringGrid1.Cells[0,i]:='i= '+IntToStr(i);
    StringGrid1.Cells[i,0]:='j= '+IntToStr(i);
    StringGrid2.Cells[0,i]:='j= '+IntToStr(i);

                    end;


end;

procedure TForm1.Button1Click(Sender: TObject);
begin
randomize;
N:=StrToint(Edit1.Text);
M:=StrToint(Edit2.Text);

StringGrid1.RowCount:=N+1;
StringGrid1.ColCount:=M+1;
StringGrid2.RowCount:=N+1;
StringGrid2.ColCount:=2;
for i:=1 to N do StringGrid1.Cells[0,i]:='i= ' +inttostr(i);
for i:=1 to M do StringGrid1.Cells[i,0]:='j= ' +inttostr(i);
for i:=1 to N do
for j:=1 to M do begin
A[i,j]:=random(10);StringGrid1.Cells[j,i]:=Inttostr(A[i,j]);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var k,y,l,c:integer ;

begin
for j:=1 to N do begin
C:=0;
for i:=1 to M do begin C:=C+ strToint(StringGrid1.Cells[i,j]); end;
StringGrid2.Cells[1,j]:= intTostr(c) ;

end;
end;



procedure TForm1.Button3Click(Sender: TObject);
var k,y,l,c:integer ;
begin
for i:=1 to N do begin
      s[i]:= strtoint(StringGrid2.Cells[1,i]) ;
end;
repeat
l:=0;
for i:=1 to N do   begin

  if S[i]>S[i+1] then begin

   K:=S[i];
   s[i]:=s[i+1];
   s[i+1]:=k;
   l:=l+1;

   For j:=1 to M do   begin
   y:=A[i,j];
   A[i,j]:=A[i+1,j];
   A[i+1,j]:=y;
   stringgrid1.Cells[j,i]:=inttostr(A[i,j]) ;
   stringgrid1.Cells[j,i]:=inttostr(A[i+1,j]) ;
   end;

  end;
  StringGrid2.Cells[1,i]:=inttostr(s[i]);



 end;
until l<=0 ;
end;

end.


