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.

Slides:



Advertisements
Similar presentations
Understanding Recursion. Introduction zRecursion is a powerful programming technique that provides elegant solutions to certain problems.
Advertisements

Functions a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value a sub-program when we.
Recursion.
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 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
1 Gentle Introduction to Programming Session 2: Functions.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
1 Gentle Introduction to Programming Session 3: Higher Order Functions, Recursion.
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.
1 Gentle Introduction to Programming Session 3: Recursion.
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)
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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.
1 Gentle Introduction to Programming Session 4: Arrays.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Recursion. Functions – reminder A function can call other functions. Return causes the execution of the function to terminate and returns a value to the.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
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.
A First Book of ANSI C Fourth Edition
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Understanding Recursion
CS314 – Section 5 Recitation 9
Sections 4.1 & 4.2 Recursive Definitions,
C Programming.
Topic 6 Recursion.
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Formatted and Unformatted Input/Output Functions
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Recursion.
Recursion Department of Computer Science-BGU יום שני 10 דצמבר 2018.
A function with one argument
Module 1-10: Recursion.
CS302 - Data Structures using C++
CS148 Introduction to Programming II
Main() { int fact; fact = Factorial(4); } main fact.
Recursion.
Recursive Thinking.
REPETITION Why Repetition?
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

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 other functions  These functions can call other functions.

Another way of looking at factorial As we saw, 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

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. Every recursive function has a “boundary condition”. The function stops calling itself when it is satisfied.  Why is this necessary?

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; }

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 FactRec(4) n 4 Returns… 4*… int factRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }

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

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

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

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

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

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

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

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

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

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

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

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

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

Example rec_pow.c

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 rec_pow(2, 5) x 2 Returns… y 5 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 – 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 rec_pow(2, 5) x 2 Returns… 2*… y 5 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); }

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 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. Hint: Notice that the product a*b is actually a+a+…+a (b times).

Solution rec_mul.c

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 sum_digit_rec.c

More uses Recursion is a general approach to programming functions Its uses are not confined to calculating mathematical expressions! For example – max_rec.c

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 rec_max(752, 4) rest … Returns… arr 752 size 4 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 rec_max(752, 4) rest … Returns… arr 752 size 4 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 rec_max(752, 4) rest … Returns… arr 752 size 4 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest … Returns… arr 756 size 3 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… arr 756 size 3 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… arr 756 size 3 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… arr 756 size 3 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 rec_max(752, 4) rest … Returns… arr 752 size 4 rec_max(756, 3) rest 5 Returns… 5 arr 756 size 3 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 rec_max(752, 4) rest 5 Returns… arr 752 size 4 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 rec_max(752, 4) rest 5 Returns… arr 752 size 4 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 rec_max(752, 4) rest 5 Returns… arr 752 size 4 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 rec_max(752, 4) rest 5 Returns… 5 arr 752 size 4 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; }

Riddle: what does this do? char *rec_func(char str[], char c) { if (*str == '\0') return NULL; if (*str == c) return str; return rec_func(str+1, c); }

Solution A recursive implementation of strchr See strchr_rec.c

Exercise Write a recursive implementation of strcmp –  Input – two strings  Output – 0 if both are equal, 1 if not Write a program that accepts two strings from the user and checks whether they are equal

Solution strcmp_rec.c