BudhVaar

Budh = Mercury. Budh is also the root word for brain. Also Buddha. This day is mid-week. Most work done. This section will cover some of the interesting findings in different fieds of academics.

My Photo
Name:
Location: Delhi, Singapore

Sometimes difficult but mostly understanding; sometimes fun but often moody; sometimes alive and other times plain dead.

Tuesday, June 27, 2006

Programming like an Indian (Continued)

Algorithms

From the previous article we peeped into the very English meaning of programming. I had given examples of how everything around you is programmed to work the way it does. Every program or process has some inputs, some logic of dealing with the inputs and some outputs.

Let’s take the example of making an omelet. Well, I need an egg, some salt, 1 small onion and a non-stick frying pan. Now, you’ve seen mathematical equations where you write:
y = x + 2 = f(x)
where x is the input and y is the output. This means that if you put x inside the function x it will return y i.e. x+2.
Can we write a similar function for making our omelet? Yes, we’ll try:
Omelet = egg liquid + chopped onion + pinch of salt + heat
= f(egg+onion+salt+heat)

Using this we can write an algorithm as follows:
procedure make_omelet (egg 1, onion 1)
begin
omelet = fry(egg, onion)
output omelet
end
end procedure

While we can see that fry() is another procedure which is used in the algorithm above, it still puts the right idea and line of thinking behind any process. I believe therefore, that if you understand any process and the steps that are carried out to achieve that, you’ll know how to come up with the algorithm for it.

If I wrote the details of the sub procedure within this procedure, it will be something like the following:
procedure make_omelet (egg 1, onion 1, salt 1)
begin
new frying pan, spoon, knife, chopping board
chopped_onion = chop(chopping board, knife, onion )
egg_liquid = hit_open(spoon, egg, frying pan)
frying pan = heat(frying pan)

do {
omelet = fry(frying pan, egg_liquid, chopped_onion, salt 1)
}
while omelet is done

output omelet
end
end procedure

The second approach makes the entire process literally into steps, one after another with the basic action items still used as functions like chop, fry etc.
The do { } while is a conditional approach to carry on an activity until some condition is reached.
This second approach is called procedural programming. This is the basic style of programming which is used in Basic, Pascal etc. Here everything is seen like a string of steps one after another. What is the other form of programming then?

Imagine the above process to be very complicated. Maybe the required shape of the omelete the size of onions and amount of salt has to be very presise. In procedural programming, it feels like the same ‘person’ doing the job one step after another. There is no help provided to him. He may be good at chopping onions but may not be good in frying.

Well, object oriented programming focuses on the various things required in a process as objects and accomplishes a task by creating a lot of specialized units that take in certain objects and output certain objects. In the case I presented just now in the previous paragraph, it would be like the ‘person’ having help from an expert in chopping, and expert in frying etc to put their skills together to achieve one big task.

To summarize, I present the following way of thinking:

Procedural


Get the onion, egg, salt, frying pan, knife,
Chopping board, spoon

Chop the onions with chopping board, knife

Break egg with spoon

Put the two in the frying pan with the salt

Heat till its cooked

end

OOP

get onions, egg

call the frying_pan function With the above input and get the output

end

The actual process time might be the same for both but the one on the right looks more organized. If it was a big kitchen and the entire breakfast needs to be prepared. Someone following a procedural approach will lead to 5 cooks all needing the knife and frying pan at some point in their process as they follow the steps. On the contrary, the cooks using OOP approach will assign one cook as the person who chops, another as the person who fries etc and hence get the work done in a more organized and specialized way.

I will be expanding the concepts of both Algorithms and the two programming approaches in the next article. Do leave your comments and questions if any.

- Naveen Kumar
http://uglymoth.blogspot.com

Tuesday, June 20, 2006

Programming like an Indian


I always thought I had some really good concepts in programming. I tutored for many years professionally with my students achieving excellent grades all the times but I never got to write what goes behind in my idiot-box. So, I am taking this time to share my thoughts as I sit down to write a program. This is for complete novices in programming and for those who are interested in taking a peek into the basic thought process of another programmer.

All corrections to this article are most welcome as comments.


What I don't like
I have seen so many students and friends memorizing programs and logics to solve problems. I must make it clear that if you are serious about being a good programmer, you have to get through this article understanding the importance of basic concepts.


Introduction
What is 10+3? It's 13. Just to come up with that, unknowingly you recalled the meaning of '+' and what it does to numbers, something you probably learnt decades ago.
What is 10245 X 36? Now, most likely you are thinking of a calculator. So, you find one, you press the number followed by 'X' and another number and then get the result.
What just happened? The calculator knows just like you that 'X' means to multiply. The only probable difference is that behind the screen it will take the calculation as an addition of 10245 to 10245 and 10245 and 10245.. 36 times. Because it is programmed to behave in a specific manner when a specific sets of buttons is pressed.

Let's see an example thats even simpler: that of a vending machine.
I want a coke, I put in a dollar coin, press the coke button and get the coke out. Whne you press that button, the machine recognises it as an action-command for itself and then returns you a coke. Quite similar to the caculator that persieves the '=' pressed as an action button for it to give result. Both the results require an input: whether it's two numbers with a 'X' or a dollar coin with a coke selection.

We can thus far establish that program in someone's head, or machine's circuit, or calculators chip is nothing but a perceived input followed by some processing and eventually a result. It tremendously helps the budding programmer to see how things around him are programmed. How so many things can be modelled based on the INPUT-PROCESS-OUTPUT model of a program.


Crazy Examples
I want to emphasize on the importance of seeing real world's physical dynamics as programs and therefore give more examples of programs:

1) There is a program in a plants head which helps it to know that the soil has water and the sun is shining and the air is present, and given all these inputs, it is time for the leaves to start making food. Food which makes the plant grow.

2) There is a program in the head of a snake that tells it to look out for warm blooded animals. And when it does sense one, the program makes it pounce on it as the required action or process and the output is that the snake is full and it grows bigger.

What does that sound like? Every process, every habit, every event and function can be seen like a program. Most important thing is to understand that process to be able to identify the steps that define it. There we have the string to connect to the introduction of algorithms.

-----------------------------------------------------
This same section will be expanded. So, do keep a lookout.
TBC.

-Naveen Kumar
http://uglymoth.blogspot.com/