Thread: modified tower of hanoi problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Question modified tower of hanoi problem

    Hello,
    I am stuck with this problem . How can I modify the code below to give output like this for n = 3 .

    Move 1- top disk from 1 to 3
    Move 2- top disk from 1 to 2
    Move 3- top disk from 3 to 2
    Move 4- top disk from 1 to 3
    Move 5- top disk from 2 to 1
    Move 6- top disk from 2 to 3
    Move 7- top disk from 1 to 3
    7 Moves in total

    Code:
    #include<stdio.h>
    
    
    void move(int n,int needle1, int needle2, int needle3)
    {
        
                    if (n==1)
                    printf("Move top disk from %d to %d \n" ,needle1, needle3);
    	else
    	      {
    		
                              move(n-1,needle1, needle3, needle2);
                              printf("Move top disk from %d to %d \n",needle1, needle3);
                              move(n-1,needle2, needle1, needle3);
    		
    		
    	       }
    
    }
    
    int main(void)
    {
    	int n;
    	printf("Input the number of disks : ");
    	scanf("%d",&n);
    
    	move(n,1,2,3);
    
    	return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Add a fifth parameter to move() that is the current move count. Since you'll want it to change, make it an int*.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by tabstop View Post
    Add a fifth parameter to move() that is the current move count. Since you'll want it to change, make it an int*.
    Or - parameter could be int - number of moves made before call
    and move can return int - number of moves achieved during its call
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Tower of Hanoi
    By dmkanz07 in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 12:37 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM