Posted in Non Tech

QR Codes – Omnipresent

I am amused at how the world has been revolutionized by technology. Recently in my nearby supermarket, I saw a simple placard having a QR code powered by something called Paytm (it’s an acronym of Pay Through Mobile). This placard, was not only in that shop, I could see it on the cabs, petrol pumps and even in the parkings at a few places. The technology that this little placard uses is the QR code. What is more ironical is that this QR code was used by Japanese automobile companies to identify their machine parts, and now these codes are used for identifying humans – yes, you will find this code on the visiting Cards of people and even on their T-Shirts. Continue reading “QR Codes – Omnipresent”

Advertisements
Posted in Non Tech

Rains – Boon or Catastrophe

A few days ago on a fine evening, while returning to my home from my institute on my two wheeler, suddenly it started raining heavily. I did have my raincoat and a bag cover for my rescue, but yet the downpour was heavy enough to drench me through the neck opening alone. Such a heavy rain and that too just after it had rained in the morning flooded all the roads, and soon the roads transformed into pools. I was wondering that these heavy rains must be a chaos for the people. In some way I was wrong.

Continue reading “Rains – Boon or Catastrophe”

Posted in C++

ZCO 2013 – Solution : Tournament

The problem statement is described here.

The first impulse would be to calculate all the match revenues and then sum up… but it would be O(n^2) which isn’t good enough for the second subtask.

The key to the problem lies in an observation. Let’s consider an examples and then generalise it.

Let us take a different example than the given sample case.

4

3 10 7 5

Now the revenues for each match are :

10-5 +10-7 + 10-3 + 7-5 + 7-3 + 5-3 = 23

but wait that’s not what we will do… Look at it in the following manner :

Continue reading “ZCO 2013 – Solution : Tournament”

Posted in C++

Making a User friendly menu in old compilers for c++

This post is specially for those poor students who still have to use older compilers (Like Turbo C++). What I really mean by saying old is that a compiler supporting <CONIO.H > . This is no standard header file (and there is a specific reason to it too) but helps in creating Better interface for the USER.

What do we actually do to create a graphical interface ? We create colored illusions which make it seem that the interface is interactive. The top commands for this work are :

void textbackground(int );

void textcolor(int);

void gotoxy(int,int);

void clrscr(void);

char& getch(void);

Now to the working :

What happens when we have a menu and we want the current item to look like it’s selected, the special emphasis is brought about by a different color. Same thing we will also do here. Now a bit of coding manipulations :

Code for making a menu (without graphic interface) :

char* menu_items[] = {"Item 1","Item 2","Item 3"};   //Change these as per requirement
int n=3; // This variable stores the number of menu items we have
for(int i=0;i<n;i++)

{
     cout  <<   i+1   <<  " "  ;
     puts(menu_items[i]);
}                //Insert some command for pausing if required

This loop will display all our menu items along with their serial number.
Now a simple example on using the coloring commands
The output is like this :-

Plain menu
Plain menu without any graphical interface

Now the basic trick is to detect what button the user has pressed. To do this we will use the getch() command. {Note that getch() can appear on the right side of an expression and also as a normal function call}. The getch() command returns us the ascii value of the key we have hit. The tricky part here is that when the user presses an arrow key, the ascii value returned is not just one but two, So we need not only one getch() but two of them.

Continue reading “Making a User friendly menu in old compilers for c++”

Posted in C++

Overloading operator’s (in C++)

Here I will talk about overloading. Let’s start by formally defining what overloading is :

Specifying more than one definition’s for a function is called overloading.

Why do we need it ?

Now let’s say I have made a sort function. This function is sufficient for sorting an integer array, but what about an array with decimal number’s or a string. Again for sorting these data types we need more functions consisting of the same algorithm. Suppose we wanted to sort 4 different types of data types, we will have to make four different functions, and every-time I call the function I will need to decide what function to call. Wouldn’t it be nice if the compiler itself decided which function to call or if there were a self adjusting mechanism within the function to adjust according to input. This is why we need overloading. There is another mechanism to achieve the same and that is called a template, but overloading makes it easier to read the code at many occasions.

How it works ?

If you’ve been using standard c++ input methods then the familiar command cin and cout use operator >> and << respectively for functioning. Now these operators are actually overloaded. << and >> are the right shift and left shift operator’s originally, but the iostream file that we include in our programs has overloaded these operators to the extraction and insertion operators and thus now they function like that when used with cin and cout. These operators function as normal when not used with cin and cout.

This is how overloading works. Now to get to the implementation part, we will take up an example to get this better.

Let us make our own class for representing a fraction. A fraction has two parts, a numerator and a denominator, so our class will also have these two information. Here is the class prototypes :
Continue reading “Overloading operator’s (in C++)”