473,382 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 25063
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.