Programming
The basic concepts of programming are introduced, starting with the notion of an algorithm.
The emphasis of this course is on developing the ability to write correct programs/solutions to solve practical computational problems.
• Computational procedures
• Details of computations
• Computer architecture
• Integer representation
• Elementary graphics
• Coordinate-based graphics.
Introduction
We already had seen in the preamble that computers are machines. A machine which requires instructions to be given to it. The instructions are given in the form of a program.
A computer is expected typically to first read the entire program, understand it,
and then execute the instructions one by one in the specified order.
Let's look at some details of how instructions are given and how procedures are written.
Of course, we are going to look only at the instructions and procedures meant for human beings. In particular, we’ll look at the procedure for preparing tea, which is a well known recipe, we'll also look at the procedure,which a cashier in the bank uses, when he deals with customers, maybe giving money to them, when they come to withdraw money from their account.
We often instruct people to do something.
For example, we say ‘please get me a glass of water’, or we say, ‘please keep this book on the shelf’.
Such instructions are verbally given. They are not complex tasks, and whatever we say in a simplified manner is very easily understood by someone, whom we are asking the favor.
Please understand that whenever we give such an instruction, the context of that instruction is well understood by both the person, who carries out that instruction and by us.
For example, when we say ‘keep the book on the shelf’, the book is not kept in a shelf which contains clothes or which contains shoes. The person will keep the book on the bookshelf.
Now that is understood and we understand that, that is understood.
Such is the way in which simple instructions are given.
They are given verbally and they are followed automatically in the correct fashion.
However, when a task is complex and a task needs many steps to be carried out,
then verbal instructions are often inadequate. More importantly, when we have a complex task that different people have to do at different times at different places in exactly the same way, then obviously we cannot depend upon verbal instructions.
This is the situation when we give written instructions.
There are many examples of written instructions. Cook books or recipe is one example.
Instructions at the work place is another example.
Let us look at a recipe for preparing tea.
Consider these steps.
Take a cup of water in a vessel.
Add two teaspoons sugar, one teaspoon tea leaves, and milk.
Boil the mixture for three minutes, strain in a tea kettle, serve in a cup.
This is how, what we call the Tea of India is made.
There could be some variations but generally is the process that is followed.
Please understand that for a person who is not familiar with the Indian tea,
such a written procedure is very useful,because then exactly the same kind of tea could be prepared by two different people, may be one in China, one in Singapore, one in South America. They all can make tea in this fashion, which will be very similar in nature.
Consider now, the same recipe for preparing Indian tea where the steps are exchanged.
Look at these steps.
Take a cup of water in a vessel, boil for 3 minutes,
add two teaspoons sugar, one teaspoon tea leaves, milk,
strain into a tea kettle and serve in a cup.
Note that the steps are the same as the previous, except that two steps have been interchanged.
The question is will I get the right kind of Indian tea.
We can see that a procedure, if not written properly, if the steps are not written in the order in which they need to be executed then the desired task will not be correctly carried out.
Let us now look at a more formal procedure which typically bankers follow.
Let’s consider an old bank, which still maintains account information in paper form
and what is known as ledgers. A ledger is nothing but a register in which the account information about a customer is maintained.
Suppose a customer comes to withdraw some money from the account.
A customer will present a cheque or a withdrawal slip in a banking parlance, it is called an instrument. So any piece of paper or any message, which contains information about financial transactions, is called an instrument. So, suppose a customer comes and presents a cheque or a withdrawal slip, the customer should be paid cash if
A written procedure
• Must be first fully read and understood
• Then all steps must be executed in the specified order
Computational Procedures
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.
A Computational Procedure Problem
• A tank has been erected in our yard to store water
• We want to paint it, to prevent rusting
• Given the painting charges in $. per sq. meter, what will be the cost of painting the entire tank, along with its top cover?
Calculating Surface Area …
• Surface area of a tank to be painted
• (radius r and height h) = area of the outer surface + area of the top circular cover
•Area of surface = 2πrh
•Area of circular cover = πr 2
Procedure for Computing Painting Cost
• Get from me, value of painting price P in $ per sq. meter
• Get from me, values of radius R, and height H
• Calculate area A A = 2πRH + πR 2
• Calculate cost C C = PA
Procedure
• Get from me, value of painting price P in $ per sq. meter
• Get from me, values of radius R, and height H
• Calculate area A A = 2πRH + πR 2
• Calculate cost C C = PA
• Give me the value of C
Programs
• We write procedures to be executed by a computer
• Such procedures are called ‘Programs’
• A computer is capable of doing the following:
- First ‘read’ the entire program and ‘Understand’ all instructions (‘Translation’ or ‘Compilation’)
- Carry out instructions of the program, one by one, in the stipulated order (‘Execution’)
Challenge:
Compile and Execute this program
- Raise one of your hands
- Put down your raised hand
- Close your eyes and count loudly up to 10
- Loudly say ‘Ha Ha Ha’
- Write the value of Pi ( π ) correct to 3 decimal places
- Speak loudly the name of your mother tongue
- Clap three times
- While executing this program, ignore all earlier instructions and just raise both hands
Capabilities of a Computer
- Ability to collect values from as input, and to give back to us the calculated results, as output
- Ability to store these values temporarily
- Notion of a ‘memory’ location
- Ability to refer to locations by symbolic names
Summary
• Computer Programs are usually computational procedures
• These will generally Involve
• Collecting some input values from the user
• Performing calculations and getting some ‘result’ values
• Giving back the result values as output.
Practice Problems:
Problem #1 Consider the Dumbo program given below.
Use locations NBOYS, NGIRLS, NSTUDENTS;
NBOYS = 35;
NGIRLS = 124;
NSTUDENTS = 80;
Input NBOYS;
Input NGIRLS;
Output NSTUDENTS;
NSTUDENTS = NBOYS + NGIRLS;
Suppose the user types 12 and 28 as input values for the locations NBOYS and NGIRLS respectively. The output value displayed by Dumbo on our monitor screen will be
- 85
- 40
- Insufficient data to predict output value
- 80
- None of the above
Problem #2 Consider the Dumbo program given below.
i. Use locations A, B;
ii. Output "Give A and B: ";
iii. Input A; Input B;
iv. A = A + B;
v. B = A + B;
vi. Output "B is: ";
vii. Output B;
Suppose the user provides 10 and 20 as the values of A and B respectively when Dumbo executes the above instructions. What value will be output by Dumbo after printing "B is: "?
- 20
- 30
- 40
- 50
Detail 3
The following is placeholder text known as “lorem ipsum,” which is scrambled Latin used by designers to mimic real copy. Donec eget risus diam. Vivamus sit amet semper lacus, in mollis libero. Vivamus sit amet semper lacus, in mollis libero. Donec ac fringilla turpis.
Nulla eu pretium massa. Suspendisse nec congue purus. Phasellus sodales massa malesuada tellus fringilla, nec bibendum tellus blandit. Integer tempus, elit in laoreet posuere, lectus neque blandit dui, et placerat urna diam mattis orci. Donec eget risus diam.