CS 171 & 171L Introduction to Computer Science I & Lab Fall 2020

## Lab 12

### Goals and Objectives

The main goal this lab is for you to get a more practice with recursion.

You will complete several functions that use some basic recursive techniques.

 ``` // Your name(s) here void setup() { testing(); } void draw() { } void testing() { println("sum: " + factorial(4) + " should equal 24"); println("sum: " + sum(10) + " should equal 55"); println("Why does this print the numbers forward and backward?\n"); upAndDown(5); println("Fibo "+12+": "+fibo(12) + " should equal 144"); println("countSpaces: " + countSpaces("This is a test.") + " should equal 3"); println("digits: " + digits(987654) + " should equal 6"); println("binary: " + toBinary(13) + " should equal 1101"); println("binary: " + toBinary(12022008) + " should equal 101101110111000011111000"); println("multiply: " + multiply(5, 7) + " should equal 35"); println("min: " + min(new int [ ] {1, 23, 54, 653, 12, -2, 4}, 0) + " should equal -2"); println("reverse: " + reverse("deliver") + " should be reviled"); println("Ackermann: " + Ackermann(3, 3) + " should equal 61"); // Why does Ackermann(5,5) cause a stackoverflow error? // Discuss this with your lab partner. //System.out.println(Ackermann(5,5)); println("\nOnly for the brave!"); System.out.println("All 24 permutations of abcd: abcd, abdc, acbd, acdb, adbc, adcb, ..., dcba"); String [] perms = permutations("abcd"); if (perms != null) { for (int i = 0; i < perms.length; i++) { System.out.print(perms[i]); if (i < perms.length-1) System.out.print(", "); else System.out.println(); } } } // Complete each of the following methods to compute the // values as indicated using a recursive algorithm // Do not use a loop unless specifically indicated // compute n! // Example: // input: 4 // output: 24 // WORKS AS IS public static int factorial(int n) { if (n==0) { return 1; } else { return n*factorial(n-1); } } // Sum the first n positive integers // Example: // input: 10 // output: 55 // See class notes/code/video public static int sum(int n) { return 0; } // Discuss this with your lab partner // See class notes/code/video public static int upAndDown(int n) { System.out.println("Down: " + n); if (n > 0) upAndDown(n-1); System.out.println("Up: " + n); return 0; } // Compute the ith Fibonacci number // F(0) = 1 // F(1) = 1 // F(2) = 2 // ... // F(n) = F(n-1) + F(n) // Note: Computing Fibonacci numbers this way is // completely impractical as you will see // Discuss with your lab partner // See class notes/code/video public static long fibo(int n) { return 0; } // Return the number of spaces in a string // Example: // input: "This is a test." // output: 3 // Hint: Use substring and index methods of String // See class notes/code/video on length function public static int countSpaces(String s) { return 0; } // Output the number of digits in a given number // Example: // input: 0 // output: 1 // input: 987654 // output 6 // Hint: Use / operator public static int digits(int n) { return 0; } // Convert the integer value to a String representing its binary value // Example: // input: 10 // output 1010 // input: 12022008 // output: 101101110111000011111000 // Hint: Use / and % operators // Note: 10 is even, so rightmost bit is 0 // divide 10 by 2, resulting is 5 // 5 is odd, so next bit is 1 // divide 5 by 2, resulting in 2 // 2 is even, so next bit is 0 // divide 2 by 2, resulting in 1 // 1 is odd, so next bit is 1 // 10 base ten is 1010 in binary (base two) String toBinary(int n) { return ""; } // Multiply the input values using only addition and no loops. // Example: // input: 5, 7 // output: 35 // note m*n = m*(n-1+1) = m*((n-1)+1) = m*(n-1) + m public static int multiply(int m, int n) { return 0; } // Return minimal element in array a, starting at position p) // Example: // input: {1,23,54,653,12,-2,4} // output: -2 // input: {12345} // output: 12345 // Hint: the minimum element is either at p OR // it is the minimum element in the list starting at index p+1 public static int min(int[ ] a, int p) { return 0; } // Reverses String s // Example: // input: "pi" // output: "ip" // input: "computer" // output: "retupmoc" // input: "oozy rat in a sanitary zoo" // output: "ooz yratinas a ni tar yzoo" // Hint: Reverse what you have been doing public static String reverse(String s) { return ""; } // Ackermann's Function // A(m,n) = n+1 if m=0 // A(m,n) = A(m-1,1) if n=0 // A(m,n) = A(m-1,A(m,n-1)) otherwise // Note: This is a really C-R-A-Z-Y function! // See http://mathworld.wolfram.com/AckermannFunction.html public static int Ackermann(int m, int n) { return 0; } // Determine all the permutations of a given input string // Hint: Use a for loop and the substring method. // Example: // input: "abc" // output: ["abc", "acb", "bac", "bca", "cab", "cba"] // Note: You will need a for loop and an array // Note: There are n! permutations of n elements. // Generally recursive, but you can additionally use one for loop public static String[] permutations(String s) { String[] a; a = null; return a; } ```