its as beautiful as a painting...its as delightful as a poem...
U Think its Crazy? then its always a pleasure to go the crazier way....
lets give it a try... just follow and u will find something interesting...

Thursday, December 30, 2010

Beautiful Code- converting Binary to Decimal

When writing this program in my first year, i used scanf to input the binary number into an integer, however, i later realised that the int datatype is designed to store decimal numbers and using them to take input of a binary STRING is unjustified because when we write 10011 in decimal the place value increases in powers of 10 while in case of binary numbers the place value increases in powers of two. And even if you take it in integer you will face two problems
first- you will have to write a complex loop to segregate the binary number into bits and then calculate the place value and face value of each bit
Second and more important you will cross the datatype range very soon. Even if you use a 4 byte compiler you will be able to go to maximum 10 bits which in decimal form means 1024.
So, I suggest taking the binary input in a String rather than int. Furthermore, while taking input in a string it shall be checked that each bit input is 1 or 0 but nothing else.
After taking the input in a string we can easily calculate the place-value of each bit as the index position of that bit and subtracting the index of last bit entered(note: for this it is necessary to take the input in reverse order, i.e. the most significant bit should have highest index).
Below is the C code for Binary to Decimal conversion, you can get many different codes for this but i am trying to write optimized and justified lines of code.. 

#include"stdio.h" /*header file for standard input output functions like printf */
#include"math.h"  /*header file for math functions like pow */
#include"conio.h" /*header file for console input output like getch */
#include"stdlib.h" /*header file for exit function */
int main()
{
   char ch,str[20];
    int i,j;
   unsigned int decimal=0;

printf("\n enter the binary no.(not more than 20 bit) :\n");

for(i=20;i>0;i--)
{
   ch=getche();
       if(ch=='1' || ch=='0')
                  str[i]=ch;
  else if(ch==13) /*it means if the enter key is pressed, because enter key gives the character \r with ascii 13*/
     {
        str[i]='\0'; /* in C a string is terminated by null character */
        j=i; /*we need to remember this index where the string is terminating */
        break;
      }
   else
   {
     printf("wrong binary string, program terminating");
      getch();
      exit(1);
   }
}
for(i=20;i>0;i--)
{
if(str[i]=='\0')
   break;
else
{
   decimal=decimal + ((int)str[i]-'0')*pow(2,i-j-1);
}
}

printf("\n decimal value is %d",decimal);
getch();
return 0;
}

I m Waiting for suggestions, criticism and comments...

Wednesday, December 29, 2010

Art and Programming?

Have you ever painted..or written a poem? what do you really do.. bring out your feelings and potray them into beautifully arranged colors or words.. and still you think programming is a boring stuff.. Just look at it once again.. is it not the expression of your imaginations into some language.. the creation of your logic and even stronger one.. it affects the digital world.. the non-living machines come alive..start communicating with you.. searching answers to your problems.. and in the well organized way.. the exact way you want.. your poems or paintings may give different message to different people. but your programs generate exactly the same expression of your creativity every time it is run.. is it not more interesting to bring the non-living alive than trying to manipulate the living world..

To write a poem you work with a basic theme, you design lines, verses, finally organize them..
To make a painting you choose a theme, you select colors, you decide what shades to use and finally get at work..
To write a beautiful code you think of the central logic, you decide procedures and sub-procedures to design, you arrange the expressions and finally organise them..

You have got a set of colors..you get pleasure mixing them, producing new colors, new shades, new visual effects..You have got a strong vocabulary, you love arranging words in new permutations to generate beautiful effects.. i have got my library of functions, i love creating different structures using them, nesting them and creating different beautiful codes...

you write a poem..you love to get some patient listener who may understand it and praise your effort.. you make a painting.. you would love to see people looking at it, praising the beauty.. i write codes, and love to have someone who appreciates the beautiful logic that reveals secrets of programming and can make people aback..

You spend hours improving the verses of your poem.. you spend hours improving shades of your paintings.. i love spending my hours improving my codes to make them work most beautifully in extreme worst cases.. verifying its different aspects... testing it for all limiting conditions...exploring all different possibilities..revealing hidden truths of program behavior..

You may love programming..you may love arts.. the pleasure is undefined...
Programming is Art and art is a program of letting your soul go unconfined...

Lets C

Most people with a superficial programming experience ask whats so important about this one language.. what have kept it alive and going for 4 decades in such a dynamic industry where new technologies are announced every day.. why do software companies still expect you to know this language even if you are going to work in some other programming language... lets C..


I read Kanetkar's "Let Us C" and found so many rubbish reasons he have given to support C. Then i found out wikipedia and got a book where i got some interesting explanation, i m sharing here........

Earlier days operating systems were written in assembly language, however, the assembly languages were microprocessor specific and hardware dependent. So for different hardware the whole code of the same OS had to be written differently. In such a time Denis Ritchie of the Bell laboratories came up with a system programming language which was platform independent..which means that whatever be the target hardware system, the programmer need not change the code.. the only thing that had to be done is compile the code differently for different platforms.. It was obviously easier to write compiler for different platforms than writing the whole source code for each application every time we change the hardware.. The language was so much welcomed that without any official support it spread throughout the programmers' world. The popularity increased with such pace that it soon became a general purpose language...

Now the question that comes to mind is as we have developed many higher level languages then why shall we still learn C?

One thing to remember is that system programs for all major OS like Windows, Linux and Unix are still written in C. They have not been replaced by any language. This is because code written in C interacts easily with hardware and is platform independent, the reason why C is called a middle level language..

Another important point, all driver programs for all new hardware are mostly written in C , because it gives you a high level interface with a low level operational authority, the property which makes it different.

So, the answer is however boastful other languages maybe, no higher level language gives a programmer that much authority over a system which C gives... As it was designed for System programming it gives you direct access to your hardware. You are free to use pointers and other C stuff to design or destroy your system according to your own will and capabilities.. Obviously, if you carry a sharper weapon, its careful use is your own responsibility...

  One may argue that C does not have support for networking and the most popular part of today's computing The Internet environment does not use C for security issues. However, the servers on which the internet is based are still using C based operating systems. So the basic networking part is still handled mostly by C. Only the upper application, session and presentation layers are handled by other languages. And for offline computing especially when designing applications closely related to hardware or OS, C is and will be the language of choice...
So, learning C is quite important.. In following blogs we will discuss the other aspect.. Is it as interesting to be called  an art.. Wait for the next article...