Thread: Towers of Hanoi, special output.

1. Towers of Hanoi, special output.

I have the classic Towers of Hanoi problem for homework. The code works. Here is my output for 3 disk:

Code:
```move disk from peg 1 to peg 2.
move disk from peg 1 to peg 3.
move disk from peg 2 to peg 3.
move disk from peg 1 to peg 2.
move disk from peg 3 to peg 1.
move disk from peg 3 to peg 2.
move disk from peg 1 to peg 2.
Press any key to continue```
However, the homework assignment calls for specialized output exactly like this (and I can't figure this part out):

Code:
```move disk 1 from peg 1 to peg 2.
move disk 2 from peg 1 to peg 3.
move disk 1 from peg 2 to peg 3.
move disk 3 from peg 1 to peg 2.
move disk 1 from peg 3 to peg 1.
move disk 2 from peg 3 to peg 2.
move disk 1 from peg 1 to peg 2.
Press any key to continue```

And of course, here is the obvious recursive solution to the problem:

Code:
```#include<stdio.h>

void moveDisk(int disk, int fromPeg, int toPeg, int workPeg)
{
if(disk == 1)
{
printf("move disk from %d to %d.\n", fromPeg, toPeg);
}
else
{
moveDisk(disk - 1, fromPeg, workPeg, toPeg);
moveDisk(1, fromPeg, toPeg, workPeg);
moveDisk(disk - 1, workPeg, toPeg, fromPeg);
}
}

int main(int argc, char *argv[])
{
moveDisk(3, 1, 2, 3);
return 0;
}```
I've tried lots of things and can't get it to keep track of what disk its moving. =\

thanks

2. Well, what's the difference beteween the output of your program, and the 'special' output of you problem?

3. Notice that its outputting what disk it was moving. 3 being the largest disk and 1 being the smallest disk.

4. I did it, I just used 3 arrays to to keep track of the disks and their location on the pegs. It's not an elegant solution though. Is there any way to do it with nifty bit-shifting techniques?

thanks.