Towers of Hanoi (Recurssion)?
This is the Program. (I have understood how the game works, but not able to understand and trace out the PROGRAM for it.)
#include<stdio.h>
#include<conio.h>
void towers(int n, char source, char dest, char aux);
void main()
{
int n;
clrscr();
printf("\n Enter the number of disks:");
scanf("%d",&n);
towers(n,'A','C','B');
}
void towers(int n, char source, char dest, char aux)
{
if(n==1)
{
printf("\n Move Disk 1 from peg %c to peg %c",source,dest);
return;
}
towers(n-1,source,aux,dest);
printf("\n Move dist %d from peg %c to peg %c\n",n,source,dest);
towers(n-1,aux,dest,source);
}
Please explain how i should trace the program. Also why is the return statement required inside the IF condition? (If i dont give it, it gives me error.) .
The answer to the question is available in the PDF file https://www.assignmentexpert.com/https://www.assignmentexpert.com/homework-answers/programming-answer-44396.pdf
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment