Проблемы с процедурой
Есть задание (вложенный файл)
По нему есть программа с процедурой, которая переносит неправильные(начальные) значения p[i] из процедуры вместо конечных. При этом в программе без отдельной процедуры все работает. Хотелось бы узнать в чем ошибка и как ее исправить.
Код с отдельной процедурой
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Math;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TA = array of array of integer;
var
Form1: TForm1;
a : TA;
i,j : integer;
p : array[1..5] of integer;
implementation
{$R *.dfm}
procedure one( p : array of integer; var a : TA);
var
i,j:integer;
begin
p[1]:=1;
p[2]:=1;
p[3]:=1;
p[4]:=1;
p[5]:=1;
for i :=1 to 5 do
for j :=1 to 4 do
if a[i-1,j-1]<0 then
if i=1 then p[1]:=p[1]*a[i,j];
if i=2 then p[2]:=p[2]*a[i,j];
if i=3 then p[3]:=p[3]*a[i,j];
if i=4 then p[4]:=p[4]*a[i,j];
if i=5 then p[5]:=p[5]*a[i,j];
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,Y:integer;
begin
SetLength(a,5,4);
for i :=1 to 5 do
for j :=1 to 4 do
if Length(StringGrid1.Cells[i-1,j-1])<>0
then a[i-1,j-1] := StrToInt(StringGrid1.Cells[i-1,j-1])
else a[i-1,j-1] := 0;
Y:=0;
one(p,a);
for i :=1 to 5 do
begin
for j :=1 to 4 do
if p[i]=1 then if a[i,j]<>-1 then p[i]:=0;
Y:=Y+sqr((6-i)-p[i]);
end;
Edit1.Text:=IntToStr(Y);
for i :=1 to 5 do
for j :=1 to 4 do
StringGrid1.Cells[i-1,j-1]:=IntToStr(a[i-1,j-1]);
end;
end.
Без нее.
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
a : array[1..5,1..4] of integer;
i,j,Y : integer;
p : array[1..5] of integer;
begin
for i :=1 to 5 do
for j :=1 to 4 do
if Length(StringGrid1.Cells[i-1,j-1])<>0
then a[i,j] := StrToInt(StringGrid1.Cells[i-1,j-1])
else a[i,j] := 0;
p[1]:=1;
p[2]:=1;
p[3]:=1;
p[4]:=1;
p[5]:=1;
for i :=1 to 5 do
for j :=1 to 4 do
if a[i,j]<0 then
if i=1 then p[1]:=p[1]*a[i,j];
if i=2 then p[2]:=p[2]*a[i,j];
if i=3 then p[3]:=p[3]*a[i,j];
if i=4 then p[4]:=p[4]*a[i,j];
if i=5 then p[5]:=p[5]*a[i,j];
Y:=0;
for i :=1 to 5 do
begin
for j :=1 to 4 do
if p[i]=1 then if a[i,j]<>-1 then p[i]:=0;
Y:=Y+sqr((6-i)-p[i]);
end;
Edit1.Text:=IntToStr(Y);
end;
end.
|