CS101 Computer programming and utilization

Similar documents
CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Computer Programming

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Fundamentals of Programming CS-110. Lecture 2

Computer Programming

Computer Programming

Introduction to Programming EC-105. Lecture 2

Getting started with C++ (Part 2)

REVIEW. The C++ Programming Language. CS 151 Review #2

CS101 PLEDGED SPRING 2001

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Computer Programming

2 nd Week Lecture Notes

Computer Programming

Computer Programming

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Computer Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

VARIABLES & ASSIGNMENTS

Computer Programming

Programming in C++ PART 2

LAB: INTRODUCTION TO FUNCTIONS IN C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Understanding main() function Input/Output Streams

Programming. C++ Basics

A First Program - Greeting.cpp

Computer Programming

1a Computers, Problem Solving!

Week 0: Intro to Computers and Programming. 1.1 Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

The C++ Language. Arizona State University 1

Computer Programming

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Introduction to the C++ Programming Language

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

CHAPTER 3 BASIC INSTRUCTION OF C++

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Object Oriented Design

CS242 COMPUTER PROGRAMMING

Faculty of Science, Engineering and Technology

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 1 Introduction to Computers and Programming

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Computer Programming

Introduction. Arizona State University 1

Introduction to Programming

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Your First C++ Program. September 1, 2010

LECTURE 02 INTRODUCTION TO C++

Add Subtract Multiply Divide

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

CSc 10200! Introduction to Computing. Lecture 1 Edgardo Molina Fall 2013 City College of New York

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Rules of Exponents Part 1[Algebra 1](In Class Version).notebook. August 22, 2017 WARM UP. Simplify using order of operations. SOLUTION.

Homework #3 CS2255 Fall 2012

Computer Programming

Chapter 2: Introduction to C++

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Lecture 03 & 04 Computer Programming

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Honors Computer Science C++ Mr. Clausen Program 6A, 6B, 6C, & 6G

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

CSCE Practice Midterm. Data Types

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

This watermark does not appear in the registered version - Slide 1

CS 101: Computer Programming and Utilization

Fundamentals of Structured Programming

The type of all data used in a C++ program must be specified

pointers + memory double x; string a; int x; main overhead int y; main overhead

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Computer Programming : C++

Tutorial Letter 103/1/2017 Introduction to Programming I

Midterm Practice Exam

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

C++ For Science and Engineering Lecture 2

COMP322 - Introduction to C++

Transcription:

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