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”

Using scanf safely

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”

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

Bash Script: Reading ID3v1 Tags

mp3 files contain track name, artist name, etc meta data about the music file itself. The meta data format used in mp3 files are called the ID3 tags. There are different versions of ID3 tags available. The objective is to read this meta data information from an mp3 file having an ID3v1 tag.

A detailed overview of the ID3v1 (as well as ID3v2 tags) are done here in this post: What are ID3 Tags all about?. A small ID3v1 library is also implemented (reading and writing tags) in C language here: An ID3v1 Tag Parsing Library. TagLib is available to read and write ID3v1 and ID3v2 tags and is free to use.
Continue reading “Bash Script: Reading ID3v1 Tags”

Reviewing the GNUSim8085 (v1.3.7)

This article reviews the GNUSim8085 (v1.3.7) a graphical simulator, assembler and debugger for the Intel 8085 microprocessor.

The Intel 8085 is an 8-bit microprocessor that was launched by Intel in 1977 and hence is something that we have never seen around. So why do we need to learn about the programming model and instruction set of this old microprocessor? The microprocessors we use are all for general-purpose computing but there are other applications of computers such as making automatic solar tracking-panels, automatic power-controls, or security control systems. For such applications, general-purpose CPUs are unnecessary-a waste of resources; a low powered CPU is what we need. The Intel 8085 is one of the candidates. To program this piece of hardware, we first need to know the programming model-the logical structure of the programmable registers, flags, and the instruction set.

Continue reading “Reviewing the GNUSim8085 (v1.3.7)”

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”