Towers of Hanoi, special output.

This is a discussion on Towers of Hanoi, special output. within the C Programming forums, part of the General Programming Boards category; I have the classic Towers of Hanoi problem for homework. The code works. Here is my output for 3 disk: ...

  1. #1
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182

    Towers of Hanoi, special output.

    I have the classic Towers of Hanoi problem for homework. The code works. Here is my output for 3 disk:

    Code:
    move disk from peg 1 to peg 2.
    move disk from peg 1 to peg 3.
    move disk from peg 2 to peg 3.
    move disk from peg 1 to peg 2.
    move disk from peg 3 to peg 1.
    move disk from peg 3 to peg 2.
    move disk from peg 1 to peg 2.
    Press any key to continue
    However, the homework assignment calls for specialized output exactly like this (and I can't figure this part out):

    Code:
    move disk 1 from peg 1 to peg 2.
    move disk 2 from peg 1 to peg 3.
    move disk 1 from peg 2 to peg 3.
    move disk 3 from peg 1 to peg 2.
    move disk 1 from peg 3 to peg 1.
    move disk 2 from peg 3 to peg 2.
    move disk 1 from peg 1 to peg 2.
    Press any key to continue

    And of course, here is the obvious recursive solution to the problem:

    Code:
    #include<stdio.h>
    
    void moveDisk(int disk, int fromPeg, int toPeg, int workPeg)
    {
    	if(disk == 1)
    	{
    		printf("move disk from %d to %d.\n", fromPeg, toPeg);
    	}
    	else
    	{
    		moveDisk(disk - 1, fromPeg, workPeg, toPeg);
    		moveDisk(1, fromPeg, toPeg, workPeg);
    		moveDisk(disk - 1, workPeg, toPeg, fromPeg);
    	}
    }
    
    int main(int argc, char *argv[])
    {
    	moveDisk(3, 1, 2, 3);
    	return 0;
    }
    I've tried lots of things and can't get it to keep track of what disk its moving. =\

    thanks
    Last edited by spoon_; 03-15-2003 at 03:12 PM.
    {RTFM, KISS}

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, what's the difference beteween the output of your program, and the 'special' output of you problem?

  3. #3
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    Notice that its outputting what disk it was moving. 3 being the largest disk and 1 being the smallest disk.
    {RTFM, KISS}

  4. #4
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    I did it, I just used 3 arrays to to keep track of the disks and their location on the pegs. It's not an elegant solution though. Is there any way to do it with nifty bit-shifting techniques?

    thanks.
    {RTFM, KISS}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  2. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 12:11 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

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