Thank you very much for your help Okey but there seems to be something wrong with the implementation of the above code. This is the program I wrote
using your hints:
program MyHanoi;
{$APPTYPE CONSOLE}
uses
SysUtils;
var N, Tusing, TTo, Tfrom, SN:integer;
begin
SN:=N;
while N > 0 DO
BEGIN
N:=N-1;
writeln('move ', Tfrom:1, ' --> ', Tto:1);
{Exchange the variables like your recursion would repeatedly do}
Tusing:=TTo-TUsing;
TTO:=TUSING+TTO;
TUSING:=TTO+TUsing;
END;
N:=SN;
while N > 0 DO
BEGIN
N:=N-1;
writeln('move ', Tfrom:1, ' --> ', Tto:1);
{Exchange the variables like your recursion would repeatedly do}
Tusing:=TFrom-TUsing;
TFrom:=TUSING+TFrom;
TUSING:=TFrom+TUsing;
END;
readln
end.
It gives the following result:
move 1 --> 3
move 1 --> 4
move 1 --> 3
move 1 --> 4
move -3 -->4
move 1 --> 4
I can;t figure out what am I doing wrong?
Main Topics
Browse All Topics





by: OkeyPosted on 2003-05-28 at 21:29:34ID: 8603304
To Roll a Recursion do the following!
- Globalize your Variables (No Lokal Variables or useage of PARAMETERS)
- Instead of doing a recursive call try to handle it in loops!
SN:=N;
while N > 0 THEN
BEGIN
N:=N-1,
writeln('move ', Tfrom:1, ' --> ', Tto:1);
{Exchange the variables like your recursion would repeatedly do}
Tusing:=TTo-TUsing;
TTO:=TUSING+TTO;
TUSING:=TTO+TUsing;
END
...
Then Exchange repeatedly the Variables like Your recursion would do, after your recursive calls are done.
N:=SN;
while N > 0 THEN
BEGIN
N:=N-1,
writeln('move ', Tfrom:1, ' --> ', Tto:1);
{Exchange the variables like your recursion would repeatedly do}
Tusing:=TFrom-TUsing;
TFrom:=TUSING+TFrom;
TUSING:=TFrom+TUsing;
END
...
I think that's it, but your recursion was nicer and none the less by stack limited!
Perfect example whatfor a recursion is thought!!