473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c program to implement oprations on tower of hanoi

21 New Member
This operation involves push/pop operations on a stack,arranging the smaller discs to sit on larger ones.How can I write a program to implement this?
Oct 27 '06 #1
2 5034
Banfa
9,065 Recognized Expert Moderator Expert
Use linked lists to create your stack.
Oct 27 '06 #2
jessy
106 New Member
here's the code hunny ...

it took me 4 hours to get it ...tracing this code is like hell

# include <iostream.h>
# .....

void towers(int n,char frompeg,char topeg,char auxpeg)

{
if(n==1) //if only one disk just make the move and return
{
cout<<"\n"<<"mo ve disk 1 from peg"<<frompeg<< "to peg"<<topeg<<"\ n";
return;
}
towers(n-1,frompeg,auxpe g,topeg);
cout<<"\n"<<"mo ve disk"<<" "<<n<<"from peg"<<frompeg<< "to peg"<<topeg<<"\ n;

towers(n-1,auxpeg,topeg, frompeg);

}

void main()
{ int n;
cout<<"enter no of disks 2 be moved "<<endl;
cin>>n;
towers(n,'A','C ','B');
getch();

}
Oct 27 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6200
by: Sonia | last post by:
Hi, I have written this code for the Tower of Hanoi problem. I was just wondering if there is any way of optimizing this code ? It's a failry simple code, but I thought that maybe somehow it could be improved. Please let me know. Thanks #include <iostream> using std::cout;
1
3231
by: elsheikhmh | last post by:
Constant wrote: > > I need a programe that will deal with the solving of the problem ..i > have 3 pegs A, B, C...and I want to move the disk A to disk C using B > as auxiliary.At the end all disk should be at peg C in the same order > as in the beginning. > I do not know how to solve this problem because I have only basic > skill in C++ check:
2
1411
by: Marco Ippolito | last post by:
Hi, I am giving a seminar on C# interop and I would like to feature an original sample class to stimulate some interest. Does anybody want to write an open-source ConsoleApp || WinApp that, given a starting English word, calls Babylon for translations in N languages and plays a game with the user assigning one point for every language/translation pair entered? (I am a licenced user for 2004 :-)
7
652
by: ashishnh33 | last post by:
hi.......is there nyone to tell me how to solve the "tower of hanoi" problems
0
1325
by: huzaifaa | last post by:
The Towers of Hanoi is an old famous puzzle. The task is to build a program (in assembly language for the simulator of the 6800 used in the tutorial) that, given one input value (that indicates the number of disc in the left most peg) computes the minimum number of steps required to move the tower to the left most peg. Can any one help me out with this?
3
1951
by: kamvisiouma | last post by:
Hi there, I'm fresh in learning c++ and I have to solve an exercise with hanoi and my problem is that I'm running out of time! The exercise is this, and I have to complete the ??? #include<stdio.h> #define DISK_NUMBER 4 // There are 3 towers represents 3 stacks // The names of the disks are represented by numbers (1,2,3,...,n)
3
5821
by: shaghayeghcute2003 | last post by:
I'm currently trying to write The Tower Of Hanoi (http:// illuminations.nctm.org/ActivityDetail.aspx?ID=40) in C#. I have a panel on the form and I draw pegs on the panel inside the form class. I passed GraphicObject to drawDisk method in disk class to draw disks' pictures. I used MouseDown/Up/Move to move these disks on the panel. I'm attaching some part of my code: private void panel1_MouseDown(object sender,...
5
3952
by: seemanikam | last post by:
Can anyone suggest me how to devise an efficient divide-and-conquer algorithm for the Tower-of-Hanoi problem when the disks are colored alternately red and blue, and with the extra rule that no disk may be placed on any other disk of the same color.
11
4763
by: 960392954 | last post by:
The Towers of Hanoi is a famous problem about moving a certain number of disks from one peg to another.I write a recursive program for it.If you have known it ,you can skip the program. #include <stdio.h> void hanoi(int num,char initial,char terminal,char temporary); void main (){ int num;/*num is the number of disks of the initial peg*/ char a='a',b='b',c='c'; /*a is the initial peg ,c is the terminal peg,b is used as a temporary peg*/
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.