
The Towers of Hanoi
I have to write a program that the user enters the number of disks from tower 1 to tower 3. The rules are: only one disk can be moved at any time and no disk can be placed on top of a disk with a smaller diameter. All i have left to do is change my code so that it for each move it says e.g. disk 1 from tower 1 to tower 3. Also I need to print out how many moves it took. If you can help me, then thank you. Here is what I have:
#include <stdio.h>
void tower(int, int, int, int);
main()
{
int n;
printf("Enter the starting number of disks: ");
scanf("%d", &n);
tower(n, 1, 3, 2);
return 0;
}
void tower(int c, int start, int end, int temp)
{
if (c == 1) {
printf("%d > %d\n", start, end);
return;
}
tower(c  1, start, temp, end);
printf("%d > %d\n", start, end);
tower(c  1, temp, end, start);
}

use a global variable
int i;
and incrment it each time you printf()

Code:
#include <iostream>
using namespace std;
void towers(int, int, int, int);
int level[3];
int main()
{
int n;
cout << "Enter the number of disks on peg # 1: " << endl;
cin >> n;
level[0] = n;
level[1] = 0;
level[2] = 0;
towers(1, 3, 2, n);
return 0;
}
void towers(int fromTower, int toTower, int auxTower, int n) {
if (n==1) {
cout << "Move disk 1 from level " << level[fromTower  1]
<< " of tower " << fromTower << " to level "
<< level[toTower1] + 1 << " of tower " << toTower << endl;
level[fromTower  1] = 1;
level[toTower  1] += 1;
}
else
{
towers(fromTower, auxTower, toTower, n1);
cout << "Move disk " << n << " from level " << level[fromTower  1]
<< " of tower " << fromTower << " to level "
<< level[toTower  1] + 1 << " of tower " << toTower << endl;
level[fromTower  1] = 1;
level[toTower  1] += 1;
towers(auxTower, toTower, fromTower, n1);
}
}
now you add the move counting....

Stone_Coder
What is the code in C not C++?

yeah i just noticed i was on the c board and not the c++ board sorry....
cin>> is equivalent to scanf()
cout<< is equivalent to printf()
you will need header <stdio.h> not <iostream>.... sorry
so...
Code:
#include<stdio.h>
void towers(int, int, int, int);
int level[3];
int main()
{
int n;
printf("Enter the number of disks on peg # 1: " );
fflush(stdout);
scanf("%d",&n);
printf("\n");
level[0] = n;
level[1] = 0;
level[2] = 0;
towers(1, 3, 2, n);
return 0;
}
void towers(int fromTower, int toTower, int auxTower, int n)
{
if (n==1)
{
printf("Move disk 1 from level %d of tower %d to level %d of tower %d\n",level[fromTower  1],fromTower,level[toTower1] + 1,toTower );
level[fromTower  1] = 1;
level[toTower  1] += 1;
}
else
{
towers(fromTower, auxTower, toTower, n1);
printf("Move disk %d from level %d of tower %d to level %d of tower %d\n",n,level[fromTower  1],fromTower,level[toTower1] + 1,toTower );
level[fromTower  1] = 1;
level[toTower  1] += 1;
towers(auxTower, toTower, fromTower, n1);
}
}

I made a nice program in VB which does this Exactly like you do it here (I mean the recursive solution, but more graphical, of course, it's somewhere in this board (C board, I think)
If you do some math, you will find out that for n discs, the minimum amount of moves needed is
(2^n)1
Oskilian

...
Now all that is left to do is find the iterative solution :)
adios,
biterman.

/*t_of_hanoi.c, m_z_h@hotmail.com*/
#include<stdio.h>
int shift(int, int, int);
static int move=0;
int main()
{
int disk;
printf("Number of disk:");
scanf("%d",&disk);
/*____1,2,3 are the towers*/
shift_disk(1,3,disk);
printf("\nAll disks shifted: %d Move\n",move);
printf("Successfully done!!\n\n");
getch(); /*Comment the line if not from DOS/WINDOWS*/
return 0;
}
int shift_disk(int from, int to, int disk)
{
int new_to;
new_to= from+to;
new_to = 6new_to;
if(disk==0) return 0;
/*____first move all except the last to the mid tower*/
shift_disk(from, new_to, disk1);
printf("%4d: Move a disk from tower %d to %d\n",++move,from,to);
/*____Then move all disks from mid tower to destination tower*/
shift_disk(new_to, to, disk1);
return 0;
}