Nuevo

Computational Procedures

Computational Procedures



Welcome back to this session on computational procedures.
In the last session, we have seen written procedures for complex activities,
and we had also seen that the procedures must first be understood and then executed.
We will now look at a computational procedure because eventually,
we want to learn to write programs for the computers.
In the process we will also see, how a 'program' would be executed by a computer.
Indeed this would exactly be in the the same manner that a computational procedure is carried out by human beings.
So let us look at a problem. Suppose we have a tank which we have erected in our yard to store water.
Now, the tank may rust so to prevent it from rusting, we wish to paint it.
Now, suppose we are given the painting charges in rupees per square meter.
We need to find out, what will be the cost of painting the entire tank, of course we must not forget that top cover
which will be on top of the tank. So, we need to calculate the total painting charges.
Now, this is clearly a computational problem, so we will need to make some kind
of simple mathematical model for solving this problem. First we try to see, how we can calculate the surface area.
This is the picture of a tank. This tank has some radius, its a cylindrical tank. It has some radius  r and it has some height h.
Those of you, who have done simple geometry in your earlier classes,
would easily understand how to calculate the surface area. Let us look at the calculations.
First the surface area of a tank to be painted which has the radius r and height h is equal to
the area of the outer surface plus the area of top circular cover.
Our geometry knowledge tells us that area of the outer surface which is 2 pi rh,
where r is the radius and h is the height.
In exactly the same way, we can find out that area of the circular cover will be pi r square.
Once we know these two areas, their summation will be the total area.
Having modeled this problem in terms of the mathematical formulae, which we need to calculate the painting cost,
we are now ready to write down a procedure for computing the cost.
The procedure is written as if it will be executed by some person.
So I am telling that person, get from me value of painting price P in rupees per square meter.
Then get from me, values of radius R and height H. Next step says calculate area A, which is,
A equals 2 pi RH + pi R square, and the last step says, calculate cost C, C is equal to P into A.
If you have any difficulty in understanding this,
observe that we have deviated slightly in the symbols that we have used.
We have used small r for radius in the previous slide and here we are using capital R.
But you understand it easily because for you it doesn't make a difference whether R is small or capital,
it still means radius to you. However later on we will note that when we write computer programs,
we have to be very careful because each name has to be unique
and it must be used exactly in the same way that is used elsewhere to mean the same.
Consider the steps of this procedure.
Do you agree that, this procedure will give you the final computed painting cost?
Do you agree that this procedure will get you the final painting cost, computed  by this process?
Sadly the answer is no. Let us examine why.
Lets go through these steps again get from me value of painting price, alright.
So, the person who is executing this procedure will collect the value P from me.
Next the procedure says, get from me, values of radius R and height H,
again the person who is executing this procedure, will collect from me one value from radius R and a value for height H.
The next instruction says calculate area A by the formula A equal to 2 pi RH plus pi R square.
The person will religiously do this multiplication of pi R square, do this multiplication of two into pi into R into H.
Add both these values and find out one resulting value,
which it will assign it to the symbol A on the left hand side of the equal to symbol.
So we have got the value of area A. Next instruction says calculate cost C, the person who is executing
this procedure will correctly calculate the cost C, which is given by P into A. So is the problem.
The problem is that, when entire procedure is executed, the person who is executing the procedure will have the cost C,
but we will not have the cost - Why?
Please remember, we ask that person to get from us the values of P, values of R and values of H.
But we forgot to tell him, to give us the value of C which he has calculated. Please note that a complete
procedure must contain each and every instruction which will finally get us the value of C.
So, look at the this procedure now. We have to add this step here after collecting the input value, after calculating the area and the cost.
There must be an instruction of procedure, we say give me the value of C.
That is when the person executing the procedure will give us the value of C and will fullfil our requirement of finding out what is the painting cost.
We have to write such procedures to be executed by a computer when instead of a person,
a computer executes a procedure like this, then the procedure is called a program.
We note that a computer is capable of doing exactly the same thing that the person is capable of doing while executing a computational procedure,
namely the computer can read the entire program and understand all instructions. This step by the way, is called translation or compilation.
We shall explain the meaning of these technical terms later but for the time being
we note that the computer first reads the entire program and understands all instructions.
Once it has done that then it carries out instructions of the program, one by one, in this stipulated order.
This is called the execution of the procedure.
Very much similar to the execution of the computational procedure that we just saw.
Well let us have some live experience with hypothetical program. I have written a program, it's a test program.
Of course, the instruction in that test program can be easily carried out by humans.
I would like you to behave as if you were a computer, compile and execute that program, please understand
that you have to work as correctly as possible and as quickly as a computer can.
Remember that a computer can execute millions of instructions per seconds.
You may not be able to carry out millions of steps in a second but let us see how fast you can be.
So, good luck, compile and execute this program. I will give you thirty seconds.
Ok, so let us analyse this program. The first step says  raise one of your hands. Next step says put down your raised hands.
The third step says, close your eyes and count loudly up to 10. The third one says, loudly say, ha ha ha.
The next step says, write the value of pi correct to three decimal places.
The next one says, speak loudly, the name of your mother tongue.
And the next step says, clap three times. I am sure that many of you in your eagerness will execute the program as fast as a computer would,
you would have done most of these things. In a classroom whenever I have tried this,
almost the entire class quickly starts raising their hands,
closing their eyes and counting and loudly saying ha ha ha, clapping, etc. etc.
Only those few who understand that the first step is to compile a program and then only execute it.
Which means you have to first read the entire program. Those who do that would have realized
that the last step says, while executing this program, ignore all earlier instructions
and just raise both hands. In fact, the fastest person who correctly executes this program is one who did nothing of these steps but just raised both hands. This might be fun
but this is what we need to remember when we deal with a computer.
Let us now look at the capabilities of a computer. A computer has ability to handle numerical values.
We of course expect that. For example, it can handle values such as 257, -78, or 4.675 etc.
It can also handle very large and very small values.
Most of you should be able to decipher what this symbolism means: 1.4e18 actually means a value 1.4*10 to the power 18.
Similarly this value means 0.356*10 to the power -9. Observe that this value is very small, this value is very large.
But a computer is capable of dealing with such very large
and very small values, subject of course to a limited procedure as we shall see later.
A computer of course has the ability to carry out numerical operations.
For example, it can add, it can subtract, it can multiply, it can divide. For addition and subtraction and division
we use the symbols +, - and /. For multiplication, however most computers use a symbol *.
Can you guess why? Ordinarily, when we want to depict multiplication on a piece of paper,
we say something x something. The symbol x which is used for the letter x is
also used to denote multiplication. But because computer programs do not want to be confused
with letters and symbols, they use this symbol * for indicating multiplication.
More importantly, the computer has the ability to collect values from us as input and to give back to us the calculated result as output.
It has ability to store these values temporarily.
So there will be a notion of a memory location which we shall see in the next lecture.
It also has the ability to refer to these locations by symbolic names.
In summary, the computer programs are usually computational procedures.
These will generally involve collecting some input values from the user, performing calculations and getting some result values and giving back the result values as output to us.
Thank you.

No hay comentarios