# tower of honai

Email
 Submitted on: 1/3/2015 10:39:00 AM By: Rehan Ahmed Khan (from psc cd) Level: Advanced User Rating: By 2 Users Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++ Views: 1311

in recursion we have limited stack to keep data. so when we try to find the solution of tower of honai for 5, we get in trouble. to solve this problem we've non recursive solution of it.

### INCLUDE files:

Can't Copy and Paste this?
 ```//************************************** //INCLUDE files for :tower of honai //************************************** no.```
code:
Can't Copy and Paste this?
 ``` //************************************** // Name: tower of honai // Description:in recursion we have limited stack to keep data. so when we try to find the solution of tower of honai for 5, we get in trouble. to solve this problem we've non recursive solution of it. // By: Rehan Ahmed Khan (from psc cd) // // Inputs:it take only one input.(number of plates u have) // // Returns:it returns all the movement of the plates b/w the towers // // Assumes:nothing special. but i'll be better if he knows about recursive solution of tower of honai. // // Side Effects:nothing. //************************************** #include #include #define ARRAYSIZE 100 /************************************************************************************************* PROGRAMMED BY : REHAN AHMED KHAN PAKISTAN- KARACHI BS student in FAST University (foundation for advancement of science and technology) this program is free ware and anyone can use it. /************************************************************************************************/ /************************************************************************************************* * THE ALGORITHM USED IS AS FOLLOWS * * * * Because the tree is complete it can be stored in an array of 2^n-1. * * The root of the tree is in position 2^(n-1)-1 * * For any level j, the first node at that level is in position 2^(n-1-j)-1 * * and each successive node at that level is 2^(n-j) elements beyond the previous element * * at that level * * So the sequential traversal of the array produces the solution of Towers of Hanoi Problem * * * *************************************************************************************************/ class TowersHanoi { private: struct //node structure { char chToPeg; char chFromPeg; int iDiskNo; }node[ARRAYSIZE]; //Array of Nodes char except(char chFromPeg, char chToPeg); //returns peg other than provided as parameter public: TowersHanoi(); //constructor //generates the tree void generateTree(char chFromPeg,char chToPeg, int iDiskNo); }; ////////////////////////////////////////////////////////////////////////////////////////////////// TowersHanoi::TowersHanoi() { int iDiskNo; cout << "\nENTER THE NUMBER OF DISKS >> "; cin >> iDiskNo; generateTree('A','B',iDiskNo); int cMoves=(int)pow(2,iDiskNo)-1; cout << "THE MOVES ARE AS FOLLOWS\n"; for(int i=0;i

Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)