ITI 1120 Lab #8 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.

Slides:



Advertisements
Similar presentations
Recursion Michael Ernst UW CSE 140 To seal: moisten flap, fold over, and seal.
Advertisements

CS1010 Programming Methodology
ACM Workshop Number Theory.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion Michael Ernst CSE 190p University of Washington To seal: moisten flap, fold over, and seal.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC2110 Discrete Mathematics Tutorial 5 GCD and Modular Arithmetic
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Elementary Number Theory and Methods of Proof. Basic Definitions An integer n is an even number if there exists an integer k such that n = 2k. An integer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Inverses and GCDs Supplementary Notes Prepared by Raymond Wong
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
CS1101: Programming Methodology Aaron Tan.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Divisibility October 8, Divisibility If a and b are integers and a  0, then the statement that a divides b means that there is an integer c such.
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
COMP 170 L2 Page 1 L05: Inverses and GCDs l Objective: n When does have an inverse? n How to compute the inverse? n Need: Greatest common dividers (GCDs)
MATH 224 – Discrete Mathematics
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Romelia Plesa, Alan Williams, Sylvia Boyd, Daniel Amyot, Diana Inkpen, Gilbert Arbez, Mohamad Eid ITI 1120 Lab #3.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Conditions in Java. First…Boolean Operators A boolean data type is always true or false. Boolean operators always return true or false For example: (x.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CS404 Design and Analysis of Algorithms BBy DDr. M V S Peri Sastry BB.E, PhD(BITS-Pilani)
1 Romelia Plesa, Alan Williams, Sylvia Boyd, Daniel Amyot, Diana Inkpen, Gilbert Arbez, Mohamad Eid ITI 1120 Lab #3.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Recitation 8 Programming for Engineers in Python.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
AF2. Turn off your phones Primes, gcd, some examples, reading.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
Chapter 6 More Conditionals and Loops
Computer Science 101 While Statement.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
The Hanoi Tower Problem
Conditionals.
Application: Algorithms
Application: Algorithms
Question 1a) What is printed by the following Java program? int s;
Lecture 20 – Practice Exercises 4
Presentation transcript:

ITI 1120 Lab #8 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams

Lab 8 Agenda Recursion –Examples of recursive algorithms –Recursive Java methods

Recursion Practice problem #1 Write a recursive algorithm for counting the number of digits in a non-negative integer, N

Practice problem #1- solution GIVENS: N INTERMEDIATE: RestOfDigits RESULT: Count (the number of digits in N) HEADER:Count  NumberOfDigits(N)

Practice problem #1 – solution – cont’d BODY: RestOfDigits = N / 10 RestOfDigits = 0 ? true Count  1 false Count  NumberOfDigits(RestOfDigits) Count  Count + 1

Trace for N = 254 Line NRestOfDigitsCount Initial values 254?? (1) RestOfDigits = N / (2) RestOfDigits = 0 ? false (3) CALL Count  NumberOfDigits(RestOfDigits) (4) Count  Count + 1

Trace, page 2 Line NRestOfDigitsCount Initial values 25?? (1) RestOfDigits = N / 10 2 (2) RestOfDigits = 0 ? false (3) CALL Count  NumberOfDigits(RestOfDigits) (4) Count  Count + 1 Count  NumberOfDigits(RestOfDigits) Count  NumberOfDigits(N) 25

Trace, page 3 Line NRestOfDigitsCount Initial values 2?? (1) RestOfDigits = N / 10 0 (2) RestOfDigits = 0 ? true (5) Count  1 1 Count  NumberOfDigits(RestOfDigits) Count  NumberOfDigits(N) 21

Trace, page 2 Line NRestOfDigitsCount Initial values 25?? (1) RestOfDigits = N / 10 2 (2) RestOfDigits = 0 ? false (3) CALL Count  NumberOfDigits(RestOfDigits) 1 (4) Count  Count Count  NumberOfDigits(RestOfDigits) Count  NumberOfDigits(N) 252

Trace, page 1 Line NRestOfDigitsCount Initial values 254?? (1) RestOfDigits = N / (2) RestOfDigits = 0 ? false (3) CALL Count  NumberOfDigits(RestOfDigits) 2 (4) Count  Count + 1 3

Implement this algorithm in Java

Let’s see how the code runs In your recursive method: Put System.out.println() statements at the following locations (print the actual value of n ): –Immediately after local variable declarations 1: Entering method with n = 254 –Just before recursive method call: 2: Recursive call from n = 254 –Just after recursive method call: 3: Returned from recursion with n = 254 –Just before return statement 4: Returning from method with n = 254, count = 3 –In the base case 5: Base case with n = 2

Practice problem #2 Write a recursive algorithm to test if all the characters in positions 0...N-1 of an array, A, of characters are digits.

Practice problem #2 - solution GIVENS: A (an array of characters) N(test up to this position in array) RESULT: AllDigits (Boolean, true if all characters in position 0..N are digits) HEADER: AllDigits  CheckDigits(A,N)

Practice problem #2 – solution – cont’d BODY: A[N-1] ≥ ′0′ AND A[N-1]  ′9′ ? true N = 1 ? true AllDigits  True false AllDigits  CheckDigits(A, N-1) false AllDigits  False

Practice problem #2 –– cont’d Translate the algorithm into a Java method

Practice problem #2 – solution – cont’d

Practice problem #3 Write a recursive algorithm to test if a given array is in sorted order.

Practice problem #3 - solution GIVENS: A(an array of integers) N(the size of the array) RESULT: Sorted(Boolean: true if the array is sorted) HEADER: Sorted  CheckSorted(A,N)

Practice problem #3 – solution – cont’d BODY: A[N–2] < A[N–1] ? true N = 1 ? true Sorted  True false Sorted  CheckSorted(A, N-1) false Sorted  False

Practice problem #4 Write a recursive algorithm to create an array containing the values 0 to N-1 Hint: –Sometimes you need 2 algorithms: The first is a “starter” algorithm that does some setup actions, and then starts off the recursion by calling the second algorithm The second is a recursive algorithm that does most of the work.

Practice problem #4 - solution GIVENS: N(the size of the array) RESULT: A(the array) HEADER: A  CreateArray(N) BODY: A  MakeNewArray(N) FillArray(A, N – 1) FillArray is the recursive algorithm

Practice problem #4 – solution – cont’d Algorithm FillArray GIVENS: A(an array) N(the largest position in the array to fill) MODIFIEDS: A RESULT: (none) HEADER: FillArray(A, N)

Practice problem #4 – solution – cont’d BODY: N = 0 ? true  false FillArray(A, N-1) A[N]  N

Practice Problem #5: Euclid’s algorithm The greatest common divisor (GCD) of two positive integers is the largest integer that divides both values with remainders of 0. Euclid’s algorithm for finding the greatest common divisor is as follows: gcd(a, b) is … bif a ≥ b and a mod b is 0 gcd(b, a) if a < b gcd(b, a mod b)otherwise Write a recursive algorithm that takes two integers A and B and returns their greatest common divisor. You may assume that A and B are integers greater than or equal to 1.

What is the base case? FindGCD(A, B) is … Bif A ≥ B and A MOD B is 0 FindGCD(B, A) if A < B FindGCD(B, A MOD B)otherwise Question: will this algorithm always reach the base case? –Note that A MOD B is at most B – 1.

Euclid’s Algorithm GIVENS: A, B (Two integers > 0) RESULT: GCD(greatest common divisor of A and B) HEADER: GCD  FindGCD(A, B)

Euclid’s Algorithm BODY: A MOD B = 0 ? true GCD  B false GCD  FindGCD(B, A MOD B) A ≥ B ? GCD  FindGCD(B, A) true false

Euclid’s Algorithm in Java

Test this method Create a class Euclid that includes the method findGCD, and also a main( ) method that will ask the user to enter two values and print their GCD. Try the following: a = 1234, b = 4321 a = 8192, b = 192