Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
CS101
Computer programming and utilization
Dr Deepak B PhatakSubrao Nilekani Chair Professor
Department of CSE, Kanwal Rekhi Building
IIT Bombay
Lecture 1, Introduction to programming
Tuesday, 26 July 2011
And Wednesday, 27 July 2011
IIT BOMBAY
IIT BOMBAY
Overview
• Programmable machines
• Mr Dumbo: Model of Computing for a program
• Dumbo‟s memory and its interaction with us
• Dumbo‟s computing
• Instructions and their execution
• Sample c++ programs
• Announcements
Dr Deepak B Phata 2Lectue 1 Introduction to programming
IIT BOMBAY
Machines in our life
• Refrigerator, washing machine
• Bicycle, car, railway engine, airplane
• Calculator
• Computer
• Internet, web
Dr Deepak B Phata 3Lectue 1 Introduction to programming
IIT BOMBAY
How do machines work?
• All machines carry out defined tasks
• These react to given „instructions‟
• Example of „instructions‟
• „Switch-on‟ a light (the bulb lights)
• Push the bicycle pedal (the bicycle moves ahead)
• Rotate the car steering wheel (Car turns)
Dr Deepak B Phata 4Lectue 1 Introduction to programming
IIT BOMBAY
Example of a calculator
• You wish to add 23 and 156 using a calculator
• Using the keys available on the calculator, we press:
• Keys 2 and 3
• „+‟ sign
• Keys 1, 5, and 6
• „=‟ sign
• The result value 179 is displayed in the window
• The above „instructions‟ are given interactively
• Calculator responds to an instruction as it is given
Dr Deepak B Phata 5Lectue 1 Introduction to programming
IIT BOMBAY
Digital Computer
• A computer requires all instructions to be
Pre-written in advance
• Such a pre-written set of instructions is called a
program
• Computer is capable of doing the following:
• First „read‟ the entire program and „Understand‟
all instructions („Compilation‟)
• Carry out instructions, one by one, in the
stipulated order („Execution‟)
Dr Deepak B Phata 6Lectue 1 Introduction to programming
IIT BOMBAY
Basic capabilities of a computer
• Ability to handle numerical values
• 257, -78, 4.675, etc.
• Large and small values (1.4E18, 0.356E-9)
• Ability to carry out numerical operations
• Add („+‟), Subtract („-‟),
• Multiply („*‟), divide („/‟), ...
• 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
Dr Deepak B Phata 7Lectue 1 Introduction to programming
IIT BOMBAY
A sample program in English
1. Get a number from me, and store it in a location.
Call this location A.
2. Get another number from me, and store it in
another location. Call this location B.
3. Multiply the numbers in locations A and B, and
store the resulting value in yet another location.
Call this location P.
4. Give me the value stored in location P.
Dr Deepak B Phata 8Lectue 1 Introduction to programming
IIT BOMBAY
Compilation process
1. Get a number from me, and store it in a location. Call this location A.
2. Get another number from me, and store it in another location. Call
this location B.
3. Multiply the numbers in locations A and B, and store the resulting
value in yet another location. Call this location P.
4. Give me the value stored in location P.
Dr Deepak B Phata 9Lectue 1 Introduction to programming
IIT BOMBAY
Execution process
1. Get a number from me, and store it in a location. Call this location A.
2. Get another number from me, and store it in another location. Call
this location B.
3. Multiply the numbers in locations A and B, and store the resulting
value in yet another location. Call this location P.
4. Give me the value stored in location P.
Dr Deepak B Phata 10Lectue 1 Introduction to programming
IIT BOMBAY
Live experience with a program
• Next slide shows a test program. Instructions in that
program can be easily carried out by a human
• Compile and execute that program. Try to work as
correctly, and as quickly, as a computer can!!
Good luck
Dr Deepak B Phata 11Lectue 1 Introduction to programming
IIT BOMBAY
Compile and Execute this program
1. Raise one of your hands
2. Put down your raised hand
3. Loudly say „Ha Ha Ha‟
4. Ask your neighbor‟s name. Write down that name in
your notebook
5. Write the value of Pi ( π ) correct to 3 decimal places
6. Speak loudly the name of your mother tongue
7. Calculate the product of 25 and 4 and speak the value
loudly
8. Clap three times
9. While executing this program, ignore all earlier
instructions and raise both hands
Dr Deepak B Phata 12Lectue 1 Introduction to programming
IIT BOMBAY
Back to Computers
• We will rewrite our earlier program to add numbers
1. Get a number from me, and store it in a
location. Call this location A.
2. Get another number from me, and store it in
another location. Call this location B.
3. Add the numbers in locations A and B, and store
the resulting value in yet another location. Call
this location C.
4. Give me the value stored in location C.
Dr Deepak B Phata 13Lectue 1 Introduction to programming
IIT BOMBAY
Back to the computers
• We will use a simple robot like creature called
Buddhu Ram or Mr Dumbo, to understand how the
computer works
• Forming a „conceptual model‟ in our minds
• We will provide Mr Dumbo with facilities similar to
that of a computer
• Memory locations
• Computational abilities
• Input-output capability
Dr Deepak B Phata 14Lectue 1 Introduction to programming
IIT BOMBAY
Buddhu Ram Computer - Mr Dumbo
Dr Deepak B Phata 15Lectue 1 Introduction to programming
IIT BOMBAY
Buddhu Ram Computer - Mr Dumbo
• Dumbo cannot do anything on his own
• but can execute given instructions correctly
• He is an obedient servant
• Can do arithmetic, logical comparisons, string manipulation, etc.
• Essentially, can manipulate values, and produce resultant values
• He can Compute, So He is a computer
Dr Deepak B Phata 16Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo has poor memory
• Dumbo cannot remember anything in its head
IIT BOMBAY
Dumbo has poor memory
• Dumbo uses a cupboard or a chest of drawers as memory
Dumbo’s memory
A
B
C
D
F
E
Dr Deepak B Phata 18Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo can store values in its memory
• Each drawer contains some value, for example
• A number: 437
• A name (string): “Ranade”
Location B: 437
Location F: “Ranade”
B
F
437
Ranade
Dumbo’s memory
Dr Deepak B Phata 19Lectue 1 Introduction to programming
IIT BOMBAY
Some Peculiarities of memory
• Each location can contain only one value
• multiple values cannot exist in any location
• How does a value get into a location?
• We supply an input value which Dumbo stores there
• Dumbo stores a value assigned through calculations
• Some values may already exist in a location (How?)
• Such values are „unknown‟ or „undefined‟ as these are
neither supplied by us, nor computed by Dumbo while
executing any of the instructions in our program
Dr Deepak B Phata 20Lectue 1 Introduction to programming
IIT BOMBAY
Exchanging values with Dumbo
• Dumbo uses a cart („Data Bus‟) to move values in and out
• He can put a value in a location as instructed or can copy a value
from a location and give it to us
A
B
C
D
F
E
58
Dr Deepak B Phata 21Lectue 1 Introduction to programming
IIT BOMBAY
Exchanging values with Dumbo …
• Dumbo does anything only if instructed
• The instruction to Dumbo to collect an input value from us is:
Input B;
• Dumbo will collect a value given by us, take it to its memory cupboard, and will store it in location B
• The value already present in that location is overwritten
Dr Deepak B Phata 22Lectue 1 Introduction to programming
IIT BOMBAY
Exchanging values with Dumbo …
• Dumbo can output values from locations to us
• The instruction to do so is
Output C;
• Dumbo will copy value from memory location and present it to us
• Value in named location remains unchanged
Dr Deepak B Phata 23Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo Computing
• If two numbers are stored in drawers A and B, say 35 and 12,
Dumbo can add these and store the result in a drawer, say, C,
B
C
12
?
A [35]
Dr Deepak B Phata 24Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo Computing
• We instruct him by saying:
C = A + B;
B
C
12
?
A [35]
Dr Deepak B Phata 25Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo Computing …
• How will Dumbo do this?
• Dumbo uses rough sheets or „Registers‟
• Access the location A and copy the value (35) into a register
• Access the location B and copy the value (12) into another register
• Add the two values to get (47)
• Store the result value back in location C
Location C will now contain value 47
Dr Deepak B Phata 26Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo Computing
• Dumbo uses a set of local registers
• Working Sheets on a table
B
C
12
?
A 35 + 12 = 47[35]
Dr Deepak B Phata 27Lectue 1 Introduction to programming
IIT BOMBAY
Result of Dumbo Computing
• A and B continue to hold the existing values
• Location C will now contain 47
B
C
12
47
A [35]
Dr Deepak B Phata 28Lectue 1 Introduction to programming
IIT BOMBAY
How do we give instructions
• Most machines take one instruction at a time, and
execute it
• Dumbo is special
• We have to write all our instructions in advance, and
give the entire lot to him
• This collection is called a program
• Dumbo has a „stored program‟ capability
Dr Deepak B Phata 29Lectue 1 Introduction to programming
IIT BOMBAY
Where does Dumbo keep the program?
• In the same memory!
• Dumbo uses some space for storing data values, and
another separate space in the same cupboard to store
instructions of our program
• Instruction space
• Data Space
Dr Deepak B Phata 30Lectue 1 Introduction to programming
IIT BOMBAY
Animated Dumbo
Just to consolidate the Dumbo model of computing,
we will see very short animations describing:
• An Introduction to Dumbo
• Showing Dumbo, its memory drawers
and a typical input operation
• Dumbo performing computations
• Showing addition of two numbers
Dr Deepak B Phata 31Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo‟s Programming Language
• We have seen some instructions already, which can
perform input, computations, and output
• Input A.
• C = A + B.
• Output C.
• If there are multiple values being given as input
• We may not remember which value is to be given
when!
• An additional facility to output a “messgae”
• Output “Give value of X”.
Dr Deepak B Phata 32Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo‟s Programming Language ...
• We may wish to explicitly inform Dumbo, the
different symbolic names for different locations,
which we propose to use in our programs
Integer location A.
• We can also list multiple locations in single
instruction
Integer locations A, B, C.
Dr Deepak B Phata 33Lectue 1 Introduction to programming
IIT BOMBAY
Dumbo‟s Programming Language ...
• Since we will also be reading such programs, we
may like to write some simple explanatory
comments in plain English
• We will write single line comments by starting
the line with two slashes (“//”)
• We will write multi-line comments by enclosing
all lines in symbols “/*” and “*/”
// Program to calculate sum of two numbers
/* Program written by Deeepak B Phatak as an
illustration for CS101 course. Written in July 2011
IIT Bombay */
Dr Deepak B Phata 34Lectue 1 Introduction to programming
IIT BOMBAY
A sample program for Dumbo
// Program AddNumbers.
/* program to calculate sum of two numbers */
Integer locations A, B, C.
output “Give value for A”.
Input A.
Output “Give value for B.
Input B.
C = A + B.
Output “The value in location C is”.
Output C.
Dr Deepak B Phata 35Lectue 1 Introduction to programming
IIT BOMBAY
A C++ program
// file add_two_numbers.cpp
#include <iostream>
using namespace std;
// this program reads two integer numbers and calculates the sum
int main() {
int A, B, C;
cout << “Give two numbers”;
cin >> A >> B;
C = A + B;
cout << “Sum is” << C;
return 0;
}
Dr Deepak B Phata 36Lectue 1 Introduction to programming
IIT BOMBAY
Convert temperature from 0C to 0F
// convertCtoF.cpp
#include <iostream>
using namespace std;
// To convert temperature from Centigrade into Fahrenheit
int main() {
float C; float F;
cout << “Give Temperature in degree Centigrades";
cin >> C;
F=C*9.0/5.0+32.0;
cout << F;
return 0;
}
Dr Deepak B Phata 37Lectue 1 Introduction to programming