This is the one example I found. I am not sure I fully understand what is happening in this program. Any guidance would be appreciated.
Code:
#include<stdio.h>
/* Function Prototype */
void towers( int number, int peg1, int peg3, int peg2 );
/* Function main begins program execution */
main()
{
int number; /* Variable in which number will be stored */
printf( "Enter total no. of disks: " ); /* Prompt for input */
scanf( "%d",&number ); /* Read number from user */
towers( number, 1, 3, 2 ); /* Function called */
} /* End function main */
/* Towers function definition */
void towers( int number, int peg1, int peg3, int peg2 )
{
if( number == 1 ){
printf( "%d --> %d\n", peg1, peg2 );
return; /* Indicates that program ended successfully */
} /* End if */
towers( number - 1, peg1, peg2, peg3 );
printf( "%d --> %d\n", peg1, peg2 );
towers( number - 1, peg3, peg1, peg2 );
} /* End function towers */
The output for 3 disk is
1 --> 2
1 --> 3
2 --> 3
1 --> 2
3 --> 1
3 --> 2
1 --> 2
Can you explain the function and how it knows what number goes where? This is very confusing to me.
Thank you
DMKanz07