Autor Beitrag
Shorty
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 107

Win XP, Ubuntu 8.04
Delphi 7 Prof., Delphi 2005 Pers.
BeitragVerfasst: Do 28.12.06 17:27 
Hallo!

Ich habe mit einem Freund ein Programm geschrieben, mit dem 2 Bingozettel mit je 15 Zahlen generiert werden. Die Zahlen dürfen auf einem Zettel immer nur einmal erscheinen und sollen der Reihenfolge nach geordnet werden.

Jetzt habe ich einen Sortieralgorithmus geschrieben, der die Zahlen des Arrays ordnet. (Ist wahrscheinlich nicht optimal geworden, hab nämlich quicksort und bubblesort und was das da noch gibt, nicht verstanden und hab mir dann selbst Gedanken gemacht, wie das zu realisieren ist :wink: )

Weil ich mir jetzt vorstellen kann, dass ich so ein ordnen öfters brauchen kann, wollte ich mich jetzt mal damit beschäftigen, wie man eigentlich so eine eigene Unit erstellt, das ist im Moment mein Stand:
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:
unit zahlensortieren;

interface

type TFeld = array of integer;

procedure sortieren(feld: TFeld);

var sortiert: TFeld;

implementation

procedure sortieren(feld: TFeld);
var i, j, speichern: integer;
begin
  for j:=1 to 15 do
    begin
      for i:=0 to 14 do
        begin
          if i=14 then
            begin
              if feld[14]<feld[13then
                begin
                  speichern:=feld[14];
                  feld[14]:=feld[13];
                  feld[13]:=speichern;
                end;
            end
          else
            begin
             if feld[i]>feld[i+1then
               begin
                 speichern:=feld[i+1];
                 feld[i+1]:=feld[i];
                 feld[i]:=speichern;
               end;
             end;
        end;
    end;
  for i:=0 to 14 do sortiert[i]:=feld[i];
end;

end.

Jetzt möchte ich diese Procedure über die main-unit aufrufen, dab das jetzt mit dem Befehl sortieren(zahlen1) gemacht, zahlen1 ist vom Typ TFeld = array[0..14] of integer. wenn ich das Programm dann aber starten möchte, gibts die Fehlermeldung "[Error] Unit1.pas(79): Incompatible types: 'Unit1.TFeld' and 'zahlensortieren.TFeld'"!

Kann mir da jemand helfen?
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Do 28.12.06 17:54 
Hallo,
kann es sein das du TFeld zweimal deklariert hast? (also einmal in der Main-Unit und in deiner Zusatz-Unit)
Ich weiß gar nicht das eine ist doch ein dynamischer array und das andere nicht!
Zeig am besten mal den Code, wo du die Procedure aufrufst! (mit Deklarationen) :wink:

//Edit: In deiner Sortierfunktion solltest du die festen zahlen durch dynamische ersetzen (man weiß ja nie wie lang der array ist, wenn du das auch mal für was anderes benutzen willst) :wink:
Also dann mit length(array) die Länge abfragen

_________________
Daniel S.
Shorty Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 107

Win XP, Ubuntu 8.04
Delphi 7 Prof., Delphi 2005 Pers.
BeitragVerfasst: Do 28.12.06 18:02 
Jo, hab das zweimal deklariert...
Statisch und dynamisch ist einfach, dass einmal eine bestimmte Länge für den array festgelegt wird und einmal nicht, oder? Denn dann hab ich das so, in der Hauptunit:
ausblenden Delphi-Quelltext
1:
TFeld = array [0..14of integer;					

und in der andere unit:
ausblenden Delphi-Quelltext
1:
type TFeld = array of integer;					


Und noch die Stelle, an der das aufgerufen wird:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.zettel1;
var i, j: integer;
begin
  randomize;
  for i:=0 to 14 do
    begin
      zahlen1[i]:=random(100)+1;  
      for j:=0 to (i-1do
        begin
          while (zahlen1[j] = zahlen1[i]) do
            begin
              zahlen1[i]:=random(100)+1;
            end;  
        end;  
    end;      
  sortieren(zahlen1);
end;


EDIT:Jo, das hab ich auch schon gesehen, aber vorher war das Sortieren in der Hauptunit mit immer den gleichen Werten, da war ich einfach mal faul und hab das kopiert :P
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Do 28.12.06 18:05 
Das geht eben nicht, du kannst kein dynamisches Array an eine Funktion übergeben, die ein statisches Array erwartet.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Do 28.12.06 18:08 
Gut dann wäre das ja geklärt! :wink:
Und ich kann meinen schönen Text wieder löschen! :(

_________________
Daniel S.
Shorty Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 107

Win XP, Ubuntu 8.04
Delphi 7 Prof., Delphi 2005 Pers.
BeitragVerfasst: Do 28.12.06 18:13 
Leider noch nicht ganz geklärt, hab jetzt in der Zusatz-unit das stehen:
ausblenden Delphi-Quelltext
1:
type TFeld = array[0..14of integer;					

Trotzdem kommt noch die Fehlermeldung "[Error] Unit1.pas(79): Incompatible types: 'zahlensortieren.TFeld' and 'Unit1.TFeld'" :?
rey003
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68

Win XP
BDS 2006
BeitragVerfasst: Do 28.12.06 18:16 
lösch mal die Deklaration in der Main-Unit! Ich weiß nicht zwei Deklarationen vertragen sich nicht! Wenn du die Unit eingebunden hast findet er den Typ auch so!

_________________
Daniel S.
Shorty Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 107

Win XP, Ubuntu 8.04
Delphi 7 Prof., Delphi 2005 Pers.
BeitragVerfasst: Do 28.12.06 18:18 
Ok gut, danke, jetzt geht das :)