Question

In: Computer Science

**C++ only, standard library. We are supposed to create a tower of hanoi program and do...

**C++ only, standard library.

We are supposed to create a tower of hanoi program and do a sequence of 100 disks. We are supposed to run the program and then answer two questions:

1) How long does your computer take to run(time)?

2) If I could move 1 disk per second how long would it take?

Here is my program so far:

void towerOfHanoi(int numDisks, char from_rod, char to_rod, char aux_rod)
{
int count=0;
if (numDisks == 1)
{
//cout << "Move disk 1 from rod " << from_rod <<
// " to rod " << to_rod<<endl;
return;
}
towerOfHanoi(numDisks - 1, from_rod, aux_rod, to_rod);

//cout << "Move disk " << numDisks << " from rod " << from_rod <<
//" to rod " << to_rod << endl;
towerOfHanoi(numDisks - 1, aux_rod, to_rod, from_rod);
}
  
// Driver code
int main()
{
int numDisks; // Number of disks
  
cout<<"This program solves the Hanoi Tower puzzle"<<endl<<endl;
  
cout<<"Enter the number of disks to calculate: ";
cin>>numDisks;
  
towerOfHanoi(numDisks, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
  

Solutions

Expert Solution

#include <iostream>
#include <algorithm>
#include <chrono>

using namespace std;
using namespace std::chrono;

int moves = 0;

void towerOfHanoi(int numDisks, char from_rod, char to_rod, char aux_rod)
{
    moves++;
    int count=0;
    if (numDisks == 1)
    {
        //cout << "Move disk 1 from rod " << from_rod <<
        // " to rod " << to_rod<<endl;
        return;
    }
    towerOfHanoi(numDisks - 1, from_rod, aux_rod, to_rod);

    //cout << "Move disk " << numDisks << " from rod " << from_rod <<
    //" to rod " << to_rod << endl;
    towerOfHanoi(numDisks - 1, aux_rod, to_rod, from_rod);
}
  
// Driver code
int main()
{
    int numDisks; // Number of disks
    
    cout<<"This program solves the Hanoi Tower puzzle"<<endl<<endl;
    
    cout<<"Enter the number of disks to calculate: ";
    cin>>numDisks;
    
    // Get starting timepoint
    auto start = high_resolution_clock::now();
    towerOfHanoi(numDisks, 'A', 'C', 'B'); // A, B and C are names of rods
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cout << "Time taken: "
         << duration.count() << " microseconds" << endl;

    cout << "Number of disk movements: " << moves << endl;
    return 0;
}

**************************************************
The above code can be used to measure the time taken in reality.. And also it shows how many disk movements happen.
If you notice below, for n = 10, We get (2^n - 1) movements.

Similarly, For n = 3,

Hence, For n = 100 disks, (2^100 - 1) movements will be required, which is 1267650600228229401496703205375 movements.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

I'm supposed to create a c++ program which is supposed to take a sentence as an...
I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter. ex. Enter a phrase: It's a hard knock life A2 I2 K2 C1 D1 E1 F1 H1 L1 N1 O1 R1 S1 T1 I was also given a recommended code to use as a bubble sort procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped =...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
I have a recursive Tower of Hanoi program and would like to get the index numbers...
I have a recursive Tower of Hanoi program and would like to get the index numbers to be in correct sequence: 1,2,3,4,5,6,7. How can I do this? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args){ /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a...
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...
Python we need to create a game using only the library graphic.py and use only Structure...
Python we need to create a game using only the library graphic.py and use only Structure Functions only.
My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does...
My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does not prompt user to enter the starting rod letter and the ending rod letter. Could you help me fix it? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { int index = 1; public static void main(String[] args) { /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is...
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three...
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three discs labels 1 (smallest), 2 (medium size), and 3 (largest disc) The program prompts you to enter the starting rod (A, B, or C) The program prompts you to enter the ending rod (A, B, or C, but cannot be same rod as entered as starting rod) The starting rod has discs 1, 2, 3, where 1 is on top of 2 is on...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT