(Your program needs to calculate this.) Test your program on several values for the number of disks to be moved, for example: 3,5,8, and 10. Your program will thereafter assume that the third peg, i.e., the one not mentioned in steps (2) and (3) above, will be used as the temporary storage peg. The peg to which we wish to move the disks, i.e., the "target" peg. The peg the disks are currently on, i.e., the "current" peg. Have your program get the following information from the user: 1) The number of disks to be moved. As the book suggests, you should use a recursive function with four input parameters. CS1325 - Introduction to Programming page: 5 Your task: Implement the Towers of Hanoi solution using the recursive method described in the book. A scan of that problem will be provided below for those who don't have the book. 208 of the Deitel text book (8th edition). Your feedback and queries relevant to this topic can be discussed in and brought directly to us from the comments box below.Transcribed image text: Program 2 – Towers of Hanoi A complete description of the Towers of Hanoi problem is found in problem 5.36, p. The source codes provided here have been coded perfectly, and are error-free.
#Chegg code hanoi towers free#
Void towerfun(int n, char fr, char tr, char ar)ĭo have any other alternative (completely different) source code for Tower of Hanoi in C? If yes, feel free to share them with us. Towerfun(n, 'A', 'C', 'B') // A, B, C are tower Printf("The sequence of moves involved in the Tower of Hanoi are :\n")
Int n // defined to store the number disc It is almost similar to the above one, except that in this code, you can input the number of discs. Sample OutputĪlternative C source code for Tower of Hanoi puzzle is given below. Copy the source code in Code::Blocks and run it. There is nothing to be provided as input in this program. In this C program for Tower of Hanoi, the objective of defining n is to store numbers of discs, and the other character variables fr, tr and ar stand for from rod, to rod and auxiliary rod, respectively. “ n” is of integer data type and the other three variables are of character data type. In the program source code, hanoifun () is the recursive function with four arguments, namely – n, fr, tr and ar. The aforementioned source code of this puzzle is the outcome of application of recursive function. Hanoifun(n, 'A', 'C', 'B') // A, B and C are the name of rod Int n = 4 // n immplies the number of discs Printf("\n Move disk %d from rod %c to rod %c", n, fr, tr) Printf("\n Move disk 1 from rod %c to rod %c", fr, tr) Void hanoifun(int n, char fr, char tr, char ar)//fr=from rod,tr =to rod, ar=aux rod – Transfer n-1 discs from rod B to C so that they sit over disc n Tower of Hanoi in C Using Recursion: /* C program for Tower of Hanoi*/ – Move n-1 discs from rod A to B which causes disc n alone in on the rod A
The algorithm or the working procedure, which is to be repeated for a finite number of steps, is illustrated below:Ī, B and C are rods or pegs and n is the total number of discs, 1 is the largest disk and 5 is the smallest one. So, the key to solving this puzzle is to break the problem down into a number of smaller problems and further break these into even smaller ones, so that it is made a typical best-suited problem for the application of the recursive function.
The source code for solving Tower of Hanoi in C is based on recursion.