Recursion. Functions – reminder A function can call other functions. Return causes the execution of the function to terminate and returns a value to the.

Slides:



Advertisements
Similar presentations
The Algorithmic problems?
Advertisements

Starting Out with Java: From Control Structures through Objects
Problem definition The Tower of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exercise 4 Recursion. Functions – a short reminder a group of variables and statements that is assigned a name a sub-program  inside main we can call.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CSC 212 Recursion By Dr. Waleed Alsalih. Definition A recursive function (method) is one that calls itself. A recursive method must have a basis part.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Recursion.
C Programming.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion Data Structures.
Recursion Department of Computer Science-BGU יום שני 10 דצמבר 2018.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Presentation transcript:

Recursion

Functions – reminder A function can call other functions. Return causes the execution of the function to terminate and returns a value to the calling function. The type of the value returned must be the same as the return-type defined for the function. return-type name(argType1 arg1, argType2 arg2, …) { function body; return value; }

A recursive definition C functions can also call themselves!  However, usually not with the same parameters (why?) Some functions can be defined using smaller occurrences of themselves. Such a definition is called a “recursive definition” of a function.

Recursive calling Example: void func(int n){ putchar(`*`); func(n); } Infinite series of * What is the problem ? Look for other problem …

Factorial By definition : n! = 1*2*3*… *(n-1)*n Thus, we can also define factorial the following way:  0! = 1  n! = n*(n-1)! for n>0 (n-1)! *n

Example - factorial int factRec(int n){ if (n==0 || n==1) return 1; return n*factRec(n-1); } int factorial(int n){ int fact = 1; while (n >= 1) { fact *=n; n--; } return fact; }

Conclusions for Recursive calling Every recursive function has a “boundary condition”. The function stops calling itself when it is satisfied.

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… 4*…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… 3*…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns… 2*…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns… FactRec(1) n 1 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns… FactRec(1) n 1 Returns…

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns… FactRec(1) n 1 Returns… 1

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… FactRec(2) n 2 Returns… 2*1

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… FactRec(3) n 3 Returns… 3*2

Recursive factorial – step by step int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… 4*6

הדפסת מספרים - דוגמא 1 #include void print1(int n){ if (n>=0){ printf("%d ",n); print1(n-1); { void main(){ int i = 3; print1(i); putchar('\n'); { 3210

הדפסת מספרים - דוגמא 2 #include void print2(int n){ if (n>=0){ print2(n-1); printf("%d ",n); { void main(){ int i = 3; print2(i); putchar('\n'); { 0123

הדפסת מספרים - דוגמא 3 #include void print3(int n){ if (n>=0){ printf("%d ",n); print3(n-1); printf("%d ",n); { void main(){ int i = 3; print3(i); putchar('\n'); {

הדפסת מספרים - דוגמא 4 #include void print4(int n){ if (n>=0){ print4(n-1); printf("%d ",n); print4(n-1); { void main(){ int i = 3; print4(i); putchar('\n'); {

Another example - power X y = x*x*…*x Recursive definitions (assume non-negative y):  Base: x 0 =1 1. X y = X*(X y-1 ) 2. X y = (X y/2 ) 2 (for even y ’s only) y times

Fibonacci Series Fibonacci definition:  n 0 = 0  n 1 = 1  n n = n n-1 + n n …

Fibonacci Iterative void fibonacci(int n) { int Fn, Fn1, Fn2, ind; Fn2 = 0 ; Fn1 = 1 ; Fn = 0 ; if ( n == 1 ) Fn = 1 ; for (ind=2 ; ind <= n ; ind++){ Fn = Fn1 + Fn2; Fn2 = Fn1; Fn1 = Fn; } printf("F(%d) = %d \n", n, Fn); }

Fibonacci Recursive fibonacci(1) fibonacci(5) fibonacci(4)fibonacci(3) fibonacci(2) fibonacci(1) fibonacci(2)fibonacci(1) fibonacci(0) fibonacci(1)fibonacci(0) int fibonacci(int n) { if (n==0) return 0; if (n==1) return 1; return fibonacci(n-1) + fibonacci(n-2); }

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… 2*… y 5

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… y 4

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… y 4

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… y 4

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… y 2

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… y 2

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… y 2

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… y 1

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… y 1

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… y 1

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… y 1

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… 2*… y 1

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… 2*… y 1 rec_pow(2, 0) x 2 Returns… y 0

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… 2*… y 1 rec_pow(2, 0) x 2 Returns… y 0

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… 2*… y 1 rec_pow(2, 0) x 2 Returns… 1 y 0

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(…) y 2 rec_pow(2, 1) x 2 Returns… 2*1 y 1

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(…) y 4 rec_pow(2, 2) x 2 Returns… square(2) y 2

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… y 5 rec_pow(2, 4) x 2 Returns… square(4) y 4

rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 5) x 2 Returns… 2*16 y 5

Exercise Write a program that receives two non-negative integers and computes their product recursively. Not * multiple operator !! Hint: Notice that the product a*b is actually a+a+…+a (b times).

Solution int recMult( int x, int y ) { if( x == 0) return 0; return y + recMult( x-1,y); {

Exercise Given the following iterative version of sum-of-digits calculation Find the recursive definition of this function (don’t forget the base case!) int sum_digits(int n){ int sum = 0; while (n > 0) { sum += n%10; n = n/10; } return sum; }

Solution int sumOfDigits( int x ) { if( x < 0) x *= -1; if( x == 0 ) return 0; else return x % 10 + sumOfDigits( x / 10 ); }

More uses Recursion is a general approach to programming functions. Its uses are not confined to calculating mathematical expressions! For example : write a function that finds the max member in an array of integer.

Solution int rec_max(int arr[ ], int size){ int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; }

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2 rec_max(764, 1) rest … Returns… arr 764 size 1

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2 rec_max(764, 1) rest … Returns… arr 764 size 1

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest … Returns… arr 760 size 2 rec_max(764, 1) rest … Returns… 3 arr 764 size 1

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest 3 Returns… arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest 3 Returns… arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 rec_max(760, 2) rest 3 Returns… 5 arr 760 size 2

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… 5 arr 756 size 3

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest 5 Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest 5 Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest 5 Returns… arr 752 size 4

max_rec – step by step int rec_max(int arr[ ], int size) { int rest; if (size == 1) return arr[0]; else { rest = rec_max(arr+1, size-1); if (arr[0] > rest) return arr[0]; else return rest; } rec_max(752, 4) rest 5 Returns… 5 arr 752 size 4

חיפוש בינארי int BinarySearch(int arr[],int x, int left, int right) { int middle; if(left>right) return -1; else { middle=(left+right)/2); if(arr[middle]==x) return middle; else if(x < arr[middle]) return BinarySearch(arr,x,left,middle-1); else return BinarySearch(arr,x,middle+1,right); }

Towers of Hanoi The Towers of Hanoi problem consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:  Only one disk may be moved at a time.  Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.  No disk may be placed on top of a smaller disk.

Towers of Hanoi

Recursive Solution To move n disks from peg A to peg C:  move n−1 disks from A to B. This leaves disk #n alone on peg A  move disk #n from A to C  move n−1 disks from B to C so they sit on disk #n

Recursive Solution - Function void hanoi(int x, char from, char to, char aux){ if(x==1){ printf("Move Disk From %c to %c\n", from, to); } else { hanoi(x-1,from,aux,to); printf("Move Disk From %c to %c\n", from, to); hanoi(x-1,aux,to,from); }

Scope of variables - reminder A variable declared within a function is unrelated to variables declared elsewhere. A function cannot access variables that are declared in other functions.