Sidebyside View while loop recursion Compute the sum
Side-by-side View while loop recursion
Compute the sum 1 + 2 + 3 +. . . + n int sum(int n) { int total = 0; int i = 1; while (i <= n) { total = total + i; i = i + 1; } return total; } int sum(int n) { if (n == 1) { return 1; } else { return n + sum(n-1); } }
Compute b^n double power(double b, int n) { double result = 1; int i = 0; while (i < n) { result = result * b; i = i + 1; } return result; } double power(double b, int n) { if (n == 0) { return 1; } else { return b*power(b, n-1); } }
Compute the n-th Fibonacci number (0, 1, 1, 2, 3, 5, 8, . . . ) int fibonacci(int n) { int i = 1; int cur. Fib = 0; int next. Fib = 1; while (i < n) { int new. Cur = next. Fib; int new. Next = cur. Fib + next. Fib; cur. Fib = new. Cur; next. Fib = new. Next; i = i + 1; } return cur. Fib; } int fibonacci(int n) { if (n == 1) { return 0; } else if (n == 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } }
void draw. Circles. Row(double x, double y, double radius, int n) { double cur. X = x; double cur. Y = y; int count = 0; while ( count < n ) { canvas. draw. Circle(cur. X, cur. Y, radius); cur. X = cur. X + 2*radius; count = count + 1; } } void draw. Circles. Row(double x, double y, double radius, int n) { if (n > 0) { canvas. draw. Circle(x, y, radius); draw. Circles. Row(x+2*radius, y, radius, n-1); } }
int binary. Search(int[] array, int value) { code from homework 10 }
int binary. Search(int[] array, int value) { int binary. Search(int[] array, int value, int i, int j) { if (i > j) { return -1; } code from homework 10 else { int m = (i + j) / 2; if (array[m] == value) { return m; } else if (array[m] < value) { return binary. Search(array, value, m+1, j); } else // value < array[m] { return binary. Search(array, value, i, m-1); } }
void draw. Sierpinski(double x 1, double y 1, double x 2, double y 2, double x 3, double y 3, int depth) { // lower-left vertex // lower-right vertex // top vertex MUCH EASIER TO DO WITH RECURSION (NO WHILE LOOP VERSION in CS 111) }
void draw. Sierpinski(double x 1, double y 1, // // lower-left vertex void draw. Sierpinski(double lower-left vertex double x 2, double y 2, // lower-right vertex double x 3, double y 3, // // toptop vertex double vertex intint depth) { { if (depth > 0) { // calculate the mid points of the sides double x 12 = (x 1 + x 2) / 2; double y 12 = (y 1 + y 2) / 2; double x 13 = (x 1 + x 3) / 2; double y 13 = (y 1 + y 3) / 2; double EASIER x 23 = (x 2 TO + x 3) MUCH DO/ 2; WITH RECURSION double y 23 = (y 2 + y 3) / 2; (NO WHILE LOOP VERSION in CS 111) // draw base triangle and “remove” middle section canvas. draw. Triangle(x 1, y 1, x 2, y 2, x 3, y 3, "black"); canvas. draw. Triangle(x 12, y 12, x 23, y 23, x 13, y 13, "white"); canvas. sleep(1); // draw lower-left, lower-right, top portion (in this order) draw. Sierpinski(x 1, y 1, x 12, y 12, x 13, y 13, depth-1); draw. Sierpinski(x 12, y 12, x 2, y 2, x 23, y 23, depth-1); draw. Sierpinski(x 13, y 13, x 23, y 23, x 3, y 3, depth-1); } } }
- Slides: 9