0% found this document useful (0 votes)
126 views4 pages

C Interviewquestions

The document describes the Towers of Hanoi problem and provides a C++ program to solve it. It explains the recursive approach to move n disks from the starting pole to the target pole using an auxiliary pole. It provides the code with functions to print the poles, move a disk, and recursively solve the problem. It also includes sample output showing the steps to move 4 disks.

Uploaded by

Roshinee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views4 pages

C Interviewquestions

The document describes the Towers of Hanoi problem and provides a C++ program to solve it. It explains the recursive approach to move n disks from the starting pole to the target pole using an auxiliary pole. It provides the code with functions to print the poles, move a disk, and recursively solve the problem. It also includes sample output showing the steps to move 4 disks.

Uploaded by

Roshinee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks

Programming Tutorials by SourceTricks


(http://www.sourcetricks.com/)
3:19:00 PM SourceTricks (https://www.blogger.com/profile/17421383822093860960)
Programming interview questions and answers (http://www.sourcetricks.com/search/label/Programming%20interview%20questions%20and%20answers)
2 comments (http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#comment-form)

Towers of Hanoi (http://www.sourcetricks.com/2014/01/towers-of-hanoi.html)

Approach to solve the Towers of Hanoi game


1. We need to move the n rings from the first pole to target third pole using the second pole as buffer.

2. First move the n-1 rings from first to a second pole using the third.

3. Then move the remaining big ring from first to the third.

4. Now move all the n-1 rings from the second pole to the target third pole on top of the nth (which is big).

5. For moving the n-1 rings from one pole to another pole, use the remaining pole as a buffer using recursion.
C++ program to to solve the Towers of Hanoi game

http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 1/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks

#include <iostream>
using namespace std;

#define MAX 4
int fromPoleMain[MAX] = {4,3,2,1};
int tmpPoleMain[MAX];
int toPoleMain[MAX];

void printPole(int *pole)


{
if (fromPoleMain == pole)
{
cout << "ONE ";
return;
}
if (tmpPoleMain == pole)
{
cout << "TWO ";
return;
}
if (toPoleMain == pole)
{
cout << "THREE ";

return;
}

void move(int* fromPole, int* toPole)


{
int i,j;
for(i = 0; i<MAX;i++)
{
if(fromPole[i] == 0)
break;
}

for(j = 0; j<MAX;j++)
{
if(toPole[j] == 0)
break;
}
toPole[j] = fromPole[i-1];
printPole(fromPole);
cout << ": posi " << i << " to --> ";
printPole(toPole);
cout << ": at posi " << j ;
cout << " moving " << toPole[j] <<endl;
fromPole[i-1] = 0;

void TowersOfHanoi(int n, int* fromPole, int* toPole, int* tmpPole )


{
if (n==0) return; // no more rings to move.
TowersOfHanoi(n-1, fromPole, tmpPole, toPole);
move(fromPole,toPole);
TowersOfHanoi(n-1, tmpPole, toPole, fromPole);
}

int main()
{
printPole(fromPoleMain); printPole(toPoleMain); printPole(tmpPoleMain); cout << ": " << endl;

TowersOfHanoi(MAX,fromPoleMain,toPoleMain,tmpPoleMain);
cout << "Finally target POLE: ";
for(int i = 0; i<MAX;i++)
{
cout << toPoleMain[i] << " ";
}
cout << endl;
return 0;
}

Output:-

http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 2/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks

ONE THREE TWO :


ONE : posi 4 to --> TWO : at posi 0 moving 1
ONE : posi 3 to --> THREE : at posi 0 moving 2
TWO : posi 1 to --> THREE : at posi 1 moving 1
ONE : posi 2 to --> TWO : at posi 0 moving 3
THREE : posi 2 to --> ONE : at posi 1 moving 1
THREE : posi 1 to --> TWO : at posi 1 moving 2
ONE : posi 2 to --> TWO : at posi 2 moving 1
ONE : posi 1 to --> THREE : at posi 0 moving 4
TWO : posi 3 to --> THREE : at posi 1 moving 1
TWO : posi 2 to --> ONE : at posi 0 moving 2
THREE : posi 2 to --> ONE : at posi 1 moving 1
TWO : posi 1 to --> THREE : at posi 1 moving 3
ONE : posi 2 to --> TWO : at posi 0 moving 1
ONE : posi 1 to --> THREE : at posi 2 moving 2
TWO : posi 1 to --> THREE : at posi 3 moving 1
Finally target POLE: 4 3 2 1

Related Posts:
Linked list nth last element (http://www.sourcetricks.com/2012/07/linked-list-nth-last-element.html)
Write a program to find in a linked list nth last element The approach:- Maintain 2 pointers to head of the list. Move 1st pointer n - 1 elements. Now move… Read More
(http://www.sourcetricks.com/2012/07/linked-list-nth-last-element.html)

Compute the endianness (http://www.sourcetricks.com/2012/07/compute-endianness.html)


Write a program to compute the endianness of a system Endianess In computing, the term endian or endianness refers to the ordering of individually addressable…
Read More (http://www.sourcetricks.com/2012/07/compute-endianness.html)

Shuffle a pack of cards (http://www.sourcetricks.com/2012/07/shuffle-pack-of-cards.html)


Write a program to shuffle a pack of cards The approach:- Initialize a cards array and set the seed for rand based on time. For each array index generate a … Read
More (http://www.sourcetricks.com/2012/07/shuffle-pack-of-cards.html)

Find if two strings are anagrams (http://www.sourcetricks.com/2012/07/find-if-two-strings-are-anagrams.html)


Write a program to find if two strings are anagrams Anagram - Wikipedia An anagram is a type of word play, the result of rearranging the letters of a word or … Read
More (http://www.sourcetricks.com/2012/07/find-if-two-strings-are-anagrams.html)

Reverse a linked list (http://www.sourcetricks.com/2012/07/reverse-linked-list.html)


Write a program to construct and reverse a linked list We write an iterative approach to reverse a linked list. The approach is to start from the head node an… Read
More (http://www.sourcetricks.com/2012/07/reverse-linked-list.html)

← Newer Post (http://www.sourcetricks.com/2014/01/sax-parser-to-read-xml-file-in-java.html)


Older Post → (http://www.sourcetricks.com/2014/01/find-loop-in-linked-list-and-identify.html)

2 comments :

chenmeinv0 (https://www.blogger.com/profile/04850790224822937115) December 29, 2016 at 11:16 AM (http://www.sourcetricks.com/2014/01/towers-of-


hanoi.html?showComment=1482990396577#c5837402676282437270)

gucci belts (http://www.gucci-outlet.org)


air jordan shoes (http://www.jordanshoes.us.org)
oakley sunglasses outlet (http://www.oakley-sunglasses.net.co)
oakley sunglasses outlet (http://www.cheapoakleysunglasses.net.co)
ralph lauren outlet (http://www.poloralphlauren.nom.co)
air jordan shoes (http://www.cheapjordansshoes.us.com)
ralph lauren (http://www.ralphlaurenoutlet.nom.co)
coach outlet store online (http://www.coachfactoryoutlet-online.us.org)
ray ban sunglasses outlet (http://www.ray-banssunglasses.com.co)
tory burch sale (http://www.cheaptoryburchoutlet.com)
2016.12.29xukaimin

Reply

Eon Mech (https://www.blogger.com/profile/10147433905545861008) January 8, 2017 at 11:27 AM (http://www.sourcetricks.com/2014/01/towers-of-hanoi.html?


showComment=1483855064506#c6125981469315446088)

thanks for sharing , very informative stuff

red hat linux training in chennai (http://sysacad.in/portfolio-view/linux-training-in-chennai/) | rhce courses in chennai (http://sysacad.in/portfolio-view/linux-training-in-
chennai/) | red hat training in chennai (http://sysacad.in/portfolio-view/linux-training-in-chennai/) |red hat courses in chennai (http://sysacad.in/portfolio-view/vmware-
training-in-chennai/)

Reply

http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 3/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks

Enter your comment...

Comment as: Unknown (Goo Sign out

Publish Preview Notify me

(https://www.blogger.com/comment-iframe.g?blogID=7748177500667831327&postID=861168250104027697&blogspotRpcToken=3526398)

Subscribe to: Post Comments ( Atom ) (http://www.sourcetricks.com/feeds/861168250104027697/comments/default)

Tutorial Pages

Java Tutorials (http://www.sourcetricks.com/p/java-tutorials.html)

Micro Services (http://www.sourcetricks.com/p/microservices.html)

Java Interview Questions (http://www.sourcetricks.com/2014/06/core-java-interview-questions.html)

Scala Tutorials (http://www.sourcetricks.com/p/scala-tutorials.html)

Programming in C++ (http://www.sourcetricks.com/p/programming-in-c.html)

Programming interview questions and answers in C++ (http://www.sourcetricks.com/p/programming-interview-questions-and.html)

Data Structures using C++ (http://www.sourcetricks.com/p/data-structures-using-c.html)

Algorithms in C++ (http://www.sourcetricks.com/p/algorithms-in-c.html)

Design Patterns using C++ (http://www.sourcetricks.com/p/design-patterns-using-c.html)

Android (http://www.sourcetricks.com/p/android.html)

HTML, CSS and Javascript (http://www.sourcetricks.com/p/html-css-and-javascript.html)

UML Notations (http://www.sourcetricks.com/2008/05/uml-generalization.html)

Tag Cloud

Java (http://www.sourcetricks.com/search/label/Java) CPP


(http://www.sourcetricks.com/search/label/CPP) Programming interview questions and
answers
(http://www.sourcetricks.com/search/label/Programming%20interview%20questions%20and%
20answers) Design Patterns (http://www.sourcetricks.com/search/label/Design%20Patterns) Scala
(http://www.sourcetricks.com/search/label/Scala) Android (http://www.sourcetricks.com/search/label/Android) Algorithms
(http://www.sourcetricks.com/search/label/Algorithms) Data Structures (http://www.sourcetricks.com/search/label/Data%20Structures) Micro Services
(http://www.sourcetricks.com/search/label/Micro%20Services) JavaScript (http://www.sourcetricks.com/search/label/JavaScript) tools
(http://www.sourcetricks.com/search/label/tools) UML (http://www.sourcetricks.com/search/label/UML) html (http://www.sourcetricks.com/search/label/html)

http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 4/4

You might also like