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.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Factorial Recursion stack Binary Search Towers of Hanoi
Programming Recursion.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
While loop Side-by-side View recursion. int sum(int n) { int total = 0; int i = 1; while (i
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Recursion CITS1001.
1 Library Methods and Recursion Instructor: Mainak Chaudhuri
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion1. 2 What kind of problem is “recursive”? same Recursive problems are those problems where in order to solve the original problem you have to.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion Chapter 5.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Building Java Programs Chapter 13 Searching reading: 13.3.
Recursion. Functions – reminder A function can call other functions. Return causes the execution of the function to terminate and returns a value to the.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5.
Review Transformations – Scale – Translate – Rotate Combining Transformations – Transformations are cumulative – Rotating about the center of an object.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Dynamic Programming. What is dynamic programming? Break problem into subproblems Work backwards Can use ‘recursion’ ‘Programming’ - a mathematical term.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Today’s Material Recursive (Divide & Conquer) Algorithms Design
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
CSC 205 Programming II Lecture 9 More on Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Do Now. Section 8.2 Angles in Polygons Polygon Interior Angles Theorem The sum of the measures of the interior angles of a convex polygon with n sides.
Starting Out with C++, 3 rd Edition Chapter 19 Recursion.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CPS Today’s topics Programming Recursion Invariants Reading Great Ideas, p Brookshear, Section Upcoming Copyrights, patents, and.
Recursive Algorithms and Backtracking
Looping Examples I.
RECURSION.
Computer Science 4 Mr. Gerb
Sum of natural numbers class SumOfNaturalNumbers {
CS 211 Object Oriented Programming
Functions Dr. Sajib Datta
Introduction to Computer Science - Alice
Recursion Chapter 10.
Recursive functions.
Building Java Programs
Recursion Data Structures.
22C:21 Discrete Structures
Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6,
Recursive example 1 double power(double x, int n) // post: returns x^n
Presentation transcript:

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 sum(int n) { if (n == 1) { return 1; } else { return n + sum(n-1); } Compute the sum 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 b^n

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,...)

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); }

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); }

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); }