Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ code to solve tower of Hanoi. You can implement your design using a static 2

ID: 3767500 • Letter: C

Question

C++ code to solve tower of Hanoi. You can implement your design using a static 2-D array with 3 columns for the 3 posts, and you can initialize the array with the numbers 1, 2, and 3 in the first column to represent the initial state of the game. The goal is to print out the board after each move in the game, seeing the following output. Example with two disks, i.e. call to towers(2, b, 1, 2, 3);

1 0 0
2 0 0
----------
0 0 0
2 0 1
----------
0 0 0
0 2 1
----------
0 1 0
0 2 0
----------

Here is an outline of the recursive towers function:
void towers(int disks, int b[][COLS], int from_col, int to_col, int spare)
If(number of disks is >= 1)
Call Towers with (disks-1, b, from_col, spare, to_col)
Move the disk
Print the board
Call Towers with (disks-1, b, spare, to_col, from_col)

Explanation / Answer

Program :

#include<stdio.h>

#include<conio.h>

void hanoi(int,char,char,char);

void main()

{

int n;

clrscr();

printf(" enter no. of disks to be moved: ");

scanf("%d",&n);

hanoi(n,'A','C','B');

getch();

}

void hanoi(int n,char initial,char final,char temp)

{

if(n==1)

{

printf("move disk 1 from needle %c to %c ",initial,final);

return;

}


hanoi(n-1,initial,temp,final);
printf("move disk %d from %c to %c ",n,initial,final);
hanoi(n-1,temp,final,initial);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote