473,471 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Towers of Hanoi

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++

Please help.

Thanks for your time.
Jul 19 '05 #1
8 25072
Constant escribió:
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++

Please help.

Thanks for your time.

Here you are a program wich solves the problem:

#include <iostream>
using namespace std;

void hanoi (int nDisks, int pegA, int pegB, int pegC)
{
if (n>0) {
hanoi (nDisks - 1, pegA, pegC, pegB);

cout << "Move disk from peg " << pegA << " to peg " << pegC << endl;

hanoi( nDisks-1, pegB, pegA, pegC);
}
}

int main ()
{
int n;
cout << "Input number of disks: ";
cin >> n;

hanoi (n, 1, 2, 3);

return 0;
}

Jul 19 '05 #2


Alexandros wrote:


Please do not solve other peoples homework.
Give him some hints, try to make him thinking about
the problem, but do not post a program he can turn in
without actually having mastered the problem by himself.

Hanoi is often used to check if a student has grasped
the concept of recursion. He needs to build up a mental
model on how recursion works in order to solve it with
recursion:
a) What makes the problem complicated?
b) What parameter can be made simpler, such that
finally this parameter reaches 0 or 1 or any other
trivial case.
c) What is the trivial case, how can it be solved?
d) How can I use a simpler solution to find the solution
for the more complex problem.

By providing him a complete workable solution,
you have defeated that intention.

Thank you.

PS (for the OP):
a) the number of disks. Obviously, if no disk must be moved
the problem is so simple, that even a 2 year old child can solve
it: do nothing.
b) again, the number of disks. Moving n-1 disks is easier then
moving n disks.
c) Having to move 0 disks. No action is required then.
d) To move n disks from a StartPad to GoalPad with the help
of a ParkingPad:
* move n-1 disks from StartPad to ParkingPad, in order to free
the n-th disk for movement. You can use the GoalPad as the parking pad.
* move the n-th disk from StartPad to GoalPad
* move the n-1 disks you have parked at ParkingPad, from ParkingPad
to GoalPad, this time you can use the StartPad as the parking pad.

moving n-1 disks is simpler then moving n disks. Thus the above
uses the solution of simpler problems to solve the problem for n.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
Constant <co*******@yahoo.com> wrote in message
news:5c**************************@posting.google.c om...
I need a programe This is not a 'program supply' group. It's a group
for discussing the C++ language.
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++


No C++ knowledge or computer knowledge at all is required
to solve this problem. It can (and imo should) first be
done with paper and pencil. Once you've worked out and
tested (again on paper) your algorithm, only then is is
time to translate it into a programming language.

WHen you have some code representing your best attempt,
but are still stuck or confused, then by all means post it here
and ask specific questions. That's how to get quality assistance
here.

-Mike

Jul 19 '05 #4
co*******@yahoo.com (Constant) wrote in message news:<5c**************************@posting.google. com>...
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++

Please help.

Thanks for your time.


Do you know how to solve the problem in English or your native
language? Before you start to write any C++ you'll need to be able to
fully describe the algorithm in natural language. It's not very
complicated but you'll need to be precise. Once you've done that,
start translating it into C++.

Or are you at the coding stage already? If so, post compileable code
that demonstrates your problem. Include compiler messages and/or
program output that you don't understand and explain what you expect
to happen. Then people will be able to help.

Good luck.
GJD
Jul 19 '05 #5
Mike Wahler writes:
No C++ knowledge or computer knowledge at all is required
to solve this problem. It can (and imo should) first be
done with paper and pencil. Once you've worked out and
tested (again on paper) your algorithm, only then is is
time to translate it into a programming language.


The notion of making some marks on paper for a recursive solution makes me
slightly dizzy. I can't imagine what those marks would be or what they
would represent. IMO a recursive problem must be held and solved, to some
degree, in the head. A *vision*, if you will. The next step: a computer
program or at least pseudocode. Your mileage obviously varies.
Jul 19 '05 #6

osmium <r1********@comcast.net> wrote in message
news:bk************@ID-179017.news.uni-berlin.de...
Mike Wahler writes:
No C++ knowledge or computer knowledge at all is required
to solve this problem. It can (and imo should) first be
done with paper and pencil. Once you've worked out and
tested (again on paper) your algorithm, only then is is
time to translate it into a programming language.
The notion of making some marks on paper for a recursive solution makes me
slightly dizzy.


Then stop spinning around. :-)
I can't imagine what those marks would be
They would be e.g. names and values of variables at
given points of execution.

or what they
would represent.
Data and control flow.
IMO a recursive problem must be held and solved, to some
degree, in the head.
Some can do that easily, others have trouble.
A *vision*, if you will.
This 'vision' can be made concrete with a *pad* of paper,
on on top of the other. Each succeeding 'page' would
represent a level of recursion.
The next step: a computer
program or at least pseudocode. Your mileage obviously varies.


Yup.

-Mike

Jul 19 '05 #7
On 17 Sep 2003 03:34:59 -0700, co*******@yahoo.com (Constant) wrote in
comp.lang.c++:
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++

Please help.

Thanks for your time.


#include <check_calendar>

Yep, still September!
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #8
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++

Please help.

Thanks for your time.


The answer to this question can be found in the introduction to Concrete
Mathematics by Knuth and friends. There you will find a recursive
algorithm which performs the task you want, is proven, and is analyzed
for N so that we can finally answer the question about how long it will
take those monks to finish 1000 planks and we can all start over.

NR

Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
7
by: ashishnh33 | last post by:
hi.......is there nyone to tell me how to solve the "tower of hanoi" problems
3
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 ??? ...
51
by: maloov | last post by:
I have a pretty tough assignment for beginner like me & i'm seeking help please here is the assign In this assignment, you will be guided to complete the program skeleton provided to you in...
6
by: poopsy | last post by:
hi guys cud some1 explain to me the towers of hanoi code, the source code is available evrywhr. i've been trying to understand it bt cant. can some1 plz help me i hav even tried to "trace" it but i...
1
by: schoenfeld.one | last post by:
In this article we show that "top-down" controlled demolition accurately accounts for the collapse times of the World Trade Center towers. A top-down controlled demolition can be simply...
2
by: sunyboy | last post by:
hi I need a programe "Towers of Hanoi without use recessive function" in c++ Please help. Thanks for your time.
0
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...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.