24 votes 24 votes The following recursive function in C is a solution to the Towers of Hanoi problem. void move(int n, char A, char B, char C) { if (......................) { move (.............................); printf("Move disk %d from pole %c to pole %c\n", n, A, C); move (.....................); } } Fill in the dotted parts of the solution. Programming in C gatecse-2002 programming recursion descriptive + – Kathleen asked Sep 15, 2014 • recategorized Jun 30, 2017 by Silpa Kathleen 3.2k views answer comment Share Follow See 1 comment See all 1 1 comment reply Bhagirathi commented Sep 18, 2014 reply Follow Share If(n==1) { Printf("\n %s %c %s %c","move disk1 from pole",A,"to pole",B); Return; } Towers(n-1,A,C,B); Printf.... Towers(n-1,C,B,A); 2 votes 2 votes Please log in or register to add a comment.
Best answer 29 votes 29 votes void move(int n, char A, char B, char C) { if (n > 0) { move (n-1, A, C, B); printf("Move disk %d from pole %c to pole %c\n", n, A, C); move (n-1, B, A, C); } } minal answered Sep 7, 2015 • edited Dec 28, 2015 by Arjun minal comment Share Follow See all 5 Comments See all 5 5 Comments reply bahirNaik commented Dec 15, 2015 i edited by bahirNaik Dec 29, 2015 reply Follow Share The snippet tells that A must be a source and C the destination.That means function would be move(src,spare,dest). Firstcall(A,B,C) A to C using B intermediate If we go by this defination, first move(n-1,A,C,B) will move n-1 disks to B using C as spare. Now the 1 that remains is moved from A(src) to C(dest). Shouldn't the last call then be move(n-1,B,A,C) ,that is moving the n-1 disk that were previously moved to intermediate node B over C(the destination) ? 18 votes 18 votes Arjun commented Dec 28, 2015 reply Follow Share correct now? 3 votes 3 votes Riya Roy(Arayana) commented Dec 28, 2015 reply Follow Share yes 1 votes 1 votes bahirNaik commented Dec 29, 2015 reply Follow Share Yes it is correct !! 1 votes 1 votes srestha commented Oct 27, 2017 reply Follow Share need updation of this ans https://gateoverflow.in/163078/tower-of-hanoi?state=comment-163092&show=163092#c163092 2 votes 2 votes Please log in or register to add a comment.
7 votes 7 votes If(n==1) { Printf("\n %s %c %s %c","move disk1 from pole",A,"to pole",B); Return; } Towers(n-1,A,C,B); Printf.... Towers(n-1,C,B,A); Bhagirathi answered Sep 18, 2014 Bhagirathi comment Share Follow See all 0 reply Please log in or register to add a comment.