Tuesday, January 18, 2005

Long time no C

After a very long time I suddenly had an urge to write a program in C. I chose to write the program which prints all the permutations (combinations) of a given string without using recursion. I remember having given this program to one of my college mates (called Ditch) as a challenge to complete by the time myself and a friend of mine (woodworm) went and had dinner. He did take up the challenge and completed it with about 10 minutes extra grace time. He used recursion. After he went back, me and my good friend woodworm tried (tried very hard indeed) to understand what he had typed.

It looked something like this:

void printAllCombinations(int **ptr, ****goobledeegook)
{
if(&abcd!=**bcd-->next)
{
e[*a-->next] *abcd; // "you cannot understand me"
**a&&b+=-abcd;
printAllCombinations(**ptr->next--->previous);
}
}

As you can see, it is a very readable code, we did of course not waste much time trying to figure out what it did. Later, I was unable to write this code (though it is a seemingly simple question) for a very long time using recursion.

But somehow, I did not let go of the challenge, and yesterday I decided I am going to finish writing it come what may.I am very glad to say I did complete writing it. I also happened to uncover a very good algorithm while doing so. Glad to have completed the same. Though I am writing a code in C after a very long time, I did not blink around for long before I started coding with ease. Guess, once a C programmer, always a C programmer.

C is probably the best thing that happened to a computer. When I learnt C (which is when I was in my first year of college) , there were a lot of students who were experts in that language by then. I happened to come from a board where they did not teach C when you were in 12th standard (lucky it has changed now), while people from a CBSE background had already implemented a Stack while they were in their 12th standard.

When I was asked to write my first program, I used to think, who created this sort of a junk way to code things? Why should I put that stupid semi colon after every line, can't it understand that a "New-line" is a "NEW" "LINE" without my putting a semi colon.

Coming to think of it, there are so many mistakes I used to make while I started my C programming, most of which I donot make these days unconsciously. To list a few:

1) Forgetting to close the braces
2) Forgetting to initialize a variable before using it
3) Forgetting to put an "&" in a scanf statement while taking the input of a string.
4) Not forgetting to put a ";" after a #define statement
5) Forgetting to include a few important files (the stdio.h of course)
6) Forgetting to allocate memory to a pointer before storing a value in it

Well, all that is a story of the past. I still remember how frightened I got when it was my first online exam. I was askd to write a C program which converts one 2 D matrix into another form. I almost fainted, and could not understand the problem for the first few minutes. Then I wrote a lot of while loops and for loops (earlier, I used to write a program when everything will start in the main() function and end there itself...no function declaration business, no modularity, and absolutley zero clarity). Finally I ensured that my code compiled, but did not give an output.

The person who came to correct my solution (I forgot her name), typed an a.out and waited for some time. 1 minute passed. 2 minutes passed. Still no output. 3 minutes and her patience seemed to run out. She told me "I will check the solution of other and come to you, till then let it run"

She did of course come after about half an hour, and of course my program did run till then (it did not want to disappoint me by printint the solution did it?)

Then I had to explain the logic in my code, which of course I did with all enthusiasm (As long as I donot have to put semi colons, and loops, I can even explain the logic behind how Bihar is able to stand Lalloo).

Finally I managed a 12/20. "Thank you instructor , whoever you are, for not dampening my spirit of programming. Thank you very much"





No comments: