Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Matrizen-Multiplikation (https://www.delphipraxis.net/93949-matrizen-multiplikation.html)

krutho 13. Jun 2007 19:20


Matrizen-Multiplikation
 
Hiho, ich bins wieder. Steck mal wieder total fest, da das Program nicht das tut, was es soll. Diesesmal geht es um Matrizenmultiplikation mit 3 Stringgrids und 2D-Arrays.
Bekomme wieder die Fehlermeldung, das " kein geeigneter Gleitkommawert ist.
Warscheinlich wieder irgendein Anfängerfehler. *Sich schon mal selbst die hand an den kopf hau*

Delphi-Quellcode:
procedure TForm3.Matrixmul(Sender:TObject);

var  matrixA: array of array of double;
      matrixB: array of array of double;
      matrixC: array of array of double;
      a,b,c,i,j,k:integer;

begin


      a:=StrToInt(EditA.Text);
      b:=StrToInt(EditB.Text);
      c:=StrToInt(Editb.Text);
      SetLength(matrixA,a,b);
      SetLength(matrixB,b,c);
      SetLength(matrixc,a,c);
      For i:= 0 to a-1 do
        for j:= 0 to b-1 do
        begin
          matrixA[i,j]:=StrToFloat(SG1.Cells[i,j]);
        end;
      for i:= 0 to b-1 do
        for j:= 0 to c-1 do
        begin
          MatrixB[i,j]:=StrToFloat(SG2.Cells[i,j]);
        end;
        for i := 0 to a-1 do
          for j := 0 to c-1 do
            for k := 0 to b-1 do
              begin
                matrixc[i,j] := matrixc[i,j] + (matrixa[i,k] * matrixb[k,j]);
                SG3.Cells[i,j]:=FloatToStr(matrixc[i,j]);
              end;

end;
Schon mal danke im Voraus für eure Hilfe.

fLaSh11 13. Jun 2007 20:52

Re: Matrizen-Multiplikation
 
Wo tritt der Fehler genau auf?

Nikolas 13. Jun 2007 21:41

Re: Matrizen-Multiplikation
 
Die Meldung legt doch nahe, dass dein Fehler dann auftritt, wenn du versuchst einen leeren String in ein Float umzuwandeln. Der Fehler dürfte also im Einlesen der grids in die Arrays liegen. Sind denn alle Cells der Grids mit einer Zahl (z.B. 0) gefüllt?

Jelly 13. Jun 2007 21:55

Re: Matrizen-Multiplikation
 
Ausserdem hätte wohl ein Thread auch gereicht, um ein und das gleiche Problem zu erklären: siehe hier
Fehlermeldungen darf man ruhig mal lesen und versuchen zu verstehen.
Zitat:

Zitat von Fehlermeldung
" kein geeigneter Gleitkommawert

Da steht doch alles drin. Ein Leerstring kann nun mal nicht in ein Gleitkommawert umgewandelt werden.

krutho 14. Jun 2007 14:43

Re: Matrizen-Multiplikation
 
Der Fehler befindet sich nach meinen Kenntinisstand beim einlesen des zweiten Stringgrids,
also in etwa hier.

Delphi-Quellcode:
for i:= 0 to b-1 do
        for j:= 0 to c-1 do
        begin
          MatrixB[i,j]:=StrToFloat(SG2.Cells[i,j]);
        end;
Sorry das ich euer Forum überflute, aber ich weiß an der Stelle wirklich nicht weiter, da
ich nicht weiß, warum hier eine Zelle keine Gleitkommazahl enthalten sollte.
Es sind definitiv alle existierenden Stringgridcells mit Zahlen belegt (mach ich vorher mit random). Hab ich vielleicht ein Array falsch mit Indizes versehen ?

marabu 14. Jun 2007 14:50

Re: Matrizen-Multiplikation
 
Hallo,

vielleicht kommst du deinem Fehler so auf die Spur:

Delphi-Quellcode:
// ...
      for i:= 0 to b-1 do
        for j:= 0 to c-1 do
          if (i < SG2.ColCount) and (h < SG2.RowCount)
            then MatrixB[i,j] := StrToFloat(SG2.Cells[i,j])
            else ShowMessage(Format('Index out of Bounds: SG2.Cells[%d,%d]', [i, j]));
Grüße vom marabu

krutho 14. Jun 2007 15:35

Re: Matrizen-Multiplikation
 
Danke für den Vorschlag, habs aber jetzt doch selbst hingekriegt (mehr oder weniger).
Hier erstmal meine Änderungen.
Delphi-Quellcode:
procedure TForm3.Matrixmul(Sender:TObject);

var  matrixA: array of array of double;
      matrixB: array of array of double;
      matrixC: array of array of double;
      a,b,c,i,j,k:integer;

begin
      a:=StrToInt(EditA.Text);
      b:=StrToInt(EditB.Text);
      c:=StrToInt(Editb.Text);
      SetLength(matrixA,a,b);
      SetLength(matrixB,b,c);
      SetLength(matrixc,a,c);
        for i := 0 to a-1 do
            for j := 0 to c-1 do
            begin
              matrixc[i,j] := 0;
              SG3.Cells[j,i]:='0';
            end;
      For i:= 0 to SG1.RowCount-1 do
        for j:= 0 to SG1.ColCount-1 do
        begin
          matrixA[i,j]:=StrToFloat(SG1.Cells[j,i]);
        end;
      for i:= 0 to SG2.RowCount-1 do
        for j:= 0 to SG2.ColCount-1 do
        begin
          MatrixB[i,j]:=StrToFloat(SG2.Cells[j,i]);
        end;
        for i := 0 to SG1.RowCount-1 do
          for j := 0 to SG3.ColCount-1 do
            for k := 0 to SG2.RowCount-1 do
              begin
                matrixc[i,j] := matrixc[i,j] + (matrixa[i,k] * matrixb[k,j]);
                SG3.Cells[j,i]:=FloatToStr(matrixc[i,j]);
              end;

end;
In den meisten Fällen funzt das jetzt. Will ich nun jedoch z.B. eine 4x2
mit einer 2x4 Matrix multiplizieren, so erhalte ich eine neue Fehlermeldung.

"ungültige Zeigeroperation"

Was soll das denn bedeuten? Danke für eure Hilfe

shmia 14. Jun 2007 16:00

Re: Matrizen-Multiplikation
 
Du brauchst zwei Konvertierungsfunktionen:
StringGrid -> Matrize
Matrize -> Stringgrid
Hier die Deklarationen:
Delphi-Quellcode:
type
   T2DMatrix = array of array of double;
...
procedure StringgridToMatrix(const sg:TStringGrid; var matrix:T2DMatrix);
procedure MatrixToStringgrid(const matrix:T2DMatrix; sg:TStringGrid);
Wichtig: die Anzahl von Reihen und Spalten der Matrix muss zum StringGrid passen.
Das lässt sich aber per Code überprüfen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:47 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz