PA6_Recurrence

PA6_Recurrence - CSE 11 PA6 ReverseRecurse and Slots due 20...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 11: PA6 ReverseRecurse and Slots due 20 February 2014 Sachi Pitkin 14 February 2014
Image of page 1

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

View Full Document Right Arrow Icon
ReverseRecurse DEMO TIME!
Image of page 2
READ INSTRUCTIONS Create file ReverseRecurse.java Copy over TestReverseRecurse.java How to Start
Image of page 3

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

View Full Document Right Arrow Icon
Reads input from command line o expected # of arguments o accept no more than that many arguments This is very similar to previous assignments. do...while vs while loops o Very useful to know the difference for programs that read input public int[] initArray()
Image of page 4
do { /* code in this block is * guaranteed to execute at * least once, even if the * while condition is not met. */ } while (some condition is true); do...while vs a while loop while (some condition is true) { /* code in this block only is * executed if the condition * is true. Never guaranteed * to run. */ }
Image of page 5

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

View Full Document Right Arrow Icon
Loop over to print array elements Traverse the array using a loop print each element as you access it Be careful not to access index n if there are n elements in the array. o Last index in array is n-1 because we start at 0 Printing “Empty Array” Make sure to first check if there are no elements in the array. public void printArray(int[] array)
Image of page 6
First recursive function: - editing the array in place (swap) Second recursive function: - recursively swapping middle array ReverseRecurse - Two Methods
Image of page 7

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

View Full Document Right Arrow Icon
public void reverse(int[] originalArray, int low, int high) Say you start with input such as: maximum # of elements expected: 5 1 2 3 4 5 1 2 3 4 5
Image of page 8
What’s our base case?
Image of page 9

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

View Full Document Right Arrow Icon
Where there is 0 or 1 element left to recurse on. What’s our base case?
Image of page 10
Say you start with input such as: maximum # of elements expected: 5 1 2 3 4 5 1 2 3 4 5 Goal: Reach a base case where there is only one element or zero elements left to recurse on. public void reverse(int[] originalArray, int low, int high)
Image of page 11

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

View Full Document Right Arrow Icon
public void reverse(int[] originalArray, int low, int high) 1 2 3 4 5 What the original array looked like 1 Middle 5 Values to swap Goal: Reach a base case where there is only one element or zero elements left to recurse on.
Image of page 12
public void reverse(int[] originalArray, int low, int high) 1 2 3 4 5 5 Middle 1 Next, increment and decrement your low/high indices and recurse onto a smaller problem Goal: Reach a base case where there is only one element or zero elements left to recurse on.
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern