RecursionTrace-Practice

RecursionTrace-Practice - 15-121: Introduction to Data...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121: Introduction to Data Structures Exercise 09/11/2009 Relevant Reading: • Chapter 7 in the Lewis and Chase book. • Section 9.1 of the online text. Topic: Recursion. Recursion Trace Practice Fall 2009 Due: Mon, Sept 14 10:30am Assignment: (worth 0 exercise points - this is for practice only) Below are 4 methods. Figure out what they do. Try some sample inputs. Then, put them into a program and run it. See if you were right. If not, come ask questions. Handin: • Nothing to hand in. //************** // Assumes: n is >= 0 void DoIt (int n) { if ( n != 0) { System.out.println(n); DoIt (n - 1 ); } } //************************* // Assumes: n is >= 0 void DoIt2 (int n) { if ( n != 0) { DoIt2 (n - 1 ); System.out.println(n); } } //********************* // Assumes: n is >= 0 void DoIt3 (int n) { if (n != 0) { if (n % 2 == 0) { System.out.print(n); DoIt3(n / 2); } else { System.out.println(n); DoIt3(n - 1); } } } //****************** // For practice assume an initial call such as: void DoIt4(intA, int left, int right) { if (left == right) A[left] ++; else { int mid = (left + right) /2; DoIt4(A,left,mid); DoIt4(A,mid+1,right); } } DoIt4(A,0,A.length -1); ...
View Full Document

Page1 / 2

RecursionTrace-Practice - 15-121: Introduction to Data...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online