Autor Beitrag
proprogrammierer
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 16:33 
..., dass es diese Frage schon gab, ABER irgendwie klappt bei mir keine von wirklich vielen ausprobierten Lösungen!
Daher wollte ich wissen, ob mir jemand helfen kann.
Ich habe aus mehreren Editfeldern ein array of double herausgelesen und möchte dieses jetzt der Größe nach ordnen (b[1] ist das kleinste), damit ich hinterher den Median ausrechnen kann.
So zum Beispiel hab ich es schon ausprobiert, jedoch ohne wirklichen Erfolg:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.sortieren(var a: array of double; von,bis:integer);
  var i,j: integer; 
  wertDerMitte:real;
begin
  i:=von;
  j:=bis;
  wertDerMitte:=aa[((von+bis) DIV 2)]; 
  repeat
    while a[i]<wertDerMitte do inc(i);
    while wertDerMitte<aa[j] do dec(j);
    if i<=j then Begin
      tausche(a[i],a[j]);
      inc(i);
      dec(j);
    end;
  until i>j;
  if von<j then sortieren(aa,von,j); 
  if bis>i then sortieren(aa,i,bis);
end;

Die Tausch-Prozedur:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.tausche(a,b:real);
  var x:real;
begin
  x:=a;
  a:=b;
  b:=x;
end;


Vielen Dank schon im Voraus!
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 18.03.06 16:38 
Stichwort call by value oder call bei reference.

In der Tauschprozedur tauscht du zwar a und b aber dies sind lokale Variablen die nach Abschluss dieser Methode "verfallen". du musst das Schlüsselwort var vor diese setzen damit sich dieser Tausch auch auf die aufrufende Funktion auswirkt.
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 16:55 
Also erstmal vielen Dank für die schnelle Hilfe!
Wenn ich dem Ratschlag folge, bekomme ich eine Fehlermeldung und zwar:
[Fehler] mMain.pas(175): Die Typen der tatsächlichen und formalen Var-Parameter müssen übereinstimmen
an der Stelle :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.sortieren(var a: array of double; von,bis:integer);
  var i,j: integer;
  wertDerMitte:real;
begin
  i:=von;
  j:=bis;
  wertDerMitte:=a[((von+bis) DIV 2)]; 
  repeat
    while a[i]<wertDerMitte do inc(i);
    while wertDerMitte<a[j] do dec(j);
    if i<=j then Begin
      tausche(a[i],a[j]);                 // diese Zeile ist rot hinterlegt, also falsch
      inc(i);
      dec(j);
    end;
  until i>j;
  if von<j then sortieren(a,von,j); 
  if bis>i then sortieren(a,i,bis);
end;


Danke
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 18.03.06 17:00 
Probier mal!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure tausche(var a,b:double);
var x:double;
begin
x:=b;
b:=a;
a:=x;
end;
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:05 
also das war ein guter tip(ich bin ja so doof) nur hab ich jetzt irgendwie eine Zugriffsverletzung wenn ich auf Sortieren klicke.
ausblenden Delphi-Quelltext
1:
wertDerMitte:=a[((von+bis) DIV 2)]; //hier bleibt er stehen					

vielleicht sollte ich mal die ganze .pas datei hochladen?
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:20 
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
unit mMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Math, Buttons;

type
  TForm1 = class(TForm)
    BtNeuBt: TButton;
    BtDeleteBtLast: TButton;
    Bevel1: TBevel;
    Label1: TLabel;
    Label2: TLabel;
    BtOk: TButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    BtDeleteBtAll: TButton;
    Edit1: TEdit;
    BtAritMittel: TBitBtn;
    BtSort: TBitBtn;
    Label6: TLabel;
    Bevel2: TBevel;
    Label7: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BtNeuBtClick(Sender: TObject);
    procedure BtDeleteBtLastClick(Sender: TObject);
    procedure BtOkClick(Sender: TObject);
    procedure BtDeleteBtAllClick(Sender: TObject);
    procedure BtAritMittelClick(Sender: TObject);
    procedure BtSortClick(Sender: TObject);
  private
    { Private-Deklarationen }
    Ed: array of TEdit;
    b : array of double;
    function prooflength(a : array of TEdit) : boolean;
    procedure sortieren(var aa: array of double; von,bis:integer);
    procedure tausche(var a,b:double);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetLength(Ed,1);
  Ed[0]:= TEdit.Create(Form1);
  Ed[0].Parent:= Form1;
  Ed[0].Left:= 20;
  Ed[0].Top:= 20;
  Label3.Caption:='';
end;

procedure TForm1.BtNeuBtClick(Sender: TObject);
begin
  if prooflength(ed) then
   begin
     Application.Messagebox('Listenspeicher voll.','Fehler',MB_OK + MB_ICONERROR);
   end else
    begin
      SetLength(Ed,Length(Ed)+1);
      Ed[Length(Ed)-1]:= TEdit.Create(Form1);
      Ed[Length(Ed)-1].Parent:= Form1;
      Ed[Length(Ed)-1].Left:= 20;
      Ed[Length(Ed)-1].Top:= 20+30*(Length(Ed)-1);
      BtDeleteBtLast.enabled:=true;
        if length(ed)>=13 then
         begin
           Ed[Length(Ed)-1].Left:= 150;
           Ed[Length(Ed)-1].Top := 20+30*(Length(Ed)-13);
         end;
    end;
end;

procedure TForm1.BtDeleteBtLastClick(Sender: TObject);
begin
  try
   ed[(length(ed)-1)].Destroy
  except on exception do
   Application.Messagebox('Der letzte Listenplatz kann nicht gelöscht werden','Fehler',MB_OK + MB_ICONERROR);
  end;
  SetLength(ed,(length(ed)-1));
  BtDeleteBtLast.enabled:=false;
end;

function TForm1.prooflength(a : array of TEdit):boolean;
begin
  if length(a)>=24 then
   result:=true
   else
     result:=false;
end;

procedure TForm1.BtOkClick(Sender: TObject);
var i : integer;
begin
  Label3.Caption:='{ ';
  for i:=0 to length(ed)-1 do
   begin
     Label3.Caption:=Label3.Caption + ed[i].Text + '; ';
   end;
  Label3.Caption:=Label3.Caption + '}';
end;

procedure TForm1.BtDeleteBtAllClick(Sender: TObject);
var i : integer;
begin
  for i:=0 to length(ed)-1 do
   ed[i].destroy;
  SetLength(Ed,0);
end;

procedure TForm1.BtAritMittelClick(Sender: TObject);
var i : integer;
    u : real;
begin
  //----in array of double speichern
  SetLength(b,length(ed));
  for i:=0 to length(ed)-1 do
   begin
     b[i]:=StrToFloat(ed[i].Text);
   end;
  //----Arithmetisches Mittel ausrechnen
  u:=0;
  for i:=0 to length(ed) do
   begin
     u:=u+b[i];
   end;
  Edit1.Text:=FloatToStr((u)/(length(ed)));
end;

procedure TForm1.BtSortClick(Sender: TObject);
begin
  sortieren(b,Low(ed),High(ed));
end;

procedure TForm1.tausche(var a,b:double);
  var x:double;
begin
  x:=a;
  a:=b;
  b:=x;
end;

procedure TForm1.sortieren(var aa: array of double; von,bis:integer);
  var i,j: integer; 
  wertDerMitte:real;
begin
  i:=von;
  j:=bis;
  wertDerMitte:=aa[((von+bis) DIV 2)];    //hier ist der momentan fehler (zugriffsverletzung s.o.)
  repeat
    while aa[i]<wertDerMitte do inc(i);
    while wertDerMitte<aa[j] do dec(j);
    if i<=j then Begin
      tausche(aa[i],aa[j]);
      inc(i);
      dec(j);
    end;
  until i>j;
  if von<j then sortieren(aa,von,j); 
  if bis>i then sortieren(aa,i,bis);
end;

end.


ich dachte mir kann nicht schaden, also hier ist er


Zuletzt bearbeitet von proprogrammierer am Sa 18.03.06 17:25, insgesamt 1-mal bearbeitet
seepy
Hält's aus hier
Beiträge: 10

Debian 4.0r4

BeitragVerfasst: Sa 18.03.06 17:25 
dir is scho klar das du ein Dynamisches Array hast oder ?!
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:28 
ehh jap, aber inwiefern hilt mir das jetzt weiter?
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 18.03.06 17:30 
Das ist doch nen Quicksort. Und eigentlich sieht alles sehr gut aus. Allerdings hätte ich die Rekursion anders geregelt. Was mich interessieren würde ist, ob der Fehler schon beim ersten Durchlauf auftritt oder in tieferen Rekursionstufen der sortieren funktion?
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:33 
der fehler passiert schon quasi direkt. ein klick --- und alles ist vorbei
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 18.03.06 17:39 
Das nach dem Klick heißt nichts. Vielmehr ob du da schonmal Breakpoints und so weiter dringehabt hast. Also ob in der oben beschriebenen Zeile der Fehler auftritt, wenn sie das erstemal durchlaufen wird oder später. Wenn es später ist liegt es glaube ich an den Grenzen bei Umkehrung der Rekursion. Um das zu checken gibt es die Breakpoints und dann F7 um einzelne Befehle weiterzugehen.
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Sa 18.03.06 17:42 
Führst du auch einen Klcik auf BtAritMittel vor dem Klick auf BtSort aus? Weil in BtSortClick ist b noch nicht initialisiert!


MrSaint

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:45 
so ganz so viel von rekursionen versteh ich nicht, aber jetzt hab ich mal einen breakpoint auf die zeile gesetzt und dort wird dann auf dem roten punkt ein grüner haken gemacht (och wie nett), was das jetzt genau heißt..? aber vielleicht weißt du mit dieser info weiter?
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:49 
@MrSaint : passiert leider dasselbe
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 18.03.06 17:50 
Jetzt hat er dort angehalten hat aber gerade diesen Befehl noch nicht ausgeführt. Also jetzt nochmal F7 also einen Befehl weiter. Dann ist er nämlich ausgeführt und wir wissen, ob es ein Initialisierungsproblem ist oder nicht.
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 17:59 
so jetzt ist dann die zeile
ausblenden Delphi-Quelltext
1:
wertDerMitte:=aa[((von+bis) DIV 2)];					


blau hinterlegt und ein pfeil links

und ein haltepunkt weiter also bei :

ausblenden Delphi-Quelltext
1:
while aa[i]<wertDerMitte do inc(i);					


ist es rot hinterlegt und der rote punkt hat ein grünes häkchen
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 18:15 
user profile iconAllesquarks hat folgendes geschrieben:
Stichwort call by value oder call bei reference.


Was meintest du eigentlich damit??
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Sa 18.03.06 18:25 
Das ist zur Fehlersuche dar. Nebenbei, wenn du jetzt mit der Maus über die Variablen gehst wird deren WErt angezeigt.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
procedure sortieren(von,bis:integer;var aa: array of double); 
  var i,j: integer;   
  wertDerMitte:real;  
begin  
  if bis-von=1 then begin //Abbruch der Rekursion
    if aa[bis]>aa[von] then begin
      tausche (aa[von],aa[bis]);
    end;
    exit;
  end;
  i:=von;  
  j:=bis;  
  wertDerMitte:=aa[((von+bis) DIV 2)];    
  repeat  
    while (aa[i]<wertDerMitte) and (i<=j) do inc(i);  
    while (wertDerMitte<=aa[j]) and (j>=i) do dec(j);  
    if not(i=j) then Begin  
      tausche(aa[i],aa[j]);  
      continue;
    end;  
  until i=j;  
  sortieren(aa,von,i-1);   
  sortieren(aa,i,bis);  
end;


Als Vorschlag musst dich nicht dran halten Fehlersuche hab ich dir ja vorher beschrieben.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 18.03.06 18:34 
user profile iconAllesquarks hat folgendes geschrieben:
Als Vorschlag musst dich nicht dran halten Fehlersuche hab ich dir ja vorher beschrieben.


Könntest du bitte Satzzeichen verwenden, damit wir alle verstehen, was du meinst :roll: ?

AXMD
proprogrammierer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 43


Delphi 5, Delphi 2005
BeitragVerfasst: Sa 18.03.06 18:38 
hilft leider auch nicht, ich glaube ich hab mich irgendwie verhaspelt . . .
eigentlich wollte ich das auch einfach mit der funktion Mean machen, nur findet mein delphi JclStatisitcs.dcu nicht obwohl ich eigentlich die JEDI-library installiert habe...
Trotzdem erstmal vielen dank für alle bemühen und ich werd nochmal neu anfangen und gucken
vielleicht bin ich morgen schlauer dann werd ich das mitteilen

PS: ich mein --- wer weiterhin ideen hat, kann sie trotzdem posten ich werd es hier weiter verfolgen


[EDIT] was ich für einen Schwachsinn schreibe ;) , denn
Mean gibt es in der Math-Unit. - - Das Problem bleibt [/EDIT]