Thread: towers of hanoi - what is wrong with this code?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    6

    towers of hanoi - what is wrong with this code?

    Helo !

    I'm trying to write a program, that solves the logical game "towers of hanoi" for n.
    If you newer heard of it, heres the game's description from wikipedia:

    The Tower of Hanoi or Towers of Hanoi is a mathematical game or puzzle. It consists of three pegs, and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape.

    The objective of the game is to move the entire stack to another peg, obeying the following rules:

    * Only one disk may be moved at a time.
    * Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
    * No disk may be placed on top of a smaller disk.
    The problem with my program is: it DOES solve the problem IF there are 1-3 disks, but if the is 4, or more, it simply never ends.
    Here's the code:

    Code:
    #include <stdio.h>
    
    int a[101],b[101]={0},c[101]={0};
    
    int main (void)
     {
     int i,*ki,*se,*ce;
     scanf("%d",&i); /*how many disks should be*/
     a[i]=0;
     ki=&a[0];
     se=&b[0];
     ce=&c[0];
     while (i-1 >= 0) /*this is the part where the program puts the "disks" on the first "peg"*/
      {
      a[i-1]=i;
      i--;
      } 
     hanoi(ki,se,ce);
     while (c[i] != 0) /*the program writes out the contents of the third peg, represented as the "c" array*/
      {
      printf("%d ",c[i]);
      i++;
      }
     return(0);
     }
    
    hanoi(ki,se,ce)
    int *ki,*se,*ce;
     {
     int *swap,i;
     beginning:
     i=0;
     while(*(ki+i) != 0)
      i++;
     if (i == 1) /*if theres only one disk,  just put it on the right peg*/
      {
      put(ki,ce);
      return(0); 
      }
     if (i > 1) /*if there are more than one disks, use recursion: put all the disks except the last one on the "second" peg*/
      hanoi((ki+1),ce,se);
     put(ki,ce); /*and after that, put that last one on the right peg*/
     swap=ki;
     ki=se; 
     se=swap;
     goto beginning;
     }
    
    put(from,to)
    int *from,*to;
     {
     int i=0,j=0;
     while(*(from+(i+1)) != 0)
      i++;
     while(*(to+j) != 0)
      j++;
     *(to+j)=*(from+i);
     *(to+j+1)=*(from+i)=0;
     }
    In my program, three global integer arrays (a,b and c) represent the three pegs, and the disks are represented as integers. The smaller the integer is, the bigger disk it represent. The smallest integer (and biggest disk) is 1.
    In the beginning, all the "disks" are in the "a" array, from 1 to n (depending on how many disks you want: the program asks you this in the beginning). The end of the three arrays are represented by a "0" after the last integer (this way it is easy to measure how "tall" the peg is).
    The program uses a recursive function to solve this problem.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Erm, how old is the book you're reading?

    Your function style became obsolete about 20 years ago.

    Have you determined how much stack space your recursive functions use? If you're using some ancient compiler, the default stack space is around a couple of KB, so you could easily blow that away if you're not careful.

    Also, use more braces to indicate the grouping of statements. You don't have to restrict yourself to where they are compulsory, and it will save you grief in the long run.
    Also, 1 space per indent isn't enough IMO to distinguish scope levels.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    6
    Your function style became obsolete about 20 years ago.
    What do you mean? What should i write other way?

    If you're using some ancient compiler...
    I'm using gcc.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by kanesoban View Post
    What do you mean? What should i write other way?
    He means this:
    Code:
    hanoi(ki,se,ce)
    int *ki,*se,*ce;
    {
    
    ...
    
    put(from,to)
    int *from,*to;
    {
    Is usually written as:
    Code:
    void hanoi(int *ki, int *se, int *ce)
    {
    
    ...
    
    void put(int *from, int *to)
    {
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    6
    ah, i see. This new way reminds me of Pascal.
    Ok, i will use that way instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  5. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM