Google Answers Logo
View Question
 
Q: Easy tower of hanoi program ( Answered 4 out of 5 stars,   2 Comments )
Question  
Subject: Easy tower of hanoi program
Category: Computers > Programming
Asked by: phelsva-ga
List Price: $8.00
Posted: 21 Apr 2003 09:06 PDT
Expires: 21 May 2003 09:06 PDT
Question ID: 193321
This is a very easy program, I have already provided the header file,
driver program and custom functions, I just need two things done for
me, I would have done them myself, but I'm really pressed for time on
stuff at work. I need this done by 11.00pm ,Wednesday  04/23/2003. Thank
you. And Please stick to the code I already have, it compiles
perfectly.

Things I want done:
1.submit the output results from  execution of your program with  n
=3, n=5, n=10, n=20, n=30 where n  is the user input for the number of
disks. Be sure to desk-check your program results.

2.By benchmarking your program and recording the execution time for
each of the the values of n in #1 above determine the number of moves
and estimate the execution time required to run the program when n=64.
 note. Do not try to run the program when n=64 because it  would not
finish in a feasible time frame

you can either cut and paste these results and put them at the end of
the program or you can attach them somehow, but I need to see these 2
things.

The towers of  Hanoi:

In this program you’ll write a recursive function that identifies all
the moves needed to complete the towers of Hanoi  logic problem.

There are three towers T1 T2 T3 on a platform and n>0 disks (D1…….Dn)
of different diameters placed on the first tower T1.  The disks are in
the order of decreasing diameter as one scans up the tower T1.Monks
were reputedly supposed  to move the disks from tower T1 to  tower T3
obeying the following rules

1.only one disk can be moved at a time
2. no disk can be placed on top of another disk with a smaller
diameter
3. to complete the task, the disk should be stacked on tower 
T3exactly like they are on tower at1.


Write a C program that  calls a recursive function and prints  out a
sequence of moves and total number of moves needed to accomplish this
task for a given number of disks. The program should accept  from the
user, the number of disks (ranging from 1 -100)and then call the
recursive function to print each move in sequence( if the number of
disks is less than 10), and finally print the total time elapsed,
required to complete the task.


I have already provided the header files, functions and the main
driver program all I need is:

1.submit the output results from  execution of your program with  n
=3, n=5, n=10, n=20, n=30 where n  is the user input for the number of
disks. Be sure to desk-check your program results.

2.By benchmarking your program and recording the execution time for
each of the the values of n in #1 above determine the number of moves
and estimate the execution time required to run the program when n=64.
 note. Do not try to run the program when n=64 because it  would not
finish in a feasible time frame









/* Header File */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>


void hanoi(int,int,char,char,char);
void move(char,char,char);
void ddraw(void);
int peg[3][50];         
int top[3]={0,0,0};     
static char disc[13][26];
void InitialDiscGraph (int,int);
int n;




/* Driver program */
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
void InitialDiscGraph (int,int);


int main(void)
{
   int i = 0 , j = 0 ;


   InitialDiscGraph (i,j);



   return 0;

}



/* main program*/
==================== Program description ==================
// This program will solve the hanoi problem and also        
// draw the graph for the peg situation. The maximum         
// number of discs to process is 50.                         
//===========================================================



#include"tower2.h"


void InitialDiscGraph (int i,int j)  /* Initialize the disc graph */
{

	for (i=0; i<=12; i++)
	{
		for (j=0; j<=11; j++)
		{
			if (11-j>=i)
			{
				disc[i][j]=' ';
                disc[i][24-j]=' ';
             } 
			
			else 
			{
				disc[i][j]='*';
                disc[i][24-j]='*';
             }
           
		}
        
		disc[i][12]='|';
        disc[i][25]='\0';
     
	}
    
    printf(" Please input the number of disc(1-12):");
    scanf("%d",&n);
    printf("Hanoi Tower with %d discs:\n",n);
    printf("====================================================\n\n");
   
    for (i=1; i<=n; i++) /* initialize the peg status */
    {
		top[0]++;
        peg[0][top[0]]=n-i+1;
        
	}
    ddraw(); /* Draw the initial status */
    
 
    while (n != 0)
    {
		hanoi(n,n,'A','B','C'); /* Do n discs */
        printf(" Please input the number of disc:");
        scanf("%d",&n);
        printf("Hanoi Tower with %d discs:\n",n);
        printf("====================================================\n\n");
        
		/* initial top */
        
		for (i=0; i<=2; i++)
		{
			top[i]=0;
		}

        for (i=0; i<=2; i++) /* Clean up the discs in the pegs */
		{
			for (j=0; j<=20; j++) 
			{
				peg[i][j]=0;
			}

           
		}
        
		for (i=1; i<=n; i++)  /* initialize the peg status */
        {
			top[0]++;
            peg[0][top[0]]=n-i+1;
           
		}

        if (n!=0)

        ddraw();  /* Draw the initial status */
    
	}

	return  ;  
	

}


//*************************************************************
//Main function of hanoi tower program:                      
//It will move the disc recursively                      
//hanoi(n, A, B, C) =                                    
//hanoi(n-1, A, C, B) + hanoi(1, A, B, C)           
//+hanoi(n-1, B, A, C)                                 
//************************************************************* 
     

void hanoi(int num,int Disc_num,char beg,char aux,char tem)
{
	if (num==1) /* only one disc */
    {
		printf("move disc %d from peg %c to %c \n",Disc_num,beg,tem);
        move(beg,aux,tem); /* update the graph status */ 
        
		ddraw(); /* disc status draw */
     }
     
	else
    {
		hanoi(num-1,Disc_num-1,beg,tem,aux); /* move n-1 disc from beg to
aux */
        hanoi(1,Disc_num,beg,aux,tem);/* move 1 disc from beg to tem
*/
        hanoi(num-1,Disc_num-1,aux,beg,tem);/* move n-1 disc from aux
to tem */
    }
}


//************************************************************
//Move: move the discs between the pegs by updating       
//the top pointer                                   
//************************************************************      
     
void move(char beg,char aux,char tem)
{
	if (beg=='A')  /* Move disc from A to B */
    {
		if (tem=='B') 
        {
			top[1]++;
            peg[1][top[1]]=peg[0][top[0]];
            peg[0][top[0]]=0;
            top[0]--;
         }

        else
		{
			top[2]++;
            peg[2][top[2]]=peg[0][top[0]];
            peg[0][top[0]]=0;
            top[0]--;
         }
      }
      
	else
    {
		if (beg=='B') /* Move disc from B to A */
        {
			if (tem=='A') 
			{
				top[0]++;
                peg[0][top[0]]=peg[1][top[1]];
                peg[1][top[1]]=0;
                top[1]--;
			}

         else        /* Move disc from B to C */
         {
			 top[2]++;
             peg[2][top[2]]=peg[1][top[1]];
             peg[1][top[1]]=0;
             top[1]--;
          }

        }

       else
       {
		   if (tem=='A')  /* Move disc from C to A */
           {
			   top[0]++;
               peg[0][top[0]]=peg[2][top[2]];
               peg[2][top[2]]=0;
               top[2]--;
           }

         else          /* Move disc from C to B */
         {
			 top[1]++;
             peg[1][top[1]]=peg[2][top[2]];
             peg[2][top[2]]=0;
             top[2]--;
          }

        }
      
	}
      
	return;
    
 }


//******************************************************************
//Draw the disc status                             
//******************************************************************
     
void ddraw(void)
{
	int i = 0;
    int j = 0;
    int k = 0;
    

	for (i=n; i>=1; i--)
    {
		printf("  ");
        for (j=0; j<=2; j++)
		{
			for (k=0; k<=25; k++)
				printf("%c",disc[peg[j][i]][k]);
                printf("  ");
		}

     printf("\n");
     
	}
    
	for (i=0; i<81; i++) printf("-");


    printf("\n\n\n");
    
	return;
    
 }

Clarification of Question by phelsva-ga on 21 Apr 2003 11:36 PDT
For the things I need done. You need to put the data into Microsoft
EXCell to  and then draw the graph using excell

Request for Question Clarification by maniac-ga on 21 Apr 2003 14:19 PDT
Hello Phelsva,

On the clarification, would it be OK to post the columns of data so
you can copy / paste into Excel. I can certainly generate an Excel
spreadsheet with the results, but cannot post the spreadsheet itself
as part of the answer.

You also mention that the driver program compiles cleanly & works on
your system. Due to the time limit, please describe that system (e.g.,
operating system, compiler) so the answer will build and run cleanly
on that system.

  --Maniac

Clarification of Question by phelsva-ga on 21 Apr 2003 19:40 PDT
yes you can do post the columns of data so i can copy / paste into Excel.

I used the microsoft visul C++ compiler

Clarification of Question by phelsva-ga on 21 Apr 2003 19:41 PDT
I used the microsoft Visual C++ compiler

Clarification of Question by phelsva-ga on 21 Apr 2003 19:46 PDT
if you choose to use a different compiler, show proof of compilation.

Clarification of Question by phelsva-ga on 21 Apr 2003 19:57 PDT
even though i was able to run the program, please check to see if the
code is robust and good. let me know if you make any changes
Answer  
Subject: Re: Easy tower of hanoi program
Answered By: dogbite-ga on 21 Apr 2003 21:15 PDT
Rated:4 out of 5 stars
 
Hello phelsva-ga,

  I am nearly done benchmarking and capturing the output from
  your program.  I am still waiting for the results from the
  n=30 disks execution.  I will post all your requested output
  within two hours from now.

  So you know I'm working on it, the first few values are:

n   time
2   0m0.004s
5   0m0.010s
10  0m0.413s
15  0m19.236s
20  13m29.675s
30  still running

   I will be back with more shortly.

        dogbite-ga

Clarification of Answer by dogbite-ga on 21 Apr 2003 22:48 PDT
Hello phelsva-ga,

  Here are some more results.  First, I do not think you
  want the program's output for n=30.  In trying to run
  it for that n, the output exceeded 12G in size.  You
  couldn't possibly look through 12G of text, could you?

  I have put up two files with captured output:

http://nms.lcs.mit.edu/~gch/google/hanoioutsmall.txt.gz
    - contains results for n=3,5,10
    - 15k in size

http://nms.lcs.mit.edu/~gch/google/hanoiout.txt.gz
    - contains results for n=3,5,10,20
    - 23M in size

  My execution of the program for n=30 still has not
  finished, and I expect it to take a good deal of
  time still.  The program's algorithm grows exponentially
  with n, so it will take many hours to complete.
  When (if) it finishes, I will post the running time
  for your plot.

  Here are the running times again, along with
  the number of moves needed.

n   time           moves
2   0m0.004s       3
5   0m0.010s       31
10  0m0.413s       1023
15  0m19.236s      32767
20  13m29.675s     1048575
30  still running  1073741823

  Is that all that you need?  Please let me know.

        dogbite-ga

Clarification of Answer by dogbite-ga on 23 Apr 2003 16:20 PDT
Well, 4/23 has rolled around and the n=30
  execution has still not finished.  I imagine
  that is one of the points of this assignment.

  I hoped this helped you.

           dogbite-ga

  P.S. Please don't forget to tip extra helpful researchers!

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 17:45 PDT
Hello Maniac
  I'll be most gratefuf if you could let me have the number of moves
associated with your results.

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 17:47 PDT
thanks dogbite.
          I'm a new user of this system, I'll like to tip you, but I
dont know how since I already paid maniac for his info. Please let me
know how.
I could not open the link to your results for n=3,5,10,20.

Clarification of Answer by dogbite-ga on 23 Apr 2003 17:52 PDT
Hello phelsva-ga,

  Actually, I think you paid me, not
  maniac, for the answer.  He commented
  on it, but I provided the official
  "answer."

  What error do you get when you try to
  open the link?  It seems to work for 
  me just fine.

  And, for future reference, you tip when
  you give the star rating.  Tipping is
  described here, in the FAQ:

http://answers.google.com/answers/faq.html#tipping

       I hope the results were sufficient!
       
             dogbite-ga

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 18:05 PDT
hello dogbite,

             It tells me, I don't have enough memory space anytime I
try to open it.

Also could you give me an approximation for n=64, just as maniac did.

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 18:08 PDT
Dogbite
also what kind of graph do you get

Clarification of Answer by dogbite-ga on 23 Apr 2003 18:25 PDT
Yes, you probably cannot open the file
  because it is very large. It is over
  30M, which is a very large file to open.
  The program generates a *lot* of output,
  even for n=20.

  I estimate it would take a little over
  10^16 seconds to finish n=64.  Also,
  n=64 has 2^64 - 1 = 18446744073709551615
  moves.

  I also got an exponential growth plot, where
  the exponent was very close to 2.

           dogbite-ga

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 18:34 PDT
hello dogbite,
             I was able to open the small  files, I just wanted to
know if you saved it in notepad.Because it came out distorted.Does
this file show the steps

Clarification of Answer by dogbite-ga on 23 Apr 2003 18:46 PDT
Hey phelsva-ga,

  I ran the program on a UNIX system.
  Try this file and see if it works
  for you.

http://nms.lcs.mit.edu/~gch/google/hanoioutsmallDOS.txt.gz

       dogbite-ga

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 19:24 PDT
Hello dogbite , i tried to open the link you just sent, but I  got
this error.

Forbidden
You don't have permission to access
/~gch/google/hanoioutsmallDOS.txt.gz on this server.


--------------------------------------------------------------------------------

Apache/1.3.27 Server at nms.lcs.mit.edu Port 80

Request for Answer Clarification by phelsva-ga on 23 Apr 2003 19:30 PDT
Dogbite

  I don't know what is wrong with my system, but I just tried to
execute my program and it gave me  2 errors, did you also get these
errors. I would be most grateful if you could send me a file
containing exactly what you got when you compiled and executed the
program.

I don't mean in the form  n,  time,  moves. But how it appears when
you execute.

Clarification of Answer by dogbite-ga on 23 Apr 2003 20:20 PDT
I'm sorry phelsva, please try the 
  DOS link again.  

  And here, I'll paste the program
  output for two disks.

 Please input the number of disc(1-12):Hanoi Tower with 2 discs:
====================================================

             *|*           ^@              |            ^@            
 |            ^@
            **|**          ^@              |            ^@            
 |            ^@
---------------------------------------------------------------------------------


move disc 1 from peg A to B
              |            ^@              |            ^@            
 |            ^@
            **|**          ^@             *|*           ^@            
 |            ^@
---------------------------------------------------------------------------------


move disc 2 from peg A to C
              |            ^@              |            ^@            
 |            ^@
              |            ^@             *|*           ^@           
**|**          ^@
---------------------------------------------------------------------------------


move disc 1 from peg B to C
              |            ^@              |            ^@            
*|*           ^@
              |            ^@              |            ^@           
**|**          ^@
---------------------------------------------------------------------------------


 Please input the number of disc:Hanoi Tower with 0 discs:
====================================================


        dogbite-ga
phelsva-ga rated this answer:4 out of 5 stars

Comments  
Subject: Re: Easy tower of hanoi program
From: maniac-ga on 22 Apr 2003 04:49 PDT
 
Hello Phelsva,

A few comments on your program:
 - the basic program works OK, but as dogbite mentions, it generates a
*lot* of output. Moves are 2^n-1 (1 disk = 2-1, 2 disks = 4-1, 3 disks
= 7-1, and so on. With the original program, the output is 58, 290,
and 14388 lines long for 3, 5, and 10 disks respectively.
 - with a small number of disks, the time to start the program is a
dominant factor in run time. Without the output, the wall clock time I
measured using "time" was...
Disks	Time
3	0.045
5	0.03
10	0.028
15	0.06
20	0.429
22	1.571
24	6.191
26	24.65
28	98.353
30	394.194
where the times are in seconds. Plot on a chart with linear X and
logarithmic Y will show a straight line from 20 through 30; the rest
of the points are in an odd U shape. As mentioned above, the odd shape
is due to system effects - not the algorithm.
 - using the data for 20 through 30, Excel logest has a result of
1.98258023, 4.67034E-07. This compares closely to the expected
exponent near 2 and constant near zero.
 - using that same data, Excel logest has a result of 4.92173E+12 for
x=64, and 385.7198541 for 30 (a reasonably close check).

Good luck on your work.
  --Maniac
Subject: Re: Easy tower of hanoi program
From: missy-ga on 23 Apr 2003 18:37 PDT
 
Hello Phelsva!

It's nice to see new customers about!

A small point of reference should you choose to use our services in
the future:

Tipping is *purely optional*.  Please be assured that it is not
required, nor is it approved practice to "remind" or pressure our
customers to do so.

--Missy

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy