Golden Rules of Recursion and Recursive Problem Solving in C++, Assignments of Computer Science

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-cf8
koofers-user-cf8 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 Summary of Classes 1
1 Summary of Classes
Classes have member variables (data) and member functions (operations on the data)
Can define constructors for initialization.
If you define any constructor, you must define the default constructor.
Use classes in assignment and in vector operations.
Cannot use other operations like +,, /, .
Classes can be passed as input to functions and returned.
CS 1: Problem Solving in C++ c
Malik Magdon-Ismail, RPI, November 27, 2006
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Golden Rules of Recursion and Recursive Problem Solving in C++ and more Assignments Computer Science in PDF only on Docsity!

1 Summary of Classes 1

Summary of Classes

  • Can define constructors for initialization. – Classes have member variables (data) and member functions (operations on the data)

If you define any constructor, you must define the default constructor

  • Cannot use other operations like +– Use classes in assignment and in vector operations.

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:

  1. Dictionary Lookup.

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.

  1. Course Prerequisites. .... Prerequisites: CSCI-2400, CSCI-2500. CSCI-4320 Parallel Programming

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

DSA:

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.

CSCI-2300 DSA

.... Prereqs: CSCI-1200, MATH-1010,2800.

CSCI-1200 CS II

.... Prereqs: CSCI-1100.

CSCI-1100 CS I

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

  1. Use (2) to solve the harder problem2. Relate the problems which are harder to simpler ones. 1. Identify the base cases – the problems which are easy to solve.

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