Review of Previous Lesson

Slides:



Advertisements
Similar presentations
Recursion Recursive Thinking Recursive Programming
Advertisements

Recursion yet another look To recurse is divine, to iterate is human.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
1 Repetition structures Overview while statement for statement do while statement.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Loops More loops Off-by-one errors Infinite loops Nested Loops.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 8: Recursion.
1 Repetition structures Overview while statement for statement do while statement.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
M180: Data Structures & Algorithms in Java
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Chapter 5 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
IB Computer Science Unit 5 – Advanced Topics Recursion.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
Repetition Statements while and do while loops
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Recursion.
Sophomore Scholars Java
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Counted Loops.
Java Programming: Program Design Including Data Structures
Control Statement Examples
Recursion Recursive Thinking Recursive Programming
Recursion Chapter 11.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
The Basics of Recursion
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
class PrintOnetoTen { public static void main(String args[]) {
Basics of Recursion Programming with Recursion
Java Programming: Chapter 9: Recursion Second Edition
Recursion yet another look To recurse is divine, to iterate is human
Review of Previous Lesson
Review of Previous Lesson
Recursion Method calling itself (circular definition)
Last Class We Covered Recursion Stacks Parts of a recursive function:
Review of Previous Lesson
Loops and Iteration CS 21a: Introduction to Computing I
Review of Previous Lesson
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Review of Previous Lesson Thursday, 16/08/2018 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from 0 - 3.

Object Orientated Programming Paradigm (OOP) Thursday, 16/08/2018 Object Orientated Programming Paradigm (OOP) Recursion

Program Implementation Thursday, 16/08/2018 Program Implementation Part of the problem-solving process is the statement of solutions in a precise form that invites review and analysis. The implementation of solutions in the Java programming language reinforces concepts, allows potential solutions to be tested, and encourages discussion of solutions and alternatives. Control Recursion

Thursday, 16/08/2018 Recursion When a method calls itself.

Thursday, 16/08/2018 Loop Recursion int i = 0; while(i < 6){ System.out.println(i); i++; } public static void recurse(int i){ System.out.println(i); recurse(i+1); } Would go on forever like an infinite loop. We need to add in a stopping condition (in recursion this is known as the base case) to tell the code to stop – see next slide. ? Output: Output: 1 2 3 4 5 1 2 3 4 5 … ?

? Loop Recursion Thursday, 16/08/2018 int i = 0; while(i < 6){ System.out.println(i); i++; } public static void recurse(int i){ if(i==6) { //do nothing } else { System.out.println(i); recurse(i+1); Stopping Condition Base Case Stop Note that there must be a base case otherwise infinite recursion occurs. ? Output: Output: 1 2 3 4 5 1 2 3 4 5

Thursday, 16/08/2018 What do you think would happen if we swap the call & println? Recursion public static void recurse(int i){ if(i==6) { //do nothing } else { recurse(i+1); System.out.println(i); Stop ? Output: 5 4 3 2 1

How we recursively calculate the number of digits in an integer? Thursday, 16/08/2018 How we recursively calculate the number of digits in an integer? To code this recursively we will have to start at the number (e.g. 4326) and recursively divide by 10 and call until < 10 (the final digit), so that the recursion will unwind back from the last digit. Base Case is when num < 10 and return 1. Otherwise return 1 + the previous call’s return. ? ?

How we calculate the number of digits in an integer? Thursday, 16/08/2018 public class NoDigitsInAnIntegerExample { public static void main(String args[]){ int num=4326; System.out.println("No of Digits in "+ num + " is: " + findNoDigitsInAnInteger(num)); } // return no of digits in num. public static int findNoDigitsInAnInteger(int num){ if(num < 10) return 1; e.g. 4326 result = 4 return 4 num = 4326 1 + 3 return 3 num = 432 1 + 2 return 2 num = 43 1 + 1 return 1 + findNoDigitsInAnInteger(num / 10); return 1 num = 4

How we calculate the number of digits in an integer? Thursday, 16/08/2018 public class NoDigitsInAnIntegerExample { public static void main(String args[]){ int num=4326; System.out.println ("No of Digits in "+ num + " is: " + findNoDigitsInAnInteger(num)); } // return no of digits in num. public static int findNoDigitsInAnInteger (int num){ if(num < 10) return 1; Repeat of previous slide but with automatic animation. e.g. 4326 result = 4 return 4 num = 4326 1 + 3 return 3 num = 432 1 + 2 return 2 num = 43 1 + 1 return 1 + findNoDigitsInAnInteger(num / 10); return 1 num = 4

How we calculate a factorial iteratively? Thursday, 16/08/2018 How we calculate a factorial iteratively? e.g. 4 4 * 3 * 2 * 1 = 24 Or 1 * 2 * 3 * 4 = 24 ?

Thursday, 16/08/2018 1 * 2 * 3 * 4 * …. ? To code this recursively we will have to start at the number (e.g. 4) and recursively subtract 1 and call until 1, so that the recursion will unwind from 1 to the number. Base Case is when num reaches 1 and return 1. Otherwise return num (e.g. 2, 3, 4) * by the previous call’s return. Note: Unwinding from 1 is always the base case, no matter what the number we require the factorial is, this is not the case if we reversed this to unwind from the number required backwards (e.g. 4 * 3 * 2 * 1) as the base case depends on the number in this case. Therefore, recursive coding of factorial can only unwind from 1. ?

1 * 2 * 3 * 4 * …. Thursday, 16/08/2018 public class FactorialRecursionExample { public static void main(String args[]){ int num=4; System.out.println("Factorial of "+ num + " is: " + findFactorial(num)); } // return factorial of num. public static int findFactorial(int num){ if(num == 1) return 1; return num * findFactorial(num-1); e.g. Factorial 4 result = 24 return 24 num = 4 4 * 6 return 6 num = 3 3 * 2 return 2 num = 2 2 * 1 return num * findFactorial(num-1); return 1 num = 1

1 * 2 * 3 * 4 * …. Thursday, 16/08/2018 public class FactorialRecursionExample { public static void main(String args[]){ int num=4; System.out.println("Factorial of "+ num + " is: " + findFactorial(num)); } // return factorial of num. public static int findFactorial(int num){ if(num == 1) return 1; return num * findFactorial(num-1); Repeat of previous slide but with automatic animation. e.g. Factorial 4 result = 24 return 24 num = 4 4 * 6 return 6 num = 3 3 * 2 return 2 num = 2 2 * 1 return num * findFactorial(num-1); return 1 num = 1

13/04/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).

Triangle Number (Rows) 13/04/2019 Triangle Numbers Rows Triangle Number (Rows) 1 2 3 6 4 10 5 15 21 7 28 ? ? ? ? ? ? ? Write a recursive method which accepts a row number and returns the triangle number (main() in a separate class).

Rewrite programs which use loops so that they use recursion 13/04/2019 Rewrite programs which use loops so that they use recursion Obvious examples would be the independent programs in the “Loops” section (the “for”, “nested for” and “while” presentations).

4/13/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.