Presentation is loading. Please wait.

Presentation is loading. Please wait.

While loop Side-by-side View recursion. int sum(int n) { int total = 0; int i = 1; while (i <= n) { total = total + i; i = i + 1; } return total; } int.

Similar presentations


Presentation on theme: "While loop Side-by-side View recursion. int sum(int n) { int total = 0; int i = 1; while (i <= n) { total = total + i; i = i + 1; } return total; } int."— Presentation transcript:

1 while loop Side-by-side View recursion

2 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 the sum 1 + 2 + 3 +...+ n

3 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 b^n

4 int fibonacci(int n) { int i = 1; int curFib = 0; int nextFib = 1; while (i < n) { int newCur = nextFib; int newNext = curFib + nextFib; curFib = newCur; nextFib = newNext; i = i + 1; } return curFib; } int fibonacci(int n) { if (n == 1) { return 0; } else if (n == 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } Compute the n-th Fibonacci number (0, 1, 1, 2, 3, 5, 8,...)

5 void drawCirclesRow(double x, double y, double radius, int n) { double curX = x; double curY = y; int count = 0; while ( count < n ) { canvas.drawCircle(curX, curY, radius); curX = curX + 2*radius; count = count + 1; } void drawCirclesRow(double x, double y, double radius, int n) { if (n > 0) { canvas.drawCircle(x, y, radius); drawCirclesRow(x+2*radius, y, radius, n-1); }

6 int binarySearch(int[] array, int value) { code from homework 10 } int binarySearch(int[] array, int value, int i, int j) { if (i > j) { return -1; } else { int m = (i + j) / 2; if (array[m] == value) { return m; } else if (array[m] < value) { return binarySearch(array, value, m+1, j); } else // value < array[m] { return binarySearch(array, value, i, m-1); }

7 void drawSierpinski(double x1, double y1, // lower-left vertex double x2, double y2, // lower-right vertex double x3, double y3, // top vertex int depth) { MUCH EASIER TO DO WITH RECURSION } void drawSierpinski(double x1, double y1, // lower-left vertex double x2, double y2, // lower-right vertex double x3, double y3, // top vertex int depth) { if (depth > 0) { // calculate the mid points of the sides double x12 = (x1 + x2) / 2; double y12 = (y1 + y2) / 2; double x13 = (x1 + x3) / 2; double y13 = (y1 + y3) / 2; double x23 = (x2 + x3) / 2; double y23 = (y2 + y3) / 2; // draw base triangle and “remove” middle section canvas.drawTriangle(x1, y1, x2, y2, x3, y3, "black"); canvas.drawTriangle(x12, y12, x23, y23, x13, y13, "white"); canvas.sleep(1); // draw lower-left, lower-right, top portion (in this order) drawSierpinski(x1, y1, x12, y12, x13, y13, depth-1); drawSierpinski(x12, y12, x2, y2, x23, y23, depth-1); drawSierpinski(x13, y13, x23, y23, x3, y3, depth-1); }


Download ppt "While loop Side-by-side View recursion. int sum(int n) { int total = 0; int i = 1; while (i <= n) { total = total + i; i = i + 1; } return total; } int."

Similar presentations


Ads by Google