Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Assembly Language and Computer Architecture Using C++ and Java
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Assembly Language and Computer Architecture Using C++ and Java
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Computer Science 101 Introduction to Programming.
Objectives You should be able to describe: Data Types
Simple Data Type Representation and conversion of numbers
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Computer Arithmetic Nizamettin AYDIN
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Input & Output: Console
IT253: Computer Organization
CS 101: Numerical Computing Abhiram Ranade. Representing Integers “int x;” : reserves one cell in memory for x. One cell: “One word” has 32 capacitors.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
CSC 221 Computer Organization and Assembly Language
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Revision Lecture
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Digital Logic & Design Lecture 02.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 2, Fundamental programming concepts (Simple programs) Tuesday 29 June 2010

OVERVIEW  Revisit ‘Dumbo’ model for a program  Structure of a C program Explanation of some nuances  Sample program Notion of variables and data types Expression evaluation and assignment  Problems needing non-sequential execution of instructions  Conditional execution of instructions If statement and its variants

The Dumbo Model of computing  We saw the abilities of a computer a model represented by Dumbo.  A ‘program’ contains instructions executed sequentially. An instruction may Accept values as input, store these in designated memory locations A value may be computed by evaluating an expression, which is assigned to a location One or more values can be copied from memory locations and given to us as output

Computing …  Memory Comprises of ‘locations’, each of which can contain a single value Each location has a tag or a name. Such name is called a ‘variable’ as the value in that location can change or vary based on operations A location is never empty, it always contains some value, may be from some previous computing activity

More about a program (student’s thoughts)  When we discussed our sample programs (prog1.c and prog2.c), we noticed presence of several lines which we did not understand clearly. We will just identify these and take a note of the nuances we need to face as of now  When we have done some more programming and have become more familiar with the computers, we will learn the full meaning of these lines  We look at the problem of evaluating formula v = n1/n2 + x, for x=3, n1 = 2, and n2 = 5 (I expect v to be 3.4)

Program to evaluate a formula #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; }

Meaning of different lines #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } Does not make any sense, perhaps asking the computer to include some pre-written instructions

Meaning of different lines … #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } These appear to be comments meant for human readers

Meaning of different lines … #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } Seems to be something important but cannot understand the meaning. Perhaps the line indicates the beginning of our main program

Meaning of different lines … #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } Appears to define variables for the formula. I don’t understand the words int and float; int perhaps means integer, in which case, float should mean a real number

Meaning of different lines … #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } These are clearly the instructions written to solve the problem. However, the way the formula is written, the computer may first add n1 to x, and the result will be 1.0 which will be wrong

Meaning of different lines … #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } Looks like an output instruction similar to cout instruction. I am guessing this because of the word “print”, but why is it is written as ‘printf? Also, what do the funny symbols inside parenthesis mean?

Meaning of different lines … #include // prog3.c // arithmetic operations int main(){ int n1,n2; float x,v; n1 = 2; n2 = 5; x = 3; v = x + n1/n2; printf("value is %f\n", v); return 0; } This is very confusing! Perhaps the line indicates returning Value 0 to some one. But to whom? And why a zero?

In a nutshell  When I execute the programs in the lab from files prog1.c, and prog2.c, these work correctly, collecting input values from me and returning the correct computed value as output So all those funny lines must have some purpose which I do not understand now  However, I must understand the way my variables are defined, and the way computations are carried out, so that I can write instructions to correctly implement the desired calculations

Our Computing Environment  The computer has evolved significantly. It can be instructed in a human like language,  A tool called ‘compiler’ can translate these instructions in its machine language  It can exchange with us, values in a form familiar to us (e.g. decimal numbers, fractions) and convert these to and from the internal binary representation  A special program called ‘operating system’ controls its operations and its interaction with us

Our Computing Environment...  We typically interact with a computer through a keyboard, using which we type our instructions, and data values and through a monitor, which displays to us, any value output by our program  We interact with the operating system (OS), which is a set of prewritten programs to control the computers.  This interaction is either through text interface where the OS shows us a ‘command prompt’, or using a graphical window interface, We use Ubuntu, a variant of Linux OS

A session with the computing environment ~]$ pwd /home/dbp ~]$ cd cpp ~]$ cc prog3.c cpp]$ ls prog1.c prog2.c prog3.c a.out cpp]$./a.out value is

Handling Numerical values  We define variable names used in our programs. Such definition allocates a memory location to the chosen name, and also defines the ‘type’ of value which can be stored in that location  int N; : reserves one location in memory for N to store integer values. Max positive number: Min negative number:  How to store larger numbers?  We use Long integers: long M; Max magnitude ,

Handling Integer Numerical values …  The computer actually uses ‘Binary’, or ‘Hexadecimal’ representation (Base 2 or 16) Thus value limits are expressed in powers of 2 or of 16  For the time being, we will continue to believe that computer uses Decimal values  For integer values the base is immaterial  Our Computer does arithmetic with constraints because of the limited capacity of a memory location  What happens if two values are individually within the permissible range, but an arithmetic operation, such as multiplication, gives a larger result value?

Floating Point Representation  Real numbers (fractions) are stored differently, using a “floating point”  Two components of the number are stored in the same location in two separate compartments. One is called mantissa (m), and the other is called exponent (e)‏  The Number value is taken as m * B e  Computer uses B as 2 (Binary) or 16 We will take it to be our usual 10  In the program we write such values as 2.53, or -7.2E15  “float y;” reserves 1 location to store a number in this format.

Floating point …  No decimal point is actually stored! It is assumed to be at the beginning of mantissa  Can write as E 11, or as E 9  Mantissa can store about 7 decimal digits. Exponent can be between -100 and +100  double z; mantissa about 14 decimal digits * *

Problems in Numerical Computations  Since computers offer limited precision, the computations carried out on given values often lead to inaccuracies, specially with floating point numbers  Special care needs to be taken to handle such ‘round off’ errors  There are many text books which show how these problems can be handled by using special techniques and algorithms in our programs Numerical recipes in C (C++, Java)

Example of problems with floating Point arithmetic float w, y=1.5, avogadro = E 23 ; w = avogadro + y;  What is the value of w?  Exact value of w and avogadro will differ in the 23rd digit.  “float” stores 7 digits of mantissa. Digits beyond are ignored. (roundoff error). So the value of w is same as avogadro (6.023 E 23)

Summary of our understanding so far “God made natural numbers, the rest is the work of man.” -- Leopold Kronecker “Humans made fractions and decimal numbers, the rest is the work of Dumbos” -- Deepak Phatak

Mixed Arithmetic  computer converts from float to int and vice versa as needed.  In a program, a number written without decimal point is taken as integer, otherwise as float  int N=20; float y=15.8; N=y; : 15.8 is rounded down. N= /N : integer result is calculated /N : float result is calculated.

Remarks on Arithmetic  Arithmetic operators: *, /, %, +, -,  *, / have higher precedence than +, -  *, / have equal precedence, evaluated in left to right order. Similarly +, -  x = m % n; % means modulo remainder for m=10, and n=4, x will have value 2.  % same precedence as *, /  () override default precedence.

Use of Comments  Programs are read not only by compilers, but also by programmers and other humans  You should include extra description (“comments”) to help other programmers figure out the logic:  Style 1: “//......” to end of line  Style 2: “/*.... */”

An important notion: Reassignment int m m =5; … m = 3*m + 1; // Reassignment  This is not an equation, as it would be Mathematically absurd, because it would simplify to: 0 = 2*m+1  Computer interpretation: Calculate the value of the expression on rhs, then store it in the variable on lhs.

More on assignment... “increment” is a special form of reassignment count = count + 1; or count += 1; or, more simply, ++count; or count++; Input and Output instructions in C are not “natural” Handled through special “functions”

Conditional execution Program instructions are normally executed in the given sequence We can examine conditions, and based on the result, can execute instructions out of sequence if (condition) { statement group 1}; else { statement group 2};

Problem requiring conditional execution Based on the age of a passenger, the ticket cost for bus travel is different. Let us say, it is Rs for an adult, and Rs for a child (< 12 Years) Suppose we write our program as int age, float ticketcost; ticketcost = 12.75; ticketcost = 25.50; cout << ticketcost; We will always get cost as If we exchange the two assignments, then we will always get 12.75

Use of if statement  We need to execute either the first or the second instruction based on the value of age, but never both  Use of the if – else statements will permit us to correctly evaluate the ticket cost... if (age > 12) {ticketcost = 25.50}; else {ticketcost = 12.75}; …  What if elders are charged Rs 20?

If – else if ladder … if (age > 60){ ticketcost = 20.00;} else if (age > 12){ ticketcost = 25.50;} else { ticketcost = 12.75;} …

Example to find Maximum of given numbers // program 4 (prog4.cpp), find maximum of 3 given numbers #include using namespace std; int main() { int a, b, c, max; cout << “Give three numbers” << “\n”; cin >> a, b, c; max = a; if (b > max) {max = b}; if (c > max) {max = c}; cout <<“Maximum is”<< max<<"\n"; return 0; }

Finding Maximum …  If there are more numbers, say 5, we could write: int a, b, c, d, e, max; cin >>a >>b >>c >>d >>e ; max = a; if (b > max) {max = b}; if (c > max) {max = c}; if (d > max) {max = d}; if (e > max) {max = e}; cout<<“Max is”<< max << "\n";  What do we write to find maximum of 200 numbers, or when number of numbers itself varies every time a user runs the program?

Specifying comparison Conditions  “ a op b” where op is, =, ==, !=  == : is equal to  != : is not equal to  Conditions can be combined: (a > 0) && (a <= 9) : true if a is a positive digit.  && : conjunction. “and”  || : disjunction. “or”  In the programming language C, evaluation of any condition results in a value 1 if the condition is true, and 0 if it is false

Questions?

Question From NIT Kurukshetra  why do we use printf insted of print

Question From NIT Kurukshetra  why do we use printf insted of print

Question From NIT Kurukshetra  why do we use printf insted of print

Question From JEC_Kukas How will convert any floating point number in to integer number