This one is much more challenging
Write an iterative procedure for tower of hanoi problem.
Printable View
This one is much more challenging
Write an iterative procedure for tower of hanoi problem.
Here's an even more difficult task! USE A SEARCH ENGINE and find the answer yourself. No one is going to do your ****ing homework for you.Quote:
Originally posted by Unregistered
This one is much more challenging
Write an iterative procedure for tower of hanoi problem.
Quzah.
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 = ???;
}
}
You realize that this is a recursive function, right?Quote:
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.
I sure hope so.:D
I like how you call your assignment a brain tickler. You should be in marketing!
>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.
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.
>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.