The Towers of Hanoi

This is a discussion on The Towers of Hanoi within the C Programming forums, part of the General Programming Boards category; I have to write a program that the user enters the number of disks from tower 1 to tower 3. ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    The Towers of Hanoi

    I have to write a program that the user enters the number of disks from tower 1 to tower 3. The rules are: only one disk can be moved at any time and no disk can be placed on top of a disk with a smaller diameter. All i have left to do is change my code so that it for each move it says e.g. disk 1 from tower 1 to tower 3. Also I need to print out how many moves it took. If you can help me, then thank you. Here is what I have:

    #include <stdio.h>
    void tower(int, int, int, int);

    main()
    {
    int n;

    printf("Enter the starting number of disks: ");
    scanf("%d", &n);
    tower(n, 1, 3, 2);

    return 0;
    }

    void tower(int c, int start, int end, int temp)
    {
    if (c == 1) {
    printf("%d --> %d\n", start, end);
    return;
    }

    tower(c - 1, start, temp, end);

    printf("%d --> %d\n", start, end);

    tower(c - 1, temp, end, start);
    }

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    use a global variable
    int i;

    and incrment it each time you printf()

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <iostream> 
    using namespace std;
    void towers(int, int, int, int); 
    
    int level[3]; 
    int main() 
    { 
    int n; 
    cout << "Enter the number of disks on peg # 1: " << endl; 
    cin >> n;
    level[0] = n; 
    level[1] = 0; 
    level[2] = 0; 
    towers(1, 3, 2, n);
    return 0; 
    } 
    
    void towers(int fromTower, int toTower, int auxTower, int n) {
    if (n==1) {
    cout << "Move disk 1 from level " << level[fromTower - 1]
    << " of tower " << fromTower << " to level " 
    << level[toTower-1] + 1 << " of tower " << toTower << endl;
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1; 
    }
    else 
    {
    towers(fromTower, auxTower, toTower, n-1);
    cout << "Move disk " << n << " from level " << level[fromTower - 1]
    << " of tower " << fromTower << " to level "
    << level[toTower - 1] + 1 << " of tower " << toTower << endl;
    level[fromTower - 1] -= 1; 
    level[toTower - 1] += 1; 
    towers(auxTower, toTower, fromTower, n-1);
    } 
    }
    now you add the move counting....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    Stone_Coder
    What is the code in C not C++?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yeah i just noticed i was on the c board and not the c++ board sorry....

    cin>> is equivalent to scanf()
    cout<< is equivalent to printf()

    you will need header <stdio.h> not <iostream>.... sorry

    so...
    Code:
    #include<stdio.h>
    void towers(int, int, int, int); 
    
    int level[3]; 
    int main() 
    { 
    int n; 
    printf("Enter the number of disks on peg # 1: " );
    fflush(stdout); 
    scanf("%d",&n);
    printf("\n");
    level[0] = n; 
    level[1] = 0; 
    level[2] = 0; 
    towers(1, 3, 2, n);
    return 0; 
    } 
    void towers(int fromTower, int toTower, int auxTower, int n) 
    {
    if (n==1) 
    {
    printf("Move disk 1 from level %d of tower %d to level %d of tower %d\n",level[fromTower - 1],fromTower,level[toTower-1] + 1,toTower );
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1; 
    }
    else 
    {
    towers(fromTower, auxTower, toTower, n-1);
    printf("Move disk %d from level %d of tower %d to level %d of tower %d\n",n,level[fromTower - 1],fromTower,level[toTower-1] + 1,toTower );
    level[fromTower - 1] -= 1; 
    level[toTower - 1] += 1; 
    towers(auxTower, toTower, fromTower, n-1);
    } 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    I made a nice program in VB which does this Exactly like you do it here (I mean the recursive solution, but more graphical, of course, it's somewhere in this board (C board, I think)

    If you do some math, you will find out that for n discs, the minimum amount of moves needed is

    (2^n)-1

    Oskilian

  7. #7
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    ...

    Now all that is left to do is find the iterative solution

    adios,
    biterman.
    Do you know how contemptous they are of you?

  8. #8
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    /*t_of_hanoi.c, m_z_h@hotmail.com*/

    #include<stdio.h>
    int shift(int, int, int);
    static int move=0;

    int main()
    {
    int disk;
    printf("Number of disk:");
    scanf("%d",&disk);

    /*____1,2,3 are the towers*/
    shift_disk(1,3,disk);

    printf("\nAll disks shifted: %d Move\n",move);
    printf("Successfully done!!\n\n");

    getch(); /*Comment the line if not from DOS/WINDOWS*/

    return 0;
    }


    int shift_disk(int from, int to, int disk)
    {
    int new_to;

    new_to= from+to;
    new_to = 6-new_to;

    if(disk==0) return 0;

    /*____first move all except the last to the mid tower*/
    shift_disk(from, new_to, disk-1);

    printf("%4d: Move a disk from tower %d to %d\n",++move,from,to);

    /*____Then move all disks from mid tower to destination tower*/
    shift_disk(new_to, to, disk-1);

    return 0;

    }
    Last edited by zahid; 01-07-2002 at 07:09 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. towers of hanoi - what is wrong with this code?
    By kanesoban in forum C Programming
    Replies: 4
    Last Post: 09-17-2007, 02:20 PM
  2. Towers of Hanoi (need help)
    By Loudan in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2006, 10:17 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM
  4. Towers of Hanoi, special output.
    By spoon_ in forum C Programming
    Replies: 3
    Last Post: 03-15-2003, 06:08 PM
  5. Towers of Hanoi
    By janehung in forum C Programming
    Replies: 12
    Last Post: 01-07-2002, 06:40 AM

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