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.
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. It is not clear whether Lucas invented this legend or was inspired by it.
In this post I will describe the basic recursive solution to the Towers of Hanoi
Continue reading “Recursive Solution to Towers of Hanoi”
Beginners find that the scanf function behaves strangely when you input a character when scanf expects an integer or float. When used inside a loop (taking options), it loops infinitely. Here is a short post which explains what it happens, and how to solve this,
Continue reading “Using scanf safely”
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.
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”
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”
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.
A brief introduction to the Simpson’s 1/3rd rule and a uniform interval Composite Simpson’s 1/3rd Rule implementation.
Continue reading “Simpson’s 1/3rd Rule”
A brief introduction to the Trapezoidal rule and a uniform interval Composite Trapezoidal Rule implementation.
Continue reading “Trapezoidal Rule”
To check if an entered string containing nested parenthesis are balanced and valid. This write-up presents a function with which a string is checked for balanced parenthesis.
Continue reading “Balanced Parenthesis Check”