Технический форум

Технический форум (http://www.tehnari.ru/)
-   Delphi, Kylix and Pascal (http://www.tehnari.ru/f43/)
-   -   Решение двумерных массивов с помощью процедур и функций (http://www.tehnari.ru/f43/t75492/)

Jenya_Miller 28.06.2012 15:02

Решение двумерных массивов с помощью процедур и функций
 
Помогите пожалуйста с решением 3 задач на двумерные массивы с помощью процедур и функций у меня есть правильное решение но у меня не получается сделать эти задачи с помощью процедур и функций.
Вот условие задач:
1 Дан целочисленный массив В[1..5, 1..5]. Вычислить сумму элементов этого массива, расположенных выше левой диагонали.
2 Задан массив А[1..5,1..6]. Поменять в нем местами первый и последний столбец.
3 Составьте программу циклической перестановки столбцов двумерного массива К, при которой i-й столбец i+1-м, а последний столбец становится первым.

Вот листинг программ:

1
Код:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Grids, StdCtrls, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    StringGrid1: TStringGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
const n=5;
var b:array[1..n,1..n] of integer;
i,j:integer;
begin
    randomize;
    for  i:=1 to n do
    begin
    for j:=1 to n do
                  begin
                    b[i,j]:=random(10);
                    form1.StringGrid1.Cells[i-1,j-1]:=inttostr(b[i,j]);
                  end;
end;
form1.Button3.Enabled:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
form1.Button3.Enabled:=false;
form1.Label3.Caption:='';
form1.StringGrid1.Clean;
end;

procedure TForm1.Button3Click(Sender: TObject);
const n=5;
var b:array[1..n,1..n] of integer;
i,j:integer;
p:real;
begin
for i:=1 to n do
 for j:=1 to n do
b[i,j]:=strtoint(form1.StringGrid1.Cells[i-1,j-1]);
p:=0;
for i:=1 to n do
    begin

    for j:=1 to n do

                  begin
                    if i>j then
                    p:=p+b[i,j];
                    end;

end;
form1.Label3.Caption:=floattostr(p);
end;

initialization
  {$I unit1.lrs}

end.


2
Код:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Spin, Grids;

type

{ TForm1 }

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label3: TLabel;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);

private
{ private declarations }
public
{ public declarations }
end;

var
Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.Button3Click(Sender: TObject);
begin
form1.StringGrid1.Clean;
form1.Button2.Enabled:=false;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
a:array[1..5,1..6] of integer;
i,j:integer;

begin
randomize;
For i:=1 to 5 do
For j:=1 to 6 do
begin

a[i,j]:=random(100);
form1.StringGrid1.Cells[i-1,j-1]:=inttostr(a[i,j]);
end;
form1.Button2.Enabled:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
var a:array[1..5,1..6] of integer; i,j,k:integer;
begin
for i:=1 to 5 do
  for j:= 1 to 6 do
    a[i,j]:=strtoint(form1.StringGrid1.Cells[i-1,j-1]);
for i:=1 to 5 do
begin
k:=a[i,1];
a[i,1]:=a[i,6];
a[i,6]:=k;
end;
for i:=1 to 5 do
for j:= 1 to 6 do
form1.StringGrid2.Cells[i-1,j-1]:=inttostr(a[i,j]);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
form1.StringGrid2.Clean;
form1.Button2.Enabled:=false;
end;



initialization
{$I unit1.lrs}

end.


3
Код:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Grids, StdCtrls, Spin;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SpinEdit1: TSpinEdit;
    SpinEdit2: TSpinEdit;
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure SpinEdit2Change(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

var a:array of array of integer;
    m,n:integer;
{ TForm1 }

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
  form1.StringGrid1.ColCount:=form1.SpinEdit1.Value;
  form1.StringGrid2.ColCount:=form1.SpinEdit1.Value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;

begin
randomize;
m:=SpinEdit2.Value;
n:=spinEdit1.Value;
Setlength(a,m,n);
StringGrid1.ColCount:=n;
StringGrid1.RowCount:=m;
StringGrid2.ColCount:=n;
StringGrid2.RowCount:=m;
for i:=0 to m-1 do
for j:=0 to n-1 do
 begin
  a[i,j]:=-50+random(100);
  StringGrid1.Cells[j,i]:=inttostr(a[i,j]);
 end;
Button2.Enabled:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
var b:array of integer;
    i,j:integer;
begin
 Setlength(b,m);
 for i:=0 to m-1 do
 b[i]:=a[i,0];
 for j:=0 to n-2 do
 for i:=0 to m-1 do
 a[i,j]:=a[i,j+1];
 for i:=0 to m-1 do
 a[i,n-1]:=b[i];
 for i:=0 to m-1 do
 for j:=0 to n-1 do
 StringGrid2.Cells[j,i]:=inttostr(a[i,j]);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  form1.StringGrid1.Clean;
  form1.Button2.Enabled:=false;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  form1.StringGrid2.Clean;
  form1.Button2.Enabled:=false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    form1.StringGrid1.ColCount:=form1.SpinEdit1.Value;
    form1.StringGrid1.RowCount:=form1.SpinEdit2.Value;
    form1.StringGrid2.ColCount:=form1.SpinEdit1.Value;
    form1.StringGrid2.RowCount:=form1.SpinEdit2.Value;
end;

procedure TForm1.SpinEdit2Change(Sender: TObject);
begin
  form1.StringGrid1.RowCount:=form1.SpinEdit2.Value;
  form1.StringGrid2.RowCount:=form1.SpinEdit2.Value;
end;

initialization
  {$I unit1.lrs}

end.

Помогите пожалуйста

Jenya_Miller 28.06.2012 15:05

язык freepascal


Часовой пояс GMT +4, время: 00:16.

Powered by vBulletin® Version 4.5.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.