CS 112 Introduction to Programming - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

CS 112 Introduction to Programming

Description:

Towers of Hanoi. 3. 3. Recap: What is Recursion? ... Tower of Hanoi: Code. void MoveTower ( int numDisks, int start, int end, int temp ) ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 26
Provided by: zooCs
Category:

less

Transcript and Presenter's Notes

Title: CS 112 Introduction to Programming


1
CS 112 Introduction to Programming
  • Lecture 32
  • Recursive ProgrammingHttp//zoo.cs.yale.edu/clas
    ses/cs112/

2
Outline
  • Recap
  • Recursive programming
  • Palindrome
  • Change Maker
  • Towers of Hanoi

3
Recap What is Recursion?
  • A method in C can invoke itself if set up that
    way, it is called a recursive method
  • Recursion follows the mathematical notion of
    induction
  • there is a base case
  • there is an inductive (recursion) step
  • solve a smaller problem

4
Recap Recursive Sum
int Sum( int n ) if ( n 0 )
// base case return 0 else
// recursive case
return Sum( n-1 ) n
Sum(4)
Sum(0)
4
0
10
0
5
Recap Local and Formal Variables
  • Like a normal method call, each invocation of a
    recursive method has its own set of method
    variables, i.e., formal parameters and local
    variables

void LotsOfPrints(int num) int local
2num if (num 0)
else
LotsOfPrints( num 1 )
Console.WriteLine( local )
LotsOfPrints(3) 2 4
6
6
Control Flow
void LotsOfPrints( int num ) int local
2num if (num 0) // base case
else // recursion
LotsOfPrints( num 1 )
Console.WriteLine( local )
LotsOfPrints(3)
2
4
6






LotsOfPrints(2)
LotsOfPrints(1)
LotsOfPrints(0)
WriteLine(local)
WriteLine(local)
WriteLine(local)




7
Exercise
  • Without using any loop statement, write a method
    Power(int x, uint n) that raises integer x to the
    power of n, where n is a non-negative integer?
  • Examples
  • power(2, 1) 2
  • power(2, 2) 4
  • power(2, 10) 1024

8
Recursive Exponentiation
int Power( int x, uint n ) if ( n 0 )
// base case return 1 else
// recursive case
int temp Power( x, n/2 ) temp
temp if ( n 2 1 )
temp x return temp
See Power.cs
9
Writing Recursive Programs
  • Determine the structure of your method
  • think of the sub-problem(s) that you may solve to
    solve the current problem
  • make the definition of your method general enough
    to handle them so that you can use recursion
  • i.e., the sub-problem can be solved using the
    same method with different parameters
  • Determine the recursive case
  • Determine the base case

10
Outline
  • Review and admin.
  • Palindrome
  • Change maker
  • Towers of Hanoi

11
Palindrome Problem
  • A palindrome is a string that reads the same
    backward or forward
  • Question how do you check a string to see
    whether or not it is a Palindrome?
  • what is a sub-problem you can reduce your current
    problem to
  • and what is the signature of the method to allow
    recursion?
  • what is the recursive case?
  • what is the base case?

x
y
z
a
z
y
x
12
Palindrome
  • CheckPalindrome(s, left, right)
  • Recursive case
  • CheckPalindrome (s, left 1, right - 1)
  • Base case
  • if left gt right, then true
  • if sleft ! sright, then false

13
Palindrome Check
bool CheckPalindrome( string s, int left, int
right ) if ( left gt right ) //
base case 1 return true else if (
sleft ! sright ) // base case 2
return false else
// recursion return CheckPalindrome(
s, left 1, right 1 )
See Palindrome.cs
14
Outline
  • Review and admin.
  • Palindrome
  • Change maker
  • Towers of Hanoi

15
Change Maker
  • Recursion is useful for trying all possibilities
  • Consider the change making problem
  • Count the number of ways to make change for a
    given amount with pennies, nickels, dimes,
    quarters
  • Questions
  • what are sub-problems you can reduce your current
    problem to
  • and what is the signature of the method to allow
    recursion?
  • what is the recursive case?
  • what is the base case?

16
Change Maker Structure
  • Order coin types in increasing order and number
    them
  • Count( amount, numCoinTypes )
  • number of ways to make amount cents using
    numCoinTypes coins where numCoinTypes could be
    0, 1, 2, 3, 4

17
Change Maker Recursive Case
  • Question Count( 11, 2 ) ?
  • Count( 11, 2 ) sum of
  • Count( 6, 2 ) // use one nickel
  • Count( 11, 1 ) // discard the nickel type
  • Two paths
  • use one more coin of the highest denomination,
    reduce amount and try to make change for
    remaining money
  • or discard the highest denomination and try to
    make changes with remaining denominations

18
Change Maker Recursive Case
3
11, 2
2
1
1
1
1
0
19
Change Maker Recursive Case
4
15, 2
3
1
2
1
1
1
20
Change Maker Base Case
  • amount lt 0 numCoinTypes 0 0
  • amount 0 numCoinTypes 1 1

21
Change Maker Code
int coins 1, 5, 10, 25 int Count( int
amount, int numCoinTypes ) if ( amount lt 0
numCoinTypes 0 ) // base case
1 return 0 else if ( amount 0
numCoinTypes 1 ) // base case 2
return 1 else
// recursive
case return Count( amount -
coinsnumCoinTypes - 1, numCoinTypes )
Count( amount,
numCoinTypes 1 )
See ChangeMaker.cs
22
Outline
  • Review and admin.
  • Palindrome
  • Change maker
  • Towers of Hanoi

23
Towers of Hanoi
  • The Towers of Hanoi is a puzzle made up of three
    vertical pegs and several disks that slide on
    the pegs
  • The disks are of varying size, initially placed
    on one peg with the largest disk on the bottom
    with increasingly smaller ones on top
  • The goal is to move all of the disks from peg 1
    to peg 3 under the following rules
  • we can move only one disk at a time
  • we cannot move a larger disk on top of a smaller
    one

24
Questions
  • What are sub-problems you can reduce your current
    problem to
  • and what is the signature of the method to allow
    recursion?
  • What is the recursive case?
  • What is the base case?

25
Tower of Hanoi Code
void MoveTower ( int numDisks, int start, int
end, int temp ) if ( NumDisks 1 ) //
base case Console.WriteLine( move
disk from start to end ) else
// recursion
MoveTower ( numDisks-1, start, temp, end
) Console.WriteLine( move disk from
start to end ) MoveTower
( numDisks-1, temp, end, start )
public static void Main( string args )
MoveTower(4, 1, 3, 2)
See HanoiTowers.cs
Write a Comment
User Comments (0)
About PowerShow.com