Adding a timer to a recursive function

This is a discussion on Adding a timer to a recursive function within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a recursive fxn that happens to be the algorithm for the Tower of Hanoi. I need to ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    Adding a timer to a recursive function

    Hey, I have a recursive fxn that happens to be the algorithm for the Tower of Hanoi. I need to count how many times it makes a move, but I do not know where to add the simple code of move++ or something of that nature. If you could help me that would be great. Thanks. Here is the code:

    Code:
    void Move(int n, char source, char destination, char spare)
    {
    	
    	if (n == 1)
    	{
    		cout << "Move the top disk from " << source
    	<< " to " << destination << endl;
    	
    	}
    	else
    	{
    		
    		Move (n - 1, source, spare, destination);
    		Move (1, source, destination, spare);
    		Move (n - 1, spare, destination, source);
    
    		}
    }

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    no one has any suggestions?

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Could you elaborate on your problem? It's not clear to me exactly what you're trying to achieve.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    basically i need to create a counter to count how many times the fxn is called recursively. Say like this:

    Code:
    int timer(int moves)//fxn to calculate # of moves
    {
    	return moves++;
    }
    
    void Move(int n, char source, char destination, char spare,int moves)
    {
    	timer(moves);//counts the number of times the fxn is called
    	if (n == 1)
    	{
    		cout << "Move the top disk from " << source
    	<< " to " << destination << endl;
    	cout << endl << moves << endl;
    	}
    	
    	else
    	{
    		
    		Move (n - 1, source, spare, destination,moves);
    		//timer(moves);//counts the number of times the fxn is called
    		Move (1, source, destination, spare,moves);
    		//timer(moves);//counts the number of times the fxn is called
    		Move (n - 1, spare, destination, source,moves);
    		//timer(moves);//counts the number of times the fxn is called
           
    		}
    	}
    I tried putting the timer function everywhere... didn't work. The output was wrong.

    I want it to print out :
    Move the top disk from A to B
    number of moves 1 total
    Move the top disk from B to C
    number of moves 2 total
    .... and so on..

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Not for people who only wait 5 hours before bumping their threads

    Besides, its not like you have a lot of choices
    You put the ++moves inside the if part or the else part, and you see which one gives you the right answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I assume you have some mechanism for evaluating the final number of moves--either a cout statement in each function call if you pass the moves parameter by value as in the posted code, or a single cout statement back in the original calling function if you pass the moves parameter as a reference?

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Try:
    Code:
    void Move(int n, char source, char destination, char spare)
    {
    	moves++;
    	if (n == 1)
    	{
    		cout << "Move the top disk from " << source
    	<< " to " << destination << endl;
    	
    	}
    	else
    	{
    		Move (n - 1, source, spare, destination);
    		Move (1, source, destination, spare);
    		Move (n - 1, spare, destination, source);
    
    		}
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Make a global var and increment it each time the function is gone through..basically.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 04:42 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Recursive Ackermann's function - need help
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2002, 03:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21