CS101 Computer programming and utilization Dr Deepak B Phatak Subrao 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
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 2
Machines in our life Refrigerator, washing machine Bicycle, car, railway engine, airplane Calculator Computer Internet, web 3
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) 4
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 5
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 ) 6
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 7
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. 8
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. 9
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. 10
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 11
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 12
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. 13
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 14
Buddhu Ram Computer - Mr Dumbo 15
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 16
Dumbo has poor memory Dumbo cannot remember anything in its head
Dumbo has poor memory Dumbo uses a cupboard or a chest of drawers as memory A B C D E F Dumbo s memory 18
Dumbo can store values in its memory Each drawer contains some value, for example A number: 437 A name (string): Ranade B 437 Location B: 437 F Ranade Location F: Ranade Dumbo s memory 19
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 20
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 E F 58 21
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 22
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 23
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, A [35] B 12 C? 24
Dumbo Computing We instruct him by saying: C = A + B; A [35] 12 B? C 25
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 26
Dumbo Computing Dumbo uses a set of local registers Working Sheets on a table B A [35] 35 + 12 = 47 12 C? 27
Result of Dumbo Computing A and B continue to hold the existing values Location C will now contain 47 A [35] 12 B C 47 28
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 29
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 30
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 31
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. 32
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. 33
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 */ 34
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. 35
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; } 36
Convert temperature from 0 C to 0 F // 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; } 37