Thread: hanoi tower

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question hanoi tower

    ok guys, I have all of this done, but I keep getting segmentation fault core dumped... what is wrong with my functions? WHAT HAVE I DONE WRONG? sorry I didn't post the whole thing earlier...
    #include <stdio.h>
    int temp, use;
    int main()
    {
    int num_disk, dest;
    int disk[30];
    GetDisk(num_disk);
    GetPosition(num_disk, disk);
    GetDest(dest);
    move(num_disk, dest, disk);
    }
    int GetDisk(num_disk)
    {
    printf("input the number of disks:");
    scanf("%d", &num_disk);
    return num_disk;
    }
    int GetPosition(num_disk,disk)
    {
    printf("input the position of the original stack:");
    scanf("%d", &disk);
    return num_disk, disk;
    }
    int GetDest(dest)
    {
    printf("input the position of the final stack:");
    scanf("%d", &dest);

    }
    int move(num_disk, dest, disk)
    {

    for (temp = 1; temp <= 3; temp++)
    {
    if(disk != temp && dest != temp){use=temp;}
    }
    if (num_disk >= 0)
    {
    move(num_disk-1, disk, temp, dest);
    printf("Move %d --> %d\n", disk, dest);
    move(num_disk-1, temp, dest, disk);
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here are a thought:

    Do you realize that functions only return one value?

    > return num_disk, disk;

    Yes, this is valid code. No, it probably doesn't do what you think it does.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    First thing that I spot is the GetDisk() function.

    This function makes a copy of num_disk and changes it, not the original. Try this;

    Code:
    int GetDisk(void) 
    { 
         int num_disk;
         printf("input the number of disks:"); 
         scanf("%d", &num_disk); 
         return num_disk; 
    }
    then use the return value....

    Code:
    num_disk = GetDisk();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  2. Little Problem on Presentation in the Tower of Hanoi
    By momegao in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2003, 06:22 PM
  3. Tower of Hanoi
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 12:15 PM
  4. Hanoi tower
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 06:35 PM
  5. Tower Of Hanoi
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 12-18-2001, 11:12 PM