Question

Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the ith disk is į .3 pegs - A, B, C the number

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//TowerOfHanoi.java
class TowerOfHanoi
{
   public void tof(char src,char dest,char spare,int n)
   {
       //according to the strategy given
      
       if(n == 1)
       {
           System.out.println(n+" from "+src + " to "+ dest);
       }
       else
       {
           //shift n-1 disks from src to utility
          
           //shift the nth disk from source to dest
           //shift the n-1 disks from utility to dest
           tof(src, spare, dest, n-1);
           System.out.println(n + " from " + src + " to " + dest);
           tof(spare, dest, src, n-1);
       }
   }
   public static void main(String args[])
   {
       //check for number of arguments given
       if(args.length == 1)
       {
           try
           {
               //convert argument to int else print error message
               int n = Integer.parseInt(args[0]);
               //create tester object to test the program
               TowerOfHanoi tester = new TowerOfHanoi();
               //call tower of hanoi recursive function of n disks
               tester.tof('A', 'C', 'B', n);
           }
           catch(Exception e)
           {
               System.out.println("Given argument is not a number");
           }
       }
       else
       {
           System.out.println("Pass number of disks n as argument");
       }
      
   }
}
//TowerOfHanoi.java class TowerOfHanoi public void tof (char src,char dest,char spare,int n) //according to the strategy give 17 18 19 20 21 //shift the n-1 disks from utility to dest tof (src, spare, dest, n-1) Sy3tem . out .printIn(n + from + srcG4. C:Windowslsystem32cmd.exe java TowerOfHanoi 2 2 Pass number of disks n as argunent Uses fron A to B fron A to C java To

Add a comment
Know the answer?
Add Answer to:
Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of 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
  • Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see...

    Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see Creativity Exercise C-5.16 for more details). C-5.16 In the Towers of Hanoi puzzle, we are given a platform with three pegs, a, b, and c, sticking out of it. On peg a is a stack of n disks, each larger than the next, so that the smallest is on the top and the largest is on the bottom. The puzzle is to move all...

  • In the classic problem of the Towers of Hanoi, you have 3 rods and N disks...

    In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one).You have the following constraints: (A) Only one disk can be moved at a time. (B) A disk is slid off the top of one rod onto the next rod....

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • Part A [50] in c++ A toy that many children play with is a base with...

    Part A [50] in c++ A toy that many children play with is a base with three pegs and five disks of different diameters. The disks begin on one peg, with the largest disk on the bottom and the other four disks added on in order of size. The idea is to move the disks from the peg they are on to another peg by moving only one disk at a time and without ever putting a larger disk on...

  • Recursion Write a program to solve the Towers of Hanoi problem for a tower of size...

    Recursion Write a program to solve the Towers of Hanoi problem for a tower of size n, using both recursion and iteration. Time each method separately. Be very carefull to time only the actual work and avoid superfluous module calls and initialization, etc. Compare and contrast your two versions of the problem. Are they what you expected? Your analysis must contain a table of the times obtained for each run. For a tower of a particular size, your output should...

  • 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...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • Java question for two classes: Given the upper limit n as a parameter, the result of...

    Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...

  • # 3. The following code draws a sample of size $n=30$ from a chi-square distribution with...

    # 3. The following code draws a sample of size $n=30$ from a chi-square distribution with 15 degrees of freedom, and then puts $B=200$ bootstrap samples into a matrix M. After that, the 'apply' function is used to calculate the median of each bootstrap sample, giving a bootstrap sample of 200 medians. "{r} set.seed (117888) data=rchisq(30,15) M=matrix(rep(0,30*200), byrow=T, ncol=30) for (i in 1:200 M[i,]=sample(data, 30, replace=T) bootstrapmedians=apply(M,1,median) (3a) Use the 'var' command to calculate the variance of the bootstrapped medians....

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

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