10-07feb22recursion

# 10-07feb22recursion - String swapAdjacent(String s c...

This preview shows pages 1–8. Sign up to view the full content.

1 CS100J 22 Feb 2006 More on Recursion My first job was working in an orange juice factory, but I got canned: couldn't concentrate. Then I worked in the woods as a lumberjack, but I just couldn't hack it, so they gave me the axe. After that I tried to be a tailor, but I just wasn't suited for it. Mainly because it was a so-so job. Next I tried working in a muffler factory but that was exhausting. I worked as a pilot but eventually got grounded for taking off too much. Then I tried teaching but I couldn't make the grade. We derive recursive functions and look at execution of recursive calls. Study Sect 15.1, p. 415. Watch activity 15-2.1 on the CD. In DrJava, write and test as many of the self-review exercises as you can (disregard those that deal with arrays). Get more of these from the course website Prelim tonight in Olin 155 at 7:30 PM.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Geometry test
3 Recursive functions /** = a copy of s in which s[0. .1] are swapped, s[2. .3] are swapped, s[3. .4] are swapped, etc. */ public static

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: String swapAdjacent(String s) c Properties: 4 Recursive functions c c number of calls 0 1 1 2 2 2 4 3 8 4 1 5 32 6 2 n n + 1 32768 is 2 15 so b 32768 needs only 16 calls! 5 Tiling Elaine’s kitchen n n n n 6 Tiling Elaine’s kitchen n n n n 7 Executing recursive calls Steps in executing a call: 1. Draw a frame for the call, including the parameters and local variables and scope box. 2. Assign argument values to the parameters. 3. Executed the method body. 4. Erase the frame —and give value of function call to caller. method name: counter scope box put parameters and local variables here 8 Executing recursive calls /** = n!. Precondition: n 0 */ ≥ public static int fact( int n) { if (n <= 1) return 1; int b = fact(n-1); return n * b; } fact: 1 scope box n b 0! = 1. n! = n * (n-1) * (n-2) * … * 2 * 1 1. Draw frame 2. Assign argument values to parameters 3. Execute body 4. Erase frame —and give value of function call back to caller....
View Full Document

{[ snackBarMessage ]}

### Page1 / 8

10-07feb22recursion - String swapAdjacent(String s c...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online