Data Structures and Algorithms

Similar documents
Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

A function that invokes itself is said to

STUDENT LESSON A9 Recursion

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Warmup: On paper, write a C++ function that takes a single int argument (n) and returns the product of all the integers between 1 and n.

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

6. C++ Subprograms David Keil CS I slides 7/03 1

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

Lecture 2:- Functions. Introduction

Add Subtract Multiply Divide

CSCE 121 ENGR 112 List of Topics for Exam 1

Data Structures And Algorithms

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

CS101 Computer Programming and Utilization

Functions and Recursion

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

Binghamton University. CS-211 Fall Functions Revisited. Function Internals

CS101 Computer Programming and Utilization

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

CS101 Computer Programming and Utilization

15 212: Principles of Programming. Some Notes on Induction

CSci 1113 Final. Name: Student ID:

Companion C++ Examples

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

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

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Unit #3: Recursion, Induction, and Loop Invariants

Local and Global Variables

Lab 2.1: Fixing a C++ program

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

Function with default arguments

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Run-Time Data Structures

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Functions. Introduction :

Introduction to Scientific Programming with C++

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

Shell CSCE 314 TAMU. Functions continued

Algorithms and Programming I. Lecture#12 Spring 2015

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Chapter 4 Functions By C.K. Liang

Why Is Repetition Needed?

Problem 1 Write the best title lines for the functions that are called by the following main program. Do not supply the blocks for the functions.

Introduction to C++ Introduction to C++ 1

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

FORM 2 (Please put your name and form # on the scantron!!!!)

Downloaded from

Recursion. Recursion is: Recursion splits a problem:

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Understanding main() function Input/Output Streams

Patterns The Essence of Functional Programming

CS 161 Exam II Winter 2018 FORM 1

PROBLEM 1 : (Vocabulary: 12 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the contex

Split up Syllabus (Session )

Programming in MATLAB

Exercises C-Programming

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Lab Instructor : Jean Lai

From Pseudcode Algorithms directly to C++ programs

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Chapter 5 C Functions

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

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

JTSK Programming in C II C-Lab II. Lecture 3 & 4

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

We cover recursion in 150. Why do it again in 151?

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

C++ Programming Lecture 11 Functions Part I

4. C++ functions. 1. Library Function 2. User-defined Function

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

Functions. Arash Rafiey. September 26, 2017

Study Guide for Test 2

OOP THROUGH C++(R16) int *x; float *f; char *c;

Transcription:

Data Structures and Algorithms Milind Sohoni www.cse.iitb.ac.in/ sohoni email: sohoni@cse.iitb.ac.in December 17, 2016 1 / 9

Recursion The function achieved a separation of the evaluation of the function from its root finding. Thus the two activities can be separately implemented. We have seen the use of function to Avoid duplication of code. AxB.c Separate two concepts. rootfinding.c December 17, 2016 2 / 9

Recursion The function achieved a separation of the evaluation of the function from its root finding. Thus the two activities can be separately implemented. We have seen the use of function to Avoid duplication of code. AxB.c Separate two concepts. rootfinding.c AND NOW think differently! December 17, 2016 2 / 9

Recursion The function achieved a separation of the evaluation of the function from its root finding. Thus the two activities can be separately implemented. We have seen the use of function to Avoid duplication of code. AxB.c Separate two concepts. rootfinding.c AND NOW think differently! #include <iostream.h> #include <math.h> int fact(int x) else return(x*fact(x-1)); int main() int N; cout << "N?"; cin >> N; cout << fact(n); December 17, 2016 2 / 9

The function fact calls itself, but with a smaller argument. It is clear that N! = N ((N 1)!) and the code imitates that. Note that fact has one part which stops the recursion, i.e, when x==1. The other calls fact(x-1). The calling sequence is the order in which factorial are executed and the input arguments. The values are returned in the reverse order. Thus the call to fact(5) is complete only after fact(4) has returned a value. #include <iostream.h> #include <math.h> int fact(int x) else return(x*fact(x- int main() int N; cout << "N?"; cin >> N; cout << fact(n); calling sequence December 17, 2016 3 / 9

Old Problem Count the number of sequences of length n over 0-1 with NO consecutive zeros. a n = strings as above but ending in 0 b n = strings as above but ensing in 1 Our interest is in a n + b n. We have: a n = b n 1 b n = a n 1 + b n 1 Old Solution Using Arrays int A[10], B[10]. December 17, 2016 4 / 9

Old Problem Count the number of sequences of length n over 0-1 with NO consecutive zeros. a n = strings as above but ending inelse 0 return(b(x-1)); b n = strings as above but ensing in 1 Our interest is in a n + b n. We have: a n = b n 1 b n = a n 1 + b n 1 Old Solution Using Arrays int A[10], B[10]. AnBn.c int B(int x); int A(int x) int B(int x) else return(a(x-1)+b(x-1)) int main() int N; cin >> N; December 17, 2016 4 / 9

AnBn.c #include <iostream.h> #include <math.h> int B(int x); int A(int x) else return(b(x-1)); int B(int x) else return(a(x-1)+b(x-1)); int main() Many things to note here: The programs for A and B mimic their mathematical definitions. There are two functions calling each other recursively. Note the peculiar single line header of B. If this were absent, the program would not compile. AnBn.c: In function int AnBn.c:6: error: B und (first use this AnBn.c: In function int December 17, 2016 5 / 9

AnBn.c WARNING: Recursion is simpler to implement but Harder to debug. Generally Inefficient. In this case: A4 calls B3 which will call A2, B2 and so on. B4 will call A3,B3. However, the A4 call of B3 is forgotten and cannot be re-used. #include <iostream.h> #include <math.h> int B(int x); int A(int x) else return(b(x-1)); int B(int x) else return(a(x-1)+b(x-1)) int main() December 17, 2016 6 / 9

A4 B4 In Total A3 B3 B3 B2 A2 B2 A2 B2 We see that there are: A1 B1 B1 A1 B1 B1 A1 B1 5 calls to B1, 3 calls to A1. 3 calls to B2 and 2 calls to A2. 2 calls to B3 and 1 call to A3. AnBn.c #include <iostream.h> #include <math.h> int B(int x); int A(int x) else return(b(x-1)); int B(int x) else return(a(x-1)+b(x-1) int main() December 17, 2016 7 / 9

Summary Functions have three typical uses: save code repetition. separate distinct parts of the code conceptualize mathematical definitions The function must be specified before the main program. It must have input arguments and an output value. The calling program must respect these attributes. Control temporarily passes to the function and returns to the next statement. Problems Let R 1 and R 2 be two rectangles in a plane. Show that there is a line which will cut both rectangles into equal halves. Write a program to input two sets of four points. Then (i) check that each set marks a rectangle, and (ii) compute the cut above. Write a program which takes in a positive integer and prints one factorization of it into primes. December 17, 2016 8 / 9

Thanks December 17, 2016 9 / 9