10-27 Class Notes CS 107

# 10-27 Class Notes CS 107 - 10-27 Class Notes CS 107...

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

Announcements: - Solution to program #4 posted online - Remember in-class portion of midterm #2 in class on Monday. Questions? Last Time: Recursion intuition and trace mechanisms of calculating factorial Today: Review of solution to program #4 More recursion traces. Recursion example: the maze problem, traversing an array Examples continued from last class…. /** * Illustrate simple recursion examples. * */ import java.util.Scanner; // for keyboard input reverseText(); System.out.println(" \n"); public class Recursion1 { Scanner keyboard = new Scanner( System.in); // Run all examples public void doAllProblems() { printReverse( 2468); System.out.println(" \n"); recursivePrintReverse( 1357); System.out.println(" \n"); System.out.println("f1(8,3) = " + f1(8,3) ); System.out.println("f1(3,8) = " + f1(3,8) ); System.out.println("f2(2,4) = " + f2(2,4) ); System.out.println("f3(256) = " + f3(256) ); System.out.println("f4( 2,5) = " + f4( 2,5) ); } // non-recursively display the digits of a number in reverse order public void printReverse( int number) { while ( number > 0) { System.out.print( number % 10); number = number / 10; } 10-27 Class Notes CS 107 Wednesday, October 27, 2010 12:39 PM 107 Fall 2010 Page 1

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

View Full Document
while ( number > 0) { System.out.print( number % 10); number = number / 10; } System.out.println(); } // recursively display the digits of a number in reverse order public void recursivePrintReverse(int x) { // put your code here System.out.print( x%10); x = x / 10; if ( x != 0) { recursivePrintReverse( x); } System.out.println("Great!" + x); } // enter some text to be reversed, followed by '\$' public void reverseText() { System.out.print("Enter a character: "); // prompt String inputline = keyboard.nextLine(); // read input from keyboard char c = inputline.charAt(0); // get the first character in the string if (c!= '\$') { reverseText(); System.out.print( c); } } public int f1( int num1, int num2) { if (num1 > num2) return 0; else if (num2 == (num1 + 1)) return 1; else return f1( num1 + 1, num2 -1) + 2; } public double f2( double x, int n) { if (n == 0) return 0; else return x + f2( x, n-1); } // try this one with input of 256 int f3( int n) { if (n < 0) return f3(-n); else if (n<10) return n; else return f3( n/10); } // Call this with f4( 2,5) double f4( double x, int n) { if (n == 0) 107 Fall 2010 Page 2
// Call this with f4( 2,5) double f4( double x, int n) { if (n == 0) return 1.0; else if (n > 0) return f4( x, n - 1) * x; else { System.out.println("Error !"); return 0.0; } } }//end Class Recursion1 Pasted from < http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/Recursion/RecursionExamples/Recursion1.java > And now, a more detailed example combining arrays with recursion, to traverse a maze: /** * Class Maze1 illustrates navigating a maze using a 2-d array, * where the solution can be found simply by always going down * if possible, otherwise going to the right. * * Running the program gives:

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.

## This note was uploaded on 02/10/2012 for the course COP 2271 taught by Professor Reed during the Fall '10 term at University of Central Florida.

### Page1 / 8

10-27 Class Notes CS 107 - 10-27 Class Notes CS 107...

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

View Full Document
Ask a homework question - tutors are online