Question

In: Computer Science

use c++ Every programmer must grapple with certain classic problems, and the Towers problem is one...

use c++

Every programmer must grapple with certain classic problems,
and the Towers problem is one of the most famous of these. Legend has it that
in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another.
The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing
size. The priests are attempting to move the stack from this peg to a second peg under the constraints
that exactly one disk is moved at a time, and at no time may a larger disk be placed above a smaller
disk. A third peg is available for temporarily holding the disks. Supposedly the world will end when
the priests complete their task, so there is little incentive for us to facilitate their efforts.
Let’s assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to
develop an algorithm that will print the precise sequence of disk-to-disk peg transfers.
If we were to approach this problem with conventional methods, we’d rapidly find ourselves
hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in
mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only
n – 1 disks (and hence the recursion) as follows:
a) Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.
b) Move the last disk (the largest) from peg 1 to peg 3.
c) Move the n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.
The process ends when the last task involves moving n = 1 disk, i.e., the base case. This is
accomplished by trivially moving the disk without the need for a temporary holding area.
Write a program to solve the Towers of Hanoi problem. Use a recursive function with four
parameters:
a) The number of disks to be moved
b) The peg on which these disks are initially threaded
c) The peg to which this stack of disks is to be moved
d) The peg to be used as a temporary holding area
Your program should print the precise instructions it will take to move the disks from the
starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3,
your program should print the following series of moves:
1 → 3 (This means move one disk from peg 1 to peg 3.)
1 → 2
3 → 2
1 → 3
2 → 1
2 → 3
1 → 3*/

Solutions

Expert Solution

Code:

#include<iostream>
using namespace std;
void TOH(int n,char Sour, char Aux,char Des)
{
   if(n==1)
   {
       cout<<Sour<<"-->"<<Des<<endl;
       return;
   }
  
   TOH(n-1,Sour,Des,Aux);
   cout<<Sour<<"-->"<<Des<<endl;
   TOH(n-1,Aux,Sour,Des);
}

//main program
int main()
{
   int n;
   cout<<"TOWERS of Hanoi"<<endl;
   cout<<"Enter a number of disks to play. I'll give necessary moves."<<endl;  
   cin>>n;
   //calling the TOH
   TOH(n,'1','2','3');
   return 0;
}

Output:

Hope this helps.


Related Solutions

Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem requires that the digits be re-arranged.In this project, we will reverse the order of the digits in a number. Assignment: Design, develop, and test an Object-Oriented C++program that reads a whole number from the user, removes the sign and all leading and trailing zeroes from that number, and then performs the following operations on that number: 1) reverses the digits, 2) sorts the digits...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
*I was given the following problem to make a C program* You are a programmer at...
*I was given the following problem to make a C program* You are a programmer at Disney World. You need to create a program that will allow people to choose fast passes for three different attractions. 1. Space mountain 2. Haunted Mansion 3. Pirates of the Caribbean The fast passes are given out in 15 minute increments from 9am - 5pm Sunday-Saturday. The user can only obtain three fast passes per day - you must be able to tell the...
You must use Excel for solving all the problems In problem 13.9 on page 501, an...
You must use Excel for solving all the problems In problem 13.9 on page 501, an agent for real estate company wanted to predict the monthly rent for one-bedroom apartments, based on the size of the apartment. The data are stored in rentsliverspring. Use the results of that problem. a. at the 0.05 level of significance, is there evidence of a linear relationship between the size of the apartment and the monthly rent? b. Construct at 95% confidence interval estimate...
Problem 2: Indirect and Euclidean proofs (40 pts) For the following problems, you must use an...
Problem 2: Indirect and Euclidean proofs (40 pts) For the following problems, you must use an indirect proof technique. (a) (10 pts) Prove indirectly that, if a 2 is a multiple of 31, then so is a. Your proof should not consist of 30 cases – this includes absolutely no implied cases using horizontal dots (· · ·) and/or vertical dots (. . .). (b) (15 pts) Using the result of question (a), prove that √ 31 is not a...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if statements. . Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. • for input of "xxcaazz", "xxbaaz" → 3 • for input of "abc", "abc" → 2 • for input...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
Tax Return Problems – formerly Appendix C Individual Tax Return Problem 3 Required: • Use the...
Tax Return Problems – formerly Appendix C Individual Tax Return Problem 3 Required: • Use the following information to complete Rhonda Hill’s 2019 federal income tax return. If any information is missing, use reasonable assumptions to fill in the gaps. • The forms, schedules, and instructions can be found at the IRS website (www.irs.gov). The instructions can be helpful in completing the forms. Facts: 1. Rhonda Hill (unmarried) is employed as an office manager at the main office of Carter...
A certain material's temperature increases by 1.0 C for every 1560J that it gains. A 0.1964g...
A certain material's temperature increases by 1.0 C for every 1560J that it gains. A 0.1964g sample of quinone (molar mass= 108.1 g/mol) was burnt, and the surrounding material's temperature increased from 20.3C to 23.5C. Find the molar heat of reaction for the combustion of quinone.
Must be in C++ and we can not use STR[] we must use STR.at() Write a...
Must be in C++ and we can not use STR[] we must use STR.at() Write a program that: Declares a variable suitable for holding a person’s name Prompts the user to enter a person’s name with the text Enter name: Reads the user's input and stores it in the variable created in step 1 Outputs the following information about the name they entered a. The index of the last character in the name b. The last 3 characters of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT