Friday, June 5, 2009

Byhearting Computer Programs

Nothing is impossible! You can walk 1100Kms from Bangalore to Mumbai, you can drive, go by train or fly. All these are possible options but you have to wait for a while and think what’s in for you. Likewise you can by-heart contents from a news paper, from your text books. From the heading of the topic you know what I am trying to arrive at. Don’t ever ever mug up a computer program. You are stepping on a wrong stone. I know many of you have so much retention power that you can mug up anything but that’s not the way a few things are done. One of those few things is a computer program. Your next question is, out of 20 programs asked how should we go about writing them in our exams?

When you first get introduced to computers (VTU’s CCP subject) you should understand clearly that learning computer programming is a total shift from normal way of your studying Maths, Physics or Basic Electronics. Those subjects need you to understand the logic and by-heart a few things and you are done. There is a chance that if a few things are not correct you could still get some marks, not 0 which is what happens when you computer program goes wrong. While you are learning programming you don’t know why you add #include , you don’t know why you do ‘cc test.c’ and then why you do “./” in “./a.out”. There are far too many unknowns when you learn CCP. Question is how do you break into so many things and slowly seep into the world of good computer programming? I suggest you to document all the questions about the non-C related things that you have and concentrate only on ‘C’ related doubts. Understand

-How ‘for’ loops inside ‘for’ loop (nesting) is different from 2 separate ‘for’ loops.

-How ‘continue’ changes the flow of the code totally

-How ‘switch-case’ is glorified ‘if-else’

One needs to STOP too much reading of programming books but sitting by the computer and start going from Program1 in the syllabus and seeing how the complexity has been gradually increased. If you are measuring the amount of reading you have done for computer programming then its time you change the scale to amount of time you’ve spent on the computer understanding simple things like ones listed above. Your time on computer is all. Please, for heaven’s sake, don’t by-heart the computer program. You may succeed today but remember that you are stepping on the wrong stone and you are bound to fall sooner or later.

When you are told to do matrix multiplication or any complex program to write amongst your 20 exam programs follow these things:

- Make a quick flowchart, only if required. Many times its not required.

- Write an algorithm, again if required.

- Start writing your program.

If you don’t need above 2 steps start writing your program on the answer sheet and say, now you are stuck, you don’t know how to proceed. In such a case make a habit of writing a very very essential part of your flowchart or algorithm on the back of your answer sheet and get the doubt cleared. If you are satisfied with it then come back to your C program and see if you can code it.

Once you are done fixing your compilation errors, you will run ‘./a.out’ and mostly you will not get the output that you expected.

I recommend using ‘gdb a.out’ and single stepping into your program line by line and seeing if everything is going fine or not. Gdb is a debugger which stops the clock of the system and gives you a finer control over each line of C code. The difference between running ‘./a.out’ and ‘gdb ./a.out’ is you are vrooming on MG Rd at 100 Km/hr v/s you are walking on the MG Rd. While walking you can see every thing, you can do window shopping, buy things at your own pace. You cannot do these activities while you are in a car going at 100Km/hr. See http://arioch.unomaha.edu/~jclark/gdb_plus.html for basic gdb commands like print, b (break point), run, next, quit etc.

All the best!

A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.
-- George Bernard Shaw

No comments:

Post a Comment