Code:
#include <stdio.h>
#include <stdlib.h>
void tower ( int number, int peg1, int peg2, int peg3 );
int main(void)
{
int num, peg1, peg2, peg3;
peg1 = 1, peg2 = 2, peg3 = 3;
printf("?");
scanf( "%d", &num );
tower(num, peg1, peg2, peg3 );
system("PAUSE");
return 0;
}
void tower ( int number, int peg1, int peg2, int peg3 )
{
if ( number == 1 ){
printf( "%d --> %d\n", peg1, peg3 );
}
else{
tower( number -1 , peg1, peg2, peg3 );
printf( "%d --> %d\n", peg1, peg2 );
tower( number -1, peg1, peg3, peg2 );
printf( "%d --> %d\n", peg1, peg2 );
tower( number -1 , peg2, peg3, peg1 );
printf("%d --> %d\n", peg1, peg2 );
}
}
Hi well this is like my fourth recursive function programm i did skip the recursion chapter in the text book ..now i am having problems ... well i do know how recursion works i have done some examples on recursion like about 3...
Well this is what the author says
1. Move n-1 disk from peg 1 to peg 2 , using peg3 as a temp holding area
2.Move the last disk (largest) from peg1 to peg2 ...okay i have a prob with this one
3. Move the n-1 disk from peg 2 to peg 3, using peg1 as a temp holdong area..
Thanks alot