Autor Beitrag
xsus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51

Win 2000, Win XP, Win 7; Ubuntu
Delphi 7, Delphi XE2, C
BeitragVerfasst: Do 13.10.11 16:15 
Hallo,
ich bin grade mächtig am verzweifeln, beim Schreiben eines Programms, welches mir Fibonacci Zahlen ausrechnet.
Es soll so aussehen, dass er mir bestimmte Fibonacci Zahlen ausrechnet, nicht alle.
Mein Quelltext bislang lautet:

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:
program Fibonacci;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  t,n, Fletzte, Fvorletzte, Faktuell:integer;

begin
  writeln ('Zahl: ');
  readln (n);
  Fvorletzte:=1;
  Fletzte:=1;
    for t:=1 to n do
    Faktuell:=Fletzte+Fvorletzte;
    Fletzte:=Fvorletzte;
    Fvorletzte:=Faktuell;
  writeln (Faktuell);
  readln;
end.


Was mache ich denn falsch? Er zeigt mir immerhin immer 2 als ergebnis an ;)
Liebe Grüße und schonmal vielen Dank ;)
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 13.10.11 16:27 
Weil deine for-Schleife nur die Zeile 17 einschließt. Was von der for-Schleife ausgeführt wird wird nicht durch passend einrücken bestimmt sondern durch einen begin-end Block.

Für diesen Beitrag haben gedankt: xsus
xsus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51

Win 2000, Win XP, Win 7; Ubuntu
Delphi 7, Delphi XE2, C
BeitragVerfasst: Do 13.10.11 16:36 
Danke erstmal. Funktioniert, habe sogar noch nen Fehler in Zeile 16 gefunden gehabt ;)
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:
program Fibonacci;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  t,n, Fletzte, Fvorletzte, Faktuell:integer;

begin
  writeln ('Zahl: ');
  readln (n);
  Fvorletzte:=1;
  Fletzte:=1;
    for t:=3 to n do
begin
    Faktuell:=Fletzte+Fvorletzte;
    Fletzte:=Fvorletzte;
    Fvorletzte:=Faktuell;
end;
  writeln (Faktuell);
  readln;
end.


Ich erhalte jetzt aber dennoch einen Warnhinweis: Zeile 22, Faktuell ist mögl. nicht initialisiert worden, was mach ich noch dagegen? LG
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 13.10.11 16:42 
Zitat:
Warnhinweis: Zeile 22, Faktuell ist mögl. nicht initialisiert worden, was mach ich noch dagegen


Bei der Meldung? Faktuell initialisieren! Was sonst ;)

Faktuell wird nur in der Schleife gesetzt und je nach Wert für n wird die nie durchlaufen und dann ist Faktuell eben uninitialisiert(Was sollte dann dein writeln (Faktuell) ausgeben?). Setze Faktuell vor der Schleife auf einen Startwert z.B. 0;

Für diesen Beitrag haben gedankt: xsus
xsus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51

Win 2000, Win XP, Win 7; Ubuntu
Delphi 7, Delphi XE2, C
BeitragVerfasst: Do 13.10.11 16:45 
Dankeschön, sorry ich bin totaler anfänger ;D