# towers of hanoi - what is wrong with this code?

Printable View

• 09-16-2007
kanesoban
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:

Quote:

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.
• 09-16-2007
Salem
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.
• 09-17-2007
kanesoban
Quote:

Your function style became obsolete about 20 years ago.
What do you mean? What should i write other way?

Quote:

If you're using some ancient compiler...
I'm using gcc.
• 09-17-2007
hk_mp5kpdw
Quote:

Originally Posted by kanesoban
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) {```
• 09-17-2007
kanesoban
ah, i see. This new way reminds me of Pascal.
Ok, i will use that way instead.