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
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Lecture #18 EEE 574 Dr. Dan Tylavsky Nonlinear Problem Solvers.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Chapter 6: User-Defined Functions I
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Copyright © Texas Education Agency, Computer Programming For Loops.
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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Iteration for Statements, while Statements and do-while Statements.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CPS120: Introduction to Computer Science Lecture 14 Functions.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Basic Program Construction
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1 CS161 Introduction to Computer Science Topic #9.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
MECN 3500 Inter - Bayamon Lecture 6 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS101: Numerical Computing 2 Abhiram Ranade. Topics More examples of for statement  Evaluating e x.  Hemachandra Numbers  Newton Rhapson method for.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Control Structures 1 The if, for, and while Statements §5.1, §5.4, & §5.5 (Several examples here; also Lab #4)
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Functions.
Arrays & Functions Lesson xx
Introduction to Functions
For Loops October 12, 2017.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Computing Fundamentals
Chapter 6: User-Defined Functions I
Flow of Control.
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 4, Functions Wednesday 30 June 2010

Overview  Iterative Solution (Contd.) Finding roots of a given function Different ways of prescribing iteration  Functions Need, definition and usage  Workshop Projects Lecture 4 Functions

Newton Raphson method  Method to find the root of f(x), i.e. x such that f(x)=0.  Method works if: f(x) and f '(x) can be easily calculated. and a good initial guess is available.  Example: To find square root of k. use f(x) = x 2 - k. f’ (x) = 2x. f(x), f’ (x) can be calculated easily. only few arithmetic operations needed  Initial guess x 0 = 1 It always works! can be proved.

Lecture 4 Functions Newton Raphson method  Method to find the root of f(x), i.e. x such that f(x)=0.  Method works if: f(x) and f '(x) can be easily calculated. and a good initial guess is available.  Example: To find square root of k. use f(x) = x 2 - k. f’ (x) = 2x. f(x), f’ (x) can be calculated easily. only few arithmetic operations needed  Initial guess x 0 = 1 It always works! can be proved. Let x = √k then x 2 = k and x 2 – k = 0

Lecture 4 Functions How to get better x i+1 given x i Point A =(x i,0) known. f’ (x i ) = AB/AC = f(x i )/(x i - x i+1 )  x i+1 = (x i - f(x i )/f’ (x i )) Calculate f(x i ). Point B=(x i,f(x i )) is now known Approximate f by tangent C= intercept on x axis C=(x i+1,0) f(x) xixi x i+1 A B C

Lecture 4 Functions Square root of k  x i+1 = (x i - f(x i )/f’ (x i )) f(x) = x 2 - k, f’ (x) = 2x x i+1 = x i - (x i 2 - k)/(2x i ) = (x i + k/x i )/2  Starting with x 0 =1, we compute x 1, then x 2, and so on  Each successive value of x i will be closer to the root  We can get as close to sqrt(k) as required by carrying out these iterations many times Errors in floating point computations ?

Lecture 4 Functions Program segment // calculating square root of a number k float k; cin >> k; float xi=1; // Initial guess. Known to work. for (int i=0; i < 10; i++){ // 10 iterations xi = (xi + k/xi)/2; } cout << xi;

Lecture 4 Functions Another way float xi, k; cin >> k; for( xi = 1 ; // Initial guess. Known to work. xi*xi – k > || k - xi*xi > ; //until error in the square is at most xi = (xi + k/xi)/2); cout << xi;

Special ways of using ‘for’ for (xxx; yyy; zzz) { www }  In the alternate way we saw, the computations required for each iteration are all specified as part of the specifications of ‘for’ statement itself.  Thus the ‘body’ of statements (www) is missing, because it is not required  A special way of using ‘for’ for (; ; ) { www}  This specifies an infinite iteration, the loop must be broken by some condition within ‘www’ Lecture 4 Functions

Yet Another way float k; cin >> k; float xi=1; While (xi*xi – k > || k - xi*xi > 0.001){ xi = (xi + k/xi)/2 ; } cout << xi;

Lecture 4 Functions While statement while (condition) { loop body};  check condition, if true then execute loop body. Repeat.  If loop body is a single statement, then need not use { }. Always putting braces is recommended; if we later insert a statement, we may forget to put them, so we should do it at the beginning.

Lecture 4 Functions for and while  If there is a “control” variable with initial value, update rule, and whose value distinctly defines each loop iteration, use ‘for’.  Also, if loop executes fixed number of times, use ‘for’.

Functions Consider a quadratic function f(x) = ax 2 + bx + c f’(x)= 2ax + b It would be nice, if we had separate blocks of instructions to calculate these for different values of x A ‘function’ in c is such a separate block It takes one or more parameters and returns a single value of a specified type Lecture 4 Functions

Example of functions float myfunction (float a, float b, float c, float x){ float value; value = a *x*x + b*x + c; return (value); } float myderivative(float a, float b, float x){ float value; value = 2*a*x + b; return (value); } Lecture 4 Functions

Syntax int myfunction (float a, …) {  First word tells the type of the value which will be returned.  Next is the name of the function, which we choose appropriately  This is followed by one or more parameters whose values will come from the calling instruction  Note the return statement: return (value);  this says what value is to be sent back. In general, it can be an expression which is evaluated when return statement is executed Lecture 4 Functions

Function in our model  We had thought of our computer as a dumbo, so imagine each such function to be evaluated by a separate assistant dumbo  Any time a function is invoked within an instruction which is executing, the given parameters are handed over to the assistant dumbo  Assistant dumbo calculates the function value and returns the same to main dumbo  Our main dumbo then onwards carries on from exactly where he left, using that returned value in place of the reference to the function Lecture 4 Functions

Invoking a function (function call) Within a program, a function is invoked simply by using the function name (with appropriate parameters) within any expression In the Newton Raphson method, we have a value xi, and we calculate next value using xi+1 = (xi- f(xi)/f’ (xi)) Suppose our function was f(x) = ax 2 +bx + c Then we could design our program using the two functions which we have written (myfunction and myderivative) Lecture 4 Functions

Newton Raphson using function calls int main() { float x, a, b, c, root; // read a, b, c... x = 1.0; // This is the initial guess for x for (int i=0; i < 10; i++){ x = (x - myfunction(a,b,c,x) /myderivative(a,b,x)); }... } Lecture 4 Functions

Invocation rules  x = (x - myfunction(a,b,c,x)/ myderivative(a,b,x));  When dumbo encounters ‘myfunction’ while evaluating the expression, it suspends execution of the program, goes over to the defined function with the available values of the parameters calculates the value executing given instructions within that function then returns back to the main program, replaces the reference to function by the returned value, and continues evaluation of the remaining expression. Lecture 4 Functions

Some points to ponder  We see that the calculations pertaining to our function evaluation have been separated out, perhaps resulting a better structured or ‘modular’ program  Why is this important Suppose we wish to modify this same program to calculate a root of another function, then it is far easier to replace code only in that part where functions are defined. Otherwise we may have to search our entire code to find which lines we should change Lecture 4 Functions

Some points to ponder...  Can I use programming code for functions written by ohers Yes, of course, that is the very idea We can even compile those functions separately and link them with our program, but we need to include prototype definitions of these functions within the program  We can now understand why we say int (main) … and return 0;  Our entire program is actually treated as a function by the operating system Lecture 4 Functions

Question, From PSG Coimbatore:  If we declare an int variable, the largest value it holds varies from system to system. What is the reason behind this?

Question, From GEC_Thrissur:  How to return to value’s from a function at a time for example two roots of quadratic equation

Question, From NIT_Jalandhar  Can the main function return a float value