Question

In: Computer Science

C++ 1. What is the minimum number of steps if there are 1, 2, 3, 4,...

C++

1. What is the minimum number of steps if there are 1, 2, 3, 4, or more disks? Try and solve the puzzle and fill in the table.

1 1
2 3
3 7
4 15
5 ?
6 ?
7 ?
8 ?

2. Write an explicit formula for the minimum number of steps required for the puzzle? Use H(n) = ? where n is the number of disks

3. What would be the best ADT to use for the posts in the puzzle? Why is that data structure best?

4. What would be the base case (n = 1) of this problem if a recursive solution was employed? Use three of the ADTs from the previous question for storage. The three posts are labeled A, B, C. Post A is the the starting post and Post C is the ending post. Write the answer in in pseudo code.

5. What are the rest of the cases given the base case?

if (n == 1)

            disk = A.top()

            A.pop()

            C.push(disk)


please refer to concept of "Tower of Hanoi"

Solutions

Expert Solution

1. What is the minimum number of steps if there are 1, 2, 3, 4, or more disks? Try and solve the puzzle and fill in the table.

1 1
2 3
3 7
4 15
5 31
6 63
7 127
8

255

2. Write an explicit formula for the minimum number of steps required for the puzzle? Use H(n) = ? where n is the number of disks

We can derive a common pattern using values in the above table;

for number of disks (n=1), we have number of steps: 1 (2^1-1)

for number of disks (n=2), we have number of steps: 3 (2^2-1)

for number of disks (n=3), we have number of steps: 7 (2^3-1)

for number of disks (n=4), we have number of steps: 15 (2^4-1)

for number of disks (n=5), we have number of steps: 31 (2^5-1)

for number of disks (n=6), we have number of steps: 63 (2^6-1)

for number of disks (n=7), we have number of steps: 127 (2^7-1)

for number of disks (n=8), we have number of steps: 255 (2^8-1)

Thus,formula: H(n)= 2^n - 1; where n=number of disks

3. What would be the best ADT to use for the posts in the puzzle? Why is that data structure best?

Best data structure for the posts in this puzzle is a stack. Stack follows convention of first in last out. In this puzzle, we have three rings arranged in ascending order from top to bottom residing on a tower(stack) at any given point of time. The largest ring is placed first on the post and removed last following the convention of stack. Similarly, the smallest ring is placed last on top of other rings and removed first before any other ring thus, following he convention of a stack. Thus, stack is the best data structure.

4. What would be the base case (n = 1) of this problem if a recursive solution was employed? Use three of the ADTs from the previous question for storage. The three posts are labeled A, B, C. Post A is the the starting post and Post C is the ending post. Write the answer in in pseudo code.

If we follow recursive appeoach, the best case scenario would when our problem is reduced to only 1 disk out of n disks. This means we need to transfer only one disk from our source to destination. Using the formula we derived above, this can be achieved in 1 step only (2^1-1). This would be the case when we would want to move our largest disk whoch is being kept at the bottom of the source to our destination. If we wish to track the movement of every disk form one tower to another we can write the best case as follows:

void towerOfHanoi(int n, char source, char auxillary, char destination){

if ( n == 1){

disk=source.top;

source.pop();

destination.push(disk);

cout<<"Move disk 1 from "<<source<<" to "<<destination;

         return;

}

}

5. What are the rest of the cases given the base case?

Rest of the function includes recursive calls to the function towerOfHanoi with no of disks equals one less than the given number as 1 dis is already shifted to its correct place.

towerOfHamoi(n-1,source,destination,auxillary);

cout<<"Move disk 1 from "<<source<<" to "<<destination;

towerOfHanoi(n-1,auxillary,source,destination);

In this algorithm we are calling the funtion towerOfHanoi recursively. Base case if achieved when we have already one disk left to be moved. This is shown above. For rest of the n-1 disks, we will first move them from source to destination using auxillary rod. Once n-1 disks are moved to auxillary rod, we can easily move the last remaining disk in source to destination. We will then recursively repeat our approach considering there are n-1 disks.


Related Solutions

Below are the number of hours spent exercising: 2 3 4 4 4 5 1 1...
Below are the number of hours spent exercising: 2 3 4 4 4 5 1 1 4 4 4 1 2 3 3 2 Which descriptive statistics from your output would you NOT report for Hours spent Exercising? Why not? Write a few sentences describing the data (use APA formatting). This interpretation should not include only the numbers, but rather what the numbers tell you about the data. Create a histogram for the hours spent exercising. You can do this...
Below are the number of hours spent exercising: 2 3 4 4 4 5 1 1...
Below are the number of hours spent exercising: 2 3 4 4 4 5 1 1 4 4 4 1 2 3 3 2 How many participants were there in the study? What percentage of the sample exercised 4 or more hours? Report the following regarding the number of hours spent exercising, calculating by hand or using the Excel sheet provided to you. Mean: Median: Mode: Range: Variance: 4. Which descriptive statistics from your output would you NOT report for...
SE-FamilySize 1 1 4 3 2 4 2 3 4 2 4 1 4 2 2...
SE-FamilySize 1 1 4 3 2 4 2 3 4 2 4 1 4 2 2 4 5 4 5 4 4 2 4 3 1 2 3 5 5 5 Make a confidence interval. Be sure you show all the steps you took. Include a screen shot of any applet you used in your calculations. 2. Choose a confidence level (1 – α). 3. What is xbar? 4. What is s? 5. What is t? (Show a screen shot...
Here are the steps: 1). What are the hypotheses? 2). What is the rejection rule? 3)....
Here are the steps: 1). What are the hypotheses? 2). What is the rejection rule? 3). What is the test statistic? 4). What is your conclusion about the null hypothesis? 5). What is the conclusion in terms of the specific problem information? The mean time using the present technology that Donald Co. uses to make widgets takes 10 minutes a widget. Donald Co. is experimenting with a new method, and conducted a sample by making 25 widgets with the new...
Atomicity of phosphorus is (a) 1 (b) 2 (c) 3 (d) 4
Atomicity of phosphorus is ________.(a) 1(b) 2(c) 3(d) 4
Design a 4-to-16-line decoder by using the minimum number of 2-to-4-line decoders. The 2- to-4-line decoders...
Design a 4-to-16-line decoder by using the minimum number of 2-to-4-line decoders. The 2- to-4-line decoders have an enable input (‘1’=enabled) and the designed 4-to-16-line decoder does not have an enable. Name the inputs A0...A3 and the outputs D0...D15.
4. Devos inc has 4 teams (A, B, C, D) and 4 (1, 2, 3, 4)...
4. Devos inc has 4 teams (A, B, C, D) and 4 (1, 2, 3, 4) potential tasks. The total amount of days each employee would need to allocate to each job is as follows: ​ Potential Tasks Team 1 2 3 4 A 28 33 21 35 B 41 43 23 37 C 48 28 33 42 D 49 27 43 46 Use Excel solver Match the team the most efficient task. What is the total number of days...
MIPS code to calculate The 1- minimum 2- maximum 3- mid value of three number, user...
MIPS code to calculate The 1- minimum 2- maximum 3- mid value of three number, user will enter the numbers
Do only in C# (Not in C++ or C ) Minimum number of operations to convert...
Do only in C# (Not in C++ or C ) Minimum number of operations to convert array M to array N by adding an integer into a subarray Given two arrays M and N , the task is to find the minimum number of operations in which the array M can be converted into array N where each operation consists of adding an integer K into a subarray from L to R. M= {3, 7, 1, 4, 1, 2}, N...
(1) (2) (3) DI C DI C DI C $0 $4 $0 $65 $0 $2 10...
(1) (2) (3) DI C DI C DI C $0 $4 $0 $65 $0 $2 10 11 80 125 20 20 20 18 160 185 40 38 30 25 240 245 60 56 40 32 320 305 80 74 50 39 400 365 100 92 Refer to the given consumption schedules. DI signifies disposable income and C represents consumption expenditures. All figures are in billions of dollars. At an income level of $40 billion, the average propensity to consume is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT