Question

In: Computer Science

In Lecture 5, we discussed how to solve the Tower of Hanoi problem (Exercise 5.36 in...

In Lecture 5, we discussed how to solve the Tower of Hanoi problem (Exercise 5.36 in the textbook). Consider the following problem variant in which one extra constraint has been added: There are three pegs and n disks of different sizes. Initially, all disks are on the leftmost peg and arranged in order of decreasing size, with the smallest disk on top. The task is to move all the disks to the rightmost peg, under the constraints that: • Only one disk can be moved in each step; • Direct moves between the leftmost peg and the rightmost peg are not allowed; and • A larger disk may never be placed on top of a smaller disk. Write a C program that asks for a positive integer n and then outputs a valid sequence of moves that completes the above task. You may take the source code for solving the original problem presented during the lecture (available via Blackboard) as a starting point for your program.

Solutions

Expert Solution

Code:

#include <stdio.h>
void towerOfHanoi(int n, char peg_from, char peg_to, char peg_aux)
{
   if (n == 1)
   {
       printf("\nMove disk 1 from rod %c to rod %c", peg_from, peg_to);
       return;
   }
   towerOfHanoi(n-1, peg_from, peg_aux, peg_to);
   printf("\nMove disk %d from rod %c to rod %c", n, peg_from, peg_to);
   towerOfHanoi(n-1, peg_aux, peg_to, peg_from);
}
int main()
{
int n;
printf("Enter a positive integer : ");
scanf("%d",&n);
   towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of pegs
   return 0;
}

Please refer to the screenshot of the code to understand the indentation of the code:

Output:

1.

2.

​​​​​​

For any doubts or questions comment below.


Related Solutions

How do we solve teh problem of exercise addiction?
How do we solve teh problem of exercise addiction?
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include...
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; class TowersOfHanoi { friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers); public: //constructor TowersOfHanoi(); //custom methods unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout); private: //custom methods void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux); //temporary variable unsigned _n_moves; //data const unsigned _n_towers; stack<unsigned>* _tower; }; #include "TowersOfHanoi.h" // ostream& operator<<(ostream& sink, const...
Describe how the “kidney” works in amphixous using the anatomical terms we discussed in lecture and...
Describe how the “kidney” works in amphixous using the anatomical terms we discussed in lecture and then a possible mechanism of how it may move materials from the blood
: Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company...
: Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company has 8 adjacent positions on its Boeing-727 aircraft for freight containers. The weights of this containers depend on what they are carrying. and company statistics indicate that %7 of the containers are classified as ”heavy”. While heavy containers are not inherently dangerous, having two such containers next to each other is considered dangerous should the plane encounter a wind gust. Understandably, company wants to...
1. We discussed in the lecture that the coronavirus is both an AS and AD shock....
1. We discussed in the lecture that the coronavirus is both an AS and AD shock. Based on the lecture, draw a graph showing both of the movements. You need a properly drawn graph as you did for homework 4. Update for clarity: For this question, we’re only capturing the effect of the virus itself. Don’t assume there are any particular policies associated with these shocks. It’s probably easiest to think of the AS shock as the reduced labor force...
Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company has...
Recall the airplane cargo problem we have discussed in our first lecture. An air-freight company has 8 adjacent positions on its Boeing-727 aircraft for freight containers. The weights of this containers depend on what they are carrying. and company statistics indicate that %7 of the containers are classified as ”heavy”. While heavy containers are not inherently dangerous, having two such containers next to each other is considered dangerous should the plane encounter a wind gust. Understandably, company wants to know...
What is The Buyer’s Problem and how we solve it?
What is The Buyer’s Problem and how we solve it?
In this week's lecture we have discussed the concept of insider trading as it exists and...
In this week's lecture we have discussed the concept of insider trading as it exists and is viewed around the world. Discuss your view on how insider trading should be viewed
Consider the field experiment with politicians that we discussed in the lecture. The experiment is based...
Consider the field experiment with politicians that we discussed in the lecture. The experiment is based on a modified dictator game. In Treatment 1 (i.e. T1), nature plays with high probability (equal to 0.8) and randomly assigns the endowment either to the politician-dictator or to the recipient. The politician--dictator plays with complementary probability (and knows, when making a decision, that this decision will be implemented); in contrast, a recipient who receives zero (or the full endowment), will not know whether...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT