A Look Back at Functions: Recursion
A Look Back at Functions: Recursion
#include <stdio.h>
#include <math.h>
double distance(double x1, double y1, double x2, double y2)
{
double dist;
dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
int main()
{
double milage = distance(0, 0, 3, 4);
printf("Milage: %lf\n",milage);
return 0;
Lecture 11 }
Recursion
CSE115: Programming Language I
3 4
A Look Back at Functions A Look Back at Functions
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
double distance(double x1, double y1, double x2, double y2) double distance(double x1, double y1, double x2, double y2)
{ {
double dist; double dist;
dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist; return dist;
} }
int main() int main()
{ {
double milage = distance(0, 0, 3, 4); double milage = distance(0, 0, 3, 4);
printf("Milage: %lf\n",milage); printf("Milage: %lf\n",milage);
return 0; return 0;
} }
(5)
5 6
(5)
main distance
(0,0,3,4)
7
Factorial – Iterative Definition Factorial – Recursive Definition
• To define n! recursively, n! must be defined in terms of
n! = n * (n-1) * (n-2) * … * 2 * 1 for any integer n>0 the factorial of a smaller number.
0! = 1 • Observation (problem size is reduced):
n! = n * (n-1)!
Iterative Definition in C: • Base case: 0! = 1
fval = 1; • We can reach the base case, by subtracting 1 from n if n
for (i = n; i >= 1; i--) is a positive integer.
fval = fval * i;
Recursive Definition:
n! = 1 if n = 0
n! = n*(n-1)! if n > 0
In general:
if (base case)
solve it
else
reduce the problem using recursion // general case
Factorial of 4 (Recursive) Factorial of 4 (Recursive)
Calculate 4! Calculate 4!
4! = 4 * (4-1)! 4! = 4 * (4-1)!
4! = 4 * 3! 4! = 4 * 3!
4! = 4 * __ Calculate 3! 4! = 4 * __ Calculate 3!
3! = 3 * (3-1)! 3! = 3 * (3-1)!
3! = 3 * 2! 3! = 3 * 2!
3! = 3 * __ 3! = 3 * __ Calculate 2!
2! = 2 * (2-1)!
2! = 2 * 1!
2! = 2 * __
Calculate 4! Calculate 4!
4! = 4 * (4-1)! 4! = 4 * (4-1)!
4! = 4 * 3! 4! = 4 * 3!
4! = 4 * __ Calculate 3! 4! = 4 * __ Calculate 3!
3! = 3 * (3-1)! 3! = 3 * (3-1)!
3! = 3 * 2! 3! = 3 * 2!
3! = 3 * __ Calculate 2! 3! = 3 * __ Calculate 2!
2! = 2 * (2-1)! 2! = 2 * (2-1)!
2! = 2 * 1! 2! = 2 * 1!
2! = 2 * __ Calculate 1! 2! = 2 * __ Calculate 1!
1! = 1 * (1-1)! 1! = 1 * (1-1)!
1! = 1 * 0! 1! = 1 * 0!
1! = 1 * __ 1! = 1 * __
Calculate 0!
0! = 1
Factorial of 4 (Recursive) Factorial of 4 (Recursive)
Calculate 4! Calculate 4!
4! = 4 * (4-1)! 4! = 4 * (4-1)!
4! = 4 * 3! 4! = 4 * 3!
4! = 4 * __ Calculate 3! 4! = 4 * __ Calculate 3!
3! = 3 * (3-1)! 3! = 3 * (3-1)!
3! = 3 * 2! 3! = 3 * 2!
3! = 3 * __ Calculate 2! 3! = 3 * __ Calculate 2!
2! = 2 * (2-1)! 2! = 2 * (2-1)!
2! = 2 * 1! 2! = 2 * 1!
2! = 2 * __ Calculate 1! 2! = 2 * 1
1! = 1 * (1-1)! 2! = 2
1! = 1 * 0!
1! = 1 * 1
1! = 1
Calculate 4! Calculate 4!
4! = 4 * (4-1)! 4! = 4 * (4-1)!
4! = 4 * 3! 4! = 4 * 3!
4! = 4 * __ Calculate 3! 4! = 4 * 6
3! = 3 * (3-1)! 4! = 24
3! = 3 * 2!
3! = 3 * 2
3! = 6
Recursive Factorial Function Tracing Factorial(4)
// Computes the factorial of a nonnegative integer.
Factorial(4)
// Precondition: n must be greater than or equal to 0.
// Postcondition: Returns the factorial of n; n is unchanged.
int Factorial(int n)
{
if (n ==0)
return (1);
else
return (n * Factorial(n-1));
}
call call
Factorial (3)
Tracing Factorial(4) Tracing Factorial(4)
Factorial(4) = 4 * Factorial(3) Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2)) = 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
call call
Factorial (1)
“Fibonacci”
(Leonardo de Pisa)
1170-1240
35
Rabbit Rules Fibonacci’s Problem
1. All pairs of rabbits consist of a male and female How many pairs of rabbits will there be 12
2. One pair of newborn rabbits is placed in hutch on January 1 months later?
3. When this pair is 2 months old they produce a pair of baby
rabbits
4. Every month afterwards they produce another pair
5. All rabbits produce pairs in the same manner
6. Rabbits don’t die
37 38
0 2
Jan 1 Mar 1 0
1 3
Feb 1 Apr 1
1 0
4
2 May 1 2 1 0
Mar 1 0
40
3 1 0 In General,
Apr 1
4 2 1 0 0
May 1 Pairs this month
=
Pairs last month
+
Pairs of newborns
43
Fibonacci Numbers Fibonacci Numbers
int Fibonacci(int n) int Fibonacci(int n)
{ {
if(n == 0) if(n == 0)
return 0; return 0;
else if (n == 1) Base case else if (n == 1)
return 1; return 1;
else else
return Fibonacci(n-1)+Fibonacci(n-2); return Fibonacci(n-1)+Fibonacci(n-2);
} } General
case
Home-works
Implement the following function prototypes. You must implement them recursively.
• int sum(int n);
Description: Calculates the the sum 1 + 2 + ⋯ +
• void listNumbersAsc(int start, int end);
Description: Outputs the numbers from start to end in ascending order.
• void listNumbersDesc(int start, int end);
Description: Outputs the numbers from start to end in descending order.
• void printBinary(int x);
Description: Prints the binary equivalent of x.
• int mul(int a, int b);
Description: Returns the product of a and b. The only arithmetic operation that you are allowed to use in this
problem is addition (+).
• int power(int a, int b);
Description: Returns . The only arithmetic operation that you are allowed to use in this problem is
multiplication (*).
• double harmonicSum(int n);
Description: Returns the sum 1 + + + ⋯ + .
• int sumOfDigits(int x);
Description: Returns the sum of digits of the positive integer x. For example, when called with the parameter
12345, this function returns 15.