Towers of Hanoi Iterative process: simulating recursion

The last post Recursive Solution to Towers of Hanoi described the well-known recursive definition and implementation of the Towers of Hanoi problem. This post is an extension presenting the same problem iteratively by simulating the recursion stack. This implementation will simply to simulate the recursion presented on the previous post by using an explicit manual stack.

Continue reading “Towers of Hanoi Iterative process: simulating recursion”

Advertisement

Recursive Solution to Towers of Hanoi

Towers of Hanoi is a mathematical game or a puzzle in which there are three pegs, and some disks (originally 8) of different radius placed on top of one another such that no larger disk is placed on a smaller disk. The task is to transfer such a column of disks from a source peg to another destination peg. The constraints are we can move only one disk at a time, and we may use the third peg as a temporary storage for the disks, and a larger disk cannot be placed on top of a smaller disk.

The puzzle was invented by the French mathematician Édouard Lucas in 1883. There is a legend about an Indian temple which contains a large room with three time-worn posts in it surrounded by 64 golden disks. Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks, in accordance with the rules of the puzzle, since that time. The puzzle is therefore also known as the Tower of Brahma puzzle. According to the legend, when the last move of the puzzle is completed, the world will end.[2] It is not clear whether Lucas invented this legend or was inspired by it.

Wikipedia

In this post I will describe the basic recursive solution to the Towers of Hanoi
Continue reading “Recursive Solution to Towers of Hanoi”

Accessing private data members directly outside from its class in C++

Private members in C++ cannot be accessed from outside the class directly. In this post I will tell an interesting fact, that in C++ however there are means with which you can access/modify the values of the private members of a class from outside of the class, when the structure of the class is known.

Continue reading “Accessing private data members directly outside from its class in C++”

Input Strings with Blankspaces

In CLI programs getting input from the standard input is very important. Often we need to input strings from the user. Strings are delimited by newline, blankspace character and other characters as per the user interpretation. I am discussing on some ways here with which we can take input strings with blankspaces in it safely.
Continue reading “Input Strings with Blankspaces”

Allocating multidimentional array at runtime in C

Arrays in C language are static in nature, and cannot be resized at runtime. The effect of runtime allocation of an array could be achieved by hierarchically allocating blocks of memory at runtime. Below we discuss the mechanism to allocate multidimensional arrays.
Continue reading “Allocating multidimentional array at runtime in C”

C Language Constructors and Destructors with GCC

Constructors and Destructors are special functions. These are one of the features provided by an Object Oriented Programming language. Constructors and Destructors are defined inside an object class. When an object is instantiated, ie. defined of or dynamically allocated of that class type, the Constructor function of that class is executed automatically. There might be many constructors of which the correct implementation is automatically selected by the compiler. When this object is destroyed or deallocated, the Destructor function is automatically executed. For example when the scope of the object has finished or the object was dynamically allocated and now being freed. The Constructors and the Destructors are generally contains initialization and cleanup codes respectively required by an object to operate correctly. Because these functions are automatically invoked by the compiler therefore the programmer freed from the headache of calling them manually.

Continue reading “C Language Constructors and Destructors with GCC”