0% found this document useful (0 votes)
3 views

09_recursion

Chapter 17 discusses recursion, a programming technique that simplifies complex problems, particularly through the use of recursive functions. An example provided is the calculation of factorials, demonstrating how a recursive function can call itself to compute the value. The document includes a C++ implementation of a factorial function, illustrating its base case and recursive case.

Uploaded by

husbanfaisal6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

09_recursion

Chapter 17 discusses recursion, a programming technique that simplifies complex problems, particularly through the use of recursive functions. An example provided is the calculation of factorials, demonstrating how a recursive function can call itself to compute the value. The document includes a C++ implementation of a factorial function, illustrating its base case and recursive case.

Uploaded by

husbanfaisal6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 17: Recursion

Sections 17.117.2

Textbooks: Y. Daniel Liang, Introduction to Programming with C++, 3rd Edition


© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.

These slides were adapted by Prof. Gheith Abandah from the Computer Engineering Department of the University 1
of Jordan for the Course: Computer Skills for Engineers (0907101)
Outline

• Introduction
• Example: Factorials

2
Motivations
• Recursion is a technique that leads to
elegant solutions to problems that are
difficult to program using simple loops.
• A recursive function is one that invokes itself.
• Suppose you want to find all the files under a
directory that contains a particular word.
How do you solve this problem? There are
several ways to solve this problem. An
intuitive solution is to use recursion by
searching the files in the subdirectories
recursively.
3
Outline

• Introduction
• Example: Factorials

4
Factorials in Math
0! = 1 -
1! = 1 1 x 0!
2! = 2 x1 2 x 1!
3! = 3 x 2 x 1 3 x 2!
4! = 4 x 3 x 2 x 1 4 x 3!
5! = 5 x 4 x 3 x 2 x 1 5 x 4!
6! = 6 x 5 x 4 x 3 x 2 x 1 6 x 5!
7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 7 x 6!
8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 8 x 7!
9! = 9 x 8 x 7 x 6 x5 x 4 x 3 x 2 x 9 x 8!
1
10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 10 x 9!
3x2x1
5
Computing Factorial
n! = n × (n - 1) × (n - 2) × ... × 2 ×
1

0! = 1;
n! = n × (n - 1)!; n > 0

factorial(0) = 1;
factorial(n) = n*factorial(n-1);

ComputeFactorial Run
6
ComputeFactorial.cpp
#include <iostream>
using namespace std;

// Return the factorial for a specified index


long long factorial(int n)
{
if (n == 0) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive call
}

int main()
{
// Prompt the user to enter an integer
cout << "Please enter a non-negative integer: ";
int n;
cin >> n;

// Display factorial
cout << "Factorial of " << n << " is " << factorial(n);

return 0;
} Factorial of 4 is 24 7
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4)

8
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)

9
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)

10
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))

11
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))

12
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))

13
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)

14
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2

15
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
=4*6

16
animation

Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
=4*6
= 24
17
animation

Trace Recursive factorial


Executes factorial(4)

factorial(4)
Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0)
Step 4: executes factorial(0)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function

18
animation

Trace Recursive factorial

factorial(4)
Step 0: executes factorial(4)
Step 9: return 24
Executes factorial(3)
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0) Space Required
for factorial(3)

Step 4: executes factorial(0) Space Required


Step 5: return 1 for factorial(4)
Main function
return 1

19
animation

Trace Recursive factorial

factorial(4) Executes factorial(2)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1 Space Required
for factorial(2)

return 1 * factorial(0) Space Required


for factorial(3)

Step 4: executes factorial(0) Space Required


Step 5: return 1 for factorial(4)
Main function
return 1

20
animation

Trace Recursive factorial

factorial(4) Executes factorial(1)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Space Required
Step 3: executes factorial(1)
Step 6: return 1 for factorial(1)
Space Required
for factorial(2)
return 1 * factorial(0) Space Required

Step 4: executes factorial(0) for factorial(3)

Step 5: return 1 Space Required


for factorial(4)
return 1 Main function

21
animation

Trace Recursive factorial

factorial(4) Executes factorial(0)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1) Space Required


for factorial(0)
Space Required
Step 3: executes factorial(1)
Step 6: return 1 for factorial(1)
Space Required
for factorial(2)
return 1 * factorial(0) Space Required

Step 4: executes factorial(0) for factorial(3)

Step 5: return 1 Space Required


for factorial(4)
return 1 Main function

22
animation

Trace Recursive factorial

factorial(4) returns 1
Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1) Space Required


for factorial(0)
Space Required
Step 3: executes factorial(1) for factorial(1)
Step 6: return 1 Space Required
for factorial(2)
return 1 * factorial(0) Space Required
for factorial(3)
Step 4: executes factorial(0) Space Required
Step 5: return 1 for factorial(4)
Main function
return 1

23
animation

Trace Recursive factorial

factorial(4) returns factorial(0)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1) Space Required


for factorial(0)

Step 3: executes factorial(1) Space Required


Step 6: return 1 for factorial(1)
Space Required
return 1 * factorial(0) for factorial(2)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function

24
animation

Trace Recursive factorial

factorial(4) returns factorial(1)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1) Space Required
Step 6: return 1 for factorial(1)
Space Required
return 1 * factorial(0) for factorial(2)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function

25
animation

Trace Recursive factorial

factorial(4) returns factorial(2)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
Space Required
return 1 * factorial(0) for factorial(2)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function

26
animation

Trace Recursive factorial

factorial(4) returns factorial(3)


Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function

27
animation

Trace Recursive factorial


returns factorial(4)

factorial(4)
Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack

return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0)
Step 4: executes factorial(0)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function

28
factorial(4) Stack
Trace

29
Outline

• Introduction
• Example: Factorials

30

You might also like