This one is much more challenging

Write an iterative procedure for tower of hanoi problem.

Printable View

- 04-25-2002UnregisteredYet another Brain Tickler
This one is much more challenging

Write an iterative procedure for tower of hanoi problem. - 04-25-2002quzahRe: Yet another Brain TicklerQuote:

*Originally posted by Unregistered*

**This one is much more challenging**

Write an iterative procedure for tower of hanoi problem.

Quzah. - 04-25-2002stautze
It is showoff time. Actually this is no trouble for me, I have all of this stuff from when I did it. Here it is, but it has 3 things you have to figure out ....

Code:`int main(void)`

{

Move(DISKS,1,3,2);

return 1;

}

void Move(int count, int start, int finish, int temp) {

int swap;

while (count > 0) {

Move(count - 1, start, temp, finish);

// printf("Move disk %d from %d to %d.\n", count, start, finish);

count--;

swap = ???;

start = ???;

temp = ???;

}

}

- 04-25-2002quzahQuote:

*Originally posted by stautze*

**It is showoff time. Actually this is no trouble for me, I have all of this stuff from when I did it. Here it is, but it has 3 things you have to figure out ....**

Code:`void Move(int count, int start, int finish, int temp) {`

int swap;

while (count > 0) {

Move(count - 1, start, temp, finish);

// printf("Move disk %d from %d to %d.\n", count, start, finish);

count--;

swap = ???;

start = ???;

temp = ???;

}

}

Quzah. - 04-25-2002golfinguy4
I sure hope so.:D

- 04-25-2002Unregistered
I like how you call your assignment a brain tickler. You should be in marketing!

- 04-26-2002stautze
>You realize that this is a recursive function, right?

That is technically true, but I think this is still the answer that he is looking for. A truely iterative version is much less elegant and way to bulky.

This is a very clever version that eleminates tail recursion by rearanging the termination condition and repeating the the function with a while statement. I would call it iterative, and your compiler would probably agree with me. - 04-26-2002quzah
Sure, but I doubt they want any recursion. Anyway, there are a million hits on Google for Hanoi Towers, so this really is NEVER needed here again.

Quzah. - 04-27-2002Shiro
>That is technically true, but I think this is still the answer that he

>is looking for.

I don't think so, he said "Write an iterative procedure".

>A truely iterative version is much less elegant and way to bulky.

That's true, but that was not his homework. Though if he understands recursion, he can use your function by removing the recursion from it.