Question
language use : c++
Correct Tower application to move the disks from the pin on the left to the pin on the right. Hint: draw the motion of th dis
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Please read the full answer carefully. I know it's a little long but I need to explain the problem to you. I've tried solving the puzzle myself using the output of this program.And it worked!!

Answer:

I assume that this program is made for solving the mathematical puzzle known as "Tower of Hanoi". In this puzzle there are three towers/poles/pins and we have to transfer the disks from Tower 1(which is on the left) to Tower 3(which is on the right). The only rule that we need to follow for solving this puzzle is : "We cannot put/place a big disk on the top of a small one".

Now coming to the question. In the question, the three towers are named A,B and C. A represent the Source Tower (Tower from which the Disks needs to get transferred). B represents the Auxilary Tower (Tower that will be used for temporarily holding the Disks) and C represents the Destination Tower (Tower to which the Disks will be transferred).

The code provided above is almost correct. It can be corrected in the following two ways.

Method 1. By doing a little change while calling the function towers() in the main() function:

In the above code we're passing the following values while calling the towers() function. num for number of disk. 'A' for source tower, 'B' for Destination tower and 'C' for Auxilary tower. I hope you've figured out the problem till now. If you haven't let me help you out. We're Passing 'B' as the Destination tower and 'C' as the Auxilary tower. Whereas we should be passing 'B' as the Auxilary Tower and 'C' as the Destination Tower.

We can get the correct output by interchanging the values of 'B' and 'C' while calling the towers() function in the main() function. So the correct way of the function call should be: towers(num,'A','C','B');

The program will look like this if we made this change to the code:

#include <iostream>
using namespace std;
void towers(int, char, char,char);
int main()
{
int num;
cout<<"Number of disks: ";
cin>>num;
cout<<"Sequence begin here\n";
towers(num, 'A', 'C', 'B'); //the only change is made here.
return 0;
}

void towers(int num, char frompeg,char topeg, char auxpeg)
{
if (num == 1)
{
cout << "Move disk 1 from peg "<<frompeg<<" to peg " << topeg<<endl;
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
cout << "Move disk " << num << " from peg " << frompeg <<" to peg " << topeg << endl;
towers(num - 1, auxpeg, topeg, frompeg);
}

Here's a screenshot of Output from the sample run:

File Edit View Terminal Tabs Help $ ./TOHanoi Number of disks: 4 Sequence begin here Move disk 1 from peg A to peg B Move dis

Method 2: By changing the function definition as follows:

If you don't want to change the order of the arguments then you'll need to change the order of the parameters in the function definition.

Here's the modified code:

#include<iostream>
using namespace std;
void towers(int, char, char, char);
int main()
{
int num;
cout<<"Number of Disks: ";
cin>>num;
cout<<"Sequence begin here:\n";
towers(num,'A','B','C');
return 0;
}
void towers(int num,char frompeg, char auxpeg,char topeg) //change 1
{
   if(num==1)//no change
   {
       cout<<"Move Disk 1 from peg "<<frompeg<<" to peg "<<topeg<<endl;
       return;
   }

   towers(num-1,frompeg,topeg,auxpeg); //change 2
   cout<<"Move Disk "<<num<<" from peg "<<frompeg<<" to peg "<<topeg<<endl;
   towers(num-1,auxpeg,frompeg,topeg); //change 3
}

Here's the screenshot of output from the sample run:

File Edit View Terminal Tabs Help $ ./TOHanoil Number of Disks: 4 Sequence begin here: Move Disk 1 from peg A to peg Move Dis

As you can see both ouput are similar. And both of them provide the correct output.

If you have any questions regarding the answer, or have any problem in understanding any part of it; please let me know in the comments. I'll get back to you ASAP.

Thanks.

Add a comment
Know the answer?
Add Answer to:
language use : c++ Correct Tower application to move the disks from the pin on the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Bly language program that c l orresponds to the following Cr+ program.Include the memory addr Wri...

    bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include <iostream> using namespace std; int num; int main ( cout << "Enter a number:" cin >> num ; num = num * 4; cout << "num 4-<< num << endl; return 0 bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include using namespace std; int num; int main (...

  • In PEP8 code. assembly pep8 code. 30. Write an assembly language program that corresponds to the...

    In PEP8 code. assembly pep8 code. 30. Write an assembly language program that corresponds to the following C+ program #include <iostream> using namespace std; int num; int main() cin >> num: num = num/ 16; cout << "num = " << num << endl; return 0; 21 de

  • C++ Language I have a class named movie which allows a movie object to take the...

    C++ Language I have a class named movie which allows a movie object to take the name, movie and rating of a movie that the user inputs. Everything works fine but when the user enters a space in the movie's name, the next function that is called loops infinetly. I can't find the source of the problem. Down below are my .cpp and .h file for the program. #include <iostream> #include "movie.h" using namespace std; movie::movie() { movieName = "Not...

  • C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return...

    C++ Debug nextVowel.cpp #include <iostream> using namespace std; // Recursion on a single value // return next character (in ASCII order) // which is a vowel, starting with parameter // If there are no more vowels, return null character ('\0') char nextVowel(char start) { // recursive call return nextVowel(start + 1); // base case if (start > 'z') return '\0'; // incorrect base case if ('a' == start || 'i' == start || 'o' == start || 'u' == start)...

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • 1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace...

    1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace std; const int amount = 20000; int num; int sum; int main () { cin >> num; sum = numb + amount; cout << "sum = " ,<< sum << endl; return 0; } 2. Test the program in the previous question twice. The first time, enter a value for num to make the sum within the allowed range for the Pep/8 computer. The...

  • KINDLY HELP SOLVE THE C++ SECURE SHELL DRILLS, RUN AND COMPILE THEM TO PERFORM THE TASK PERFECTLY...

    KINDLY HELP SOLVE THE C++ SECURE SHELL DRILLS, RUN AND COMPILE THEM TO PERFORM THE TASK PERFECTLY. THANK YOU. /1 Function: Complete the body of the Read _Kth Num function. #include <iostream> using namespace std; // Read the k'th input value. DO NOT ISSUE A PROMPT // Example: when 3 and inputs are 4 6 8 10 12 the desired value is 8 void Read Kth Num (int k, int & value) tinclude "mainpt60300a.cpp" //Function: Complete the MaxVal function #include...

  • C++ Type in the above program . Before running the program, change each of the question...

    C++ Type in the above program . Before running the program, change each of the question marks to the numbers 1 through 5 indicating the order in which that line will be executed. Run the program and check your numbers. Fix and rerun if necessary. Submit the program and the output. #include <iostream> using namespace std; //prototypes void DemonstrateProc1(); void DemonstrateProc2(int num); int main() { cout << "?. Execution starts with main. " << endl; DemonstrateProc1();       cout << "?....

  • write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write...

    write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write a warruse function fibonacci(n) that calculatus 0, 1, 1, 2, 3, 5, 8, 13, 21,... (n) that calculates the n" Fib begins with the terms and 1 and has the property preceding terms. a) Write a cand unsigned long long int for i number. Use unsigned int for the function's paramete her that can be printed on your system. type. b) Determine the largest...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT