Autor Beitrag
justifyofgod
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Di 29.01.08 21:52 
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:
unit best;

interface

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

type
  TForm1 = class(TForm)

    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
   procedure sort (var a:array of integer);
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  a:array[1..10] of integer;
  anzahl:integer;


implementation



procedure Tform1.sort (var a:array of integer);
var i,j : integer;
var buffer: integer;
begin
for i:=1 to 10-1 do
   for j:= i+1 to 10 do
      if a[i]>a[j] then
         begin
         buffer:= a[i];
         a[i]:= a[j];
         a[j]:= buffer
         end
end;







{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var h,x: integer;
begin
x:=1;
if anzahl=10 then
  begin
   edit1.visible:=false;
   button1.visible:=false;
   button2.visible:=false;
   listbox1.visible:=true;
   sort (a);
   for h:=1 to anzahl do
    begin
   listbox1.items.Add(inttostr(a[h]))
    end;
  end
 else
  begin
   inc(anzahl);
   a[anzahl]:=strtoint(edit1.text);
   edit1.text:='';

  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var h,x:integer;
begin
   x:=1;
   edit1.visible:=false;
   button1.visible:=false;
   button2.visible:=false;
   listbox1.visible:=true;
   sort (a);
   for h:=1 to anzahl do
    begin
   listbox1.items.Add(inttostr(a[h]))
    end;
end;

end.





sorry dass es so lang ist
aber ich finde den verdammten fehler nicht
programm ist eigendlich simpel -> zahlen eingeben und sortiert ausgeben
müsste eigendlcih auch funktionieren aber ich denke ich habe einige formulierungen falsch gemacht
währe nett wenn ihr mir helfen könntet
edit: denke dass der fehler bei den arrays ist da ich dort die formulierung mit mühe und not gefriemelt habe

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 29.01.08 23:18 
Moin und :welcome: im Forum!

user profile iconjustifyofgod hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure Tform1.sort (var a:array of integer);
var i,j : integer;
var buffer: integer;
begin
for i:=1 to 10-1 do
   for j:= i+1 to 10 do
      if a[i]>a[j] then
         begin
         buffer:= a[i];
         a[i]:= a[j];
         a[j]:= buffer
         end
end;

Ich weiß nicht, was das hier für ein Algorithmus ist, aber es hat jedenfalls nichts (oder zumindest zu wenig ;)) mit Suche in: Delphi-Forum, Delphi-Library BUBBLESORT zu tun. :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
LorenzS
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 128

MS-DOS, WIN 7, WIN 10
BP7.0, D3, D5
BeitragVerfasst: Mi 30.01.08 00:22 
der Algorithmus ist korregt, so steht er auch in meinem alten C buch aus der schule. hat aber keien speziellen namen.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 30.01.08 00:30 
Moin!

user profile iconLorenzS hat folgendes geschrieben:
der Algorithmus ist korregt
:shock: Stimmt, der funktioniert tatsächlich... :oops: Hatte mich von der ungewöhnlichen Version auf den Holzweg führen lassen. :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 30.01.08 01:46 
Was mir gleich aufgefallen ist, ist, dass du anzahl nicht initialisiert hast. Das solltest du vielleicht erstmal auf einen Wert setzen, bevor du die Variable erhöhst bei jeder neuen Zahl.
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Mi 30.01.08 08:36 
also ich mach den BubbleSort algo immer so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
for i := 0 to l -2 do //hier länge l -2 !
  for j := i +1 to l -1 do //hier länge l -1 !
    if arr[j] < arr[i] then
    begin
      //...
    end;


obwohl jetzt sehe ich grade, ist bei dir 10 die länge des arrays oder der Index des letzte Elements?
Genie
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 17

Win 95, 98, 2000, Millenium, XP, Linux, Unix

BeitragVerfasst: Mi 30.01.08 09:10 
Das ist der Bubble Sort Algorithmus....
Schliesslich kann man diesen auf verschiendene Weise realisieren.
Diesmal halt mit 2 for Schleifen und einem if Zweig
justifyofgod Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 30.01.08 10:41 
ja schon mal vielen dank für die antworten
allerdings bin ich mir immer noch nicht sicher ob ich die arrays formal korrekt eingegeben habe währe nett wenn mal jemand drüber guckt weil ich wuste halt nicht ob die korrekt sind
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Mi 30.01.08 13:37 
Es wäre vielleicht ganz schön, wenn du mal verrätst was eigentlich nicht geht...

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
justifyofgod Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 30.01.08 16:31 
entschuldigung ihr habt ja keine glaskugel^^
also gebe ich zahlen ein und drücke dann den button 2 werden sie 1.unsortiert und manche durch eine null ersetzt ausgegeben.
bei hohen zahlen werden plötzlich gggaaannnnzzz viele zahlen ausgegeben und auch total unsinnig lange
edit: ist denn das sort(a) richtig???
und die eingabe der arrays in die procedure ich meine formal so wie ich dass will nicht dass der etwas anderes macht
Einloggen, um Attachments anzusehen!
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 30.01.08 18:30 
Es könnte helfen, wenn du Anzahl initialisierst, also z.B. im FormCreate den Wert 0 zuweist...

Edit:
Äh ne, doch nicht.
justifyofgod Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 30.01.08 18:54 
also sind die formulierungen correct??
ok das hatte cih zwischenzeitlich versucht aber hat auch nciht geklappt
danke trotzdem
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 30.01.08 20:25 
Also, ich vermute das Problem besteht darin, dass du ein statisches array von 1..10 befüllst, aber als dynamisches array an deine sortierroutine übergibst.

Mach das mal so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
type
  TZahlenArray = array[1..10] of integer;


  TForm1 = class(TForm)
....
  private
   procedure sort (var myarray: TZahlenArray);
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  a:TZahlenArray;
  anzahl:integer;
....


Dass du also einen Typen deklarierst, und diesen verwendest.
Damit sollte das gehen, und der sortiert auch richtig ;)

Was du unbedingt auch noch machen solltest: Du hast in deiner Sortierprozedur den Parameter "a" benannt, aber gleichzeitig auch eine Variable "a", nämlich das array selber.
Ich hab den Parameter oben in dem Beispiel mal umbenannt..
justifyofgod Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 30.01.08 20:32 
da bekomme ich eine fehlermeldung:
fehler im modul best falsche methodendeklaration in klasse tform1
was so ich nun tun?




sorry das letzte habe ich nicht gerafft
meinst du ich soll die zeile abändern???
sort (a);
soll ich statt a einfach was anderes einsetzen??
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 30.01.08 20:52 
Du sollst eigentlich nur die 2. Zeile über das TForm1 ... schreiben,
und entsprechend die das "array of integer" ersetzen.

Zum 2. Punkt:
Ich meinte, den Parameter der Procedur "Sort" nennst du nicht auch noch a, sondern z.B. MyArray, wie in dem Quellcode den ich kopiert hab, Zeile 8.
Aufrufen kannst du das trotzdem noch mit
Sort(a);
justifyofgod Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 30.01.08 21:00 
super danke es funktioniert
weiß auch nciht was ich da für einen mist gebaut hatte
naja ich muss mir mal so manche sachen einprägen......
wie merke ich nun für weitere projekte wannich die variable als type teil machen muss und wann nicht??
das a habe ich gelassen da z.b. myarray irgendwie dann auch array sein muss und da fand ich a schon ganz gut...

sf Vielen dank habt mir echt geholfen
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 30.01.08 21:03 
Wie gesagt, wenn du die Variable als statisches
ausblenden Delphi-Quelltext
1:
Array[1..10] of integer					

deklarierst, dann aber mit einem dynamischen
ausblenden Delphi-Quelltext
1:
array of integer					

arbeitest, geht das nicht unbedingt gut.
justifyofgod Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mi 30.01.08 21:23 
sie ist ja wie das 2. deklariert