






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of recursion, a problem-solving technique used in computer science. It covers the concept of recursion, its golden rules, and examples of recursive algorithms such as dictionary lookup, course prerequisites, factorial, and power functions. Students are encouraged to read and practice the concepts presented in the document.
Typology: Assignments
1 / 12
This page cannot be seen from the preview
Don't miss anything!







1 Summary of Classes 1
Summary of Classes
If you define any constructor, you must define the default constructor
CS 1: Problem Solving in C++ – Classes can be passed as input to functions and returned.
©c Malik Magdon-Ismail, RPI, November 27, 2006
2 Recursion 2
Recursion
“Defining a task in terms of itself”
Circular?
Examples:
fustic
: the wood of a large, tropical American tree
Chlorophora tinctoria
Gaelicof the mulberry family, yielding a light yellow dye.
: of or pertaining to the Gaels or Gaelic.
CS 1: Problem Solving in C++
©c Malik Magdon-Ismail, RPI, November 27, 2006
4 Course Prerequisites 4
Course Prerequisites
find
prerequisites
foo
1: Start a prerequisite list of courses
2: Get prerequisites of
foo
3: for
each
prereq
do
4:
Add it to list if it is not already there and it is not taken.
5:
if
prereq
is added
then
6:
find
prerequisites
prereq
7:
end if
8: end for
Some course must need no prerequisites - the “simple” courses.
The prerequisites should in some sense be “simpler” than the course itself.
The prerequisite structure needs to be consistent. An inconsistency could be
Prerequisites are CS2 and Discrete Math.
Discrete Math:
Prerequisites are Calc2 and DSA.
CS 1: Problem Solving in C++
©c Malik Magdon-Ismail, RPI, November 27, 2006
5 Course Prerequisites 5
Course Prerequisites
find
prerequisites
foo
1: Start a prerequisite list of courses
2: Get prerequisites of
foo
3: for
each
prereq
do
4:
Add it to list if it is not already there and it is not taken.
5:
if
prereq
is added
then
6:
find
prerequisites
prereq
7:
end if
8: end for
9: CSCI-4320 Parallel Programmingreturn
.... Prereqs: CSCI-2400,2500.
CSCI-2400 Mod. Comp.
.... Prereqs: CSCI-2300, MATH-2800.
.... Prereqs: CSCI-1200, MATH-1010,2800.
.... Prereqs: CSCI-1100.
.... Prereqs: None.
MATH-2800 Discrete Structures
.... Prereqs: MATH-1010.
MATH-1010 Calculus I
.... Prereqs: none.
CS 1: Problem Solving in C++
©c Malik Magdon-Ismail, RPI, November 27, 2006
7 Solving Problems Recursively 7
Solving Problems Recursively
as if
the simpler one was solved – the base case
takes care of the rest.
CS 1: Problem Solving in C++
©c Malik Magdon-Ismail, RPI, November 27, 2006
8 Two Examples –
(^) n !, x n
8
Two Examples –
n !, x
n
Compute
n ! for integer
n ≥ (^) 0.
f actorial
n )
prod
for
i = 1 to
n
do
prod
prod
(^) i
end for
Compute
x n for integer
n ≥
0 and
x >
power
x, n
prod
for
i = 1 to
n
do
prod
prod
(^) x
end for
CS 1: Problem Solving in C++
©c Malik Magdon-Ismail, RPI, November 27, 2006
9 Thinking Recursively Relating the more complex problem to the simpler one
n !
x n
n = 0
n >
n ! = (
n (^) −
(^) 1)!
(^) n
x n =
x (^) ∗ (^) x n − 1
1: factorial(n)
2: if
n = 0
then
3:
return 1;
4: end if
5: return n*factorial(n-1);
1: power(x,n)
2: if
n = 0
then
3:
return 1;
4: end if
5: return x*power(x,n-1);
progress isIs this circular – recursive faith: bases cases take care of “circularity” as long as recursive
always
made.
CS 1: Problem Solving in C++
©c Malik Magdon-Ismail, RPI, November 27, 2006
10 Coding Recursion – if you can pseudo-code, you can code 10
Coding Recursion – if you can pseudo-code, you can code
n !
x n
Inductive
int factorial(int n){
return prod;for(i=1;i<=n;i++)prod*=i;int i,prod=1;
double power(double x, int n){
return prod;for(i=1;i<=n;i++)prod*=x;int i;double prod=1;
Recursive
int factorial_rec(int n){
return n*factorial(n-1);if(n==0)return 1;
int power_rec(double x, int n){
return x*power(x,n-1);if(n==0)return 1;
CS 1: Problem Solving in C++ Code both versions of both functions. See them both work!
©c Malik Magdon-Ismail, RPI, November 27, 2006