Arsip: Array dan Matrix, Error....

 
user image
more 10 years ago

iyus.jr

agan-agan..tolongin dong.. sy buat program perkalian matrix pakai array...di delphi nih tp error..katanya read memory 0x00000

procedure TForm1.btn1Click(Sender: TObject);
var dim : Integer;
begin
dim := StrToInt(Edit1.Text);
StringGrid1.ColCount := dim;
StringGrid1.RowCount := dim;
StringGrid3.ColCount := dim;
StringGrid3.RowCount := dim;
end;

procedure TForm1.Button1Click(Sender: TObject);
var x,y,w : byte;
dim : Integer;
arr1 : array of array of double;
arr2 : array of array of double;
arrhasil : array of array of double;
begin
dim := StrToInt(Edit1.Text);
SetLength(arr1,dim);
SetLength(arr2,dim);
SetLength(arrhasil,dim);
  for y := 0 to dim do
    begin
      for x := 0 to dim do
        begin
          arr1[y,x] := StrToFloat(StringGrid1.Cells[y,x]);
          arr2[y,x] := StrToFloat(StringGrid1.Cells[y,x]);
        end;
      end;
  for w := 0 to dim do
    begin
      for y := 0 to dim do
        begin
          arrhasil[w,y] := 0;
          for x := 0 to dim do
            begin
              arrhasil[w,y] := arrhasil[w,y] + arr1[x,w]*arr2[y,x];
            end;
        end;
    end;
  for y := 0 to dim do
    begin
      for x := 0 to dim do
        begin
          stringGrid3.Cells[x,y] := FloatToStr(arrhasil[y,x]);
        end;
    end;
end;
user image
more 10 years ago

mas_kofa

kalo saya liat... array yg kamu deklarasikan adalah array 2 dimensi... tapi saat set panjang array kamu hanya set spt array 1 dimensi...
user image
more 10 years ago

mat_koder

lagian juga: - kode ini salah : ".... for blabla := 0 to dim do ...." - Anda harus hati2 di "boundary" , jika SETLENGTH(any array) = DIM maka element terakhir adalah DIM-1
user image
more 10 years ago

iyus.jr

thanks atas semuanya...sudah betul nih... emang kurang di Setlengthnya...ama dim-1.... ini kode lengkapnya...kali aja ada teman2 yg membutuhkannya nanti...Perkalian matrix N x N masih boros variabel sih kayaknya..

procedure TForm1.btn1Click(Sender: TObject);
var dim : Integer;
begin
dim := StrToInt(Edit1.Text);
StringGrid1.ColCount := dim;
StringGrid1.RowCount := dim;
StringGrid3.ColCount := dim;
StringGrid3.RowCount := dim;
end;

procedure TForm1.Button1Click(Sender: TObject);
var x,y,w : byte;
arr1 : array of array of double;
arr2 : array of array of double;
arrhasil : array of array of double;
dimensi,dim : Integer;
begin
dimensi := StrToInt(Edit1.Text);
SetLength(arr1,dimensi,dimensi);
SetLength(arr2,dimensi,dimensi);
SetLength(arrhasil,dimensi,dimensi);
dim:=dimensi-1;  
ShowMessage(IntToStr(Length(arr1)));   //sekedar ingin lihat panjangnya
  for y := 0 to dim do
    begin
      for x := 0 to dim do
        begin
          arr1[y,x] := StrToFloat(StringGrid1.Cells[y,x]);
          arr2[y,x] := StrToFloat(StringGrid1.Cells[y,x]);
        end;
      end;
  for w := 0 to dim do
    begin
      for y := 0 to dim do
        begin
          arrhasil[w,y] := 0;
          for x := 0 to dim do
            begin
              arrhasil[w,y] := arrhasil[w,y] + arr1[x,w]*arr2[y,x];
            end;
        end;
    end;
  for y := 0 to dim do
    begin
      for x := 0 to dim do
        begin
          stringGrid3.Cells[x,y] := FloatToStr(arrhasil[y,x]);
        end;
    end;
end;
more ...
  • Pages:
  • 1
Share to
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com