Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
Introduction to Computing Science and Programming I
Java Planning our Programs Flowcharts Arithmetic Operators.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Computer Science 1620 Loops.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Lattice Multiplication is an alternative way of doing higher level multiplication.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion Pepper. Recursion Definition English –procedure that can repeat a version of itself indefinitely – such as mirrors looking at each other. Math.
IB Computer Science Unit 5 – Advanced Topics Recursion.
Basic Control Structures
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Counting Loops.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
3434 Fractions By Mr. Walker. What is a Fraction? A fraction is just a smaller part of something else. If you have one piece of the pizza, you are only.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
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.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Counting up to Subtract Addition and Subtraction are related! They are called inverse operations. Think back to your fact families = = 4.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
CSC111 Quick Revision.
Introduction to Recursion
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.
Counted Loops.
Arrays & Functions Lesson xx
Building Java Programs
Building Java Programs
Decision statements. - They can use logic to arrive at desired results
While Loop Design ENGI 1020 Fall 2018.
Building Java Programs
Repetition Control Structure
Building Java Programs
Recursion Pepper.
slides created by Marty Stepp and Alyssa Harding
Recursion based on slides by Alyssa Harding
Loops.
The structure of programming
Building Java Programs
Recursion Pepper.
Building Java Programs
Building Java Programs
Building Java Programs
CSC Data Structures, Fall, 2008
Presentation transcript:

Recursion Pepper

Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count -- Ouput – total of all those numbers added up Logic: If the number I am counting is 0, just return 0. If the number I am counting is anything else, return the number I have + the total of all the rest (the number I have – 1) Requires: Trust the function will do its job A stopper A way to reach the stopper (decrement or increment) Code the addUp method and call it to addUp(10)

Start by trusting it will work Create a method that assumes it will do the job it says it will: Return the number you have + everything else that needed to be done public static int addUp(int numberToCount) { // something else will have to be put here return numberToCount + addUp(numberToCount-1); }

Then, write the stopping code When should it stop calling itself? What should it do in that last case? public static int addUp(int numberToCount) { if (numberToCount == 1) // stop when you are down to #1 { // last called case work return numberToCount;} else { // normal work return numberToCount + addUp(numberToCount-1); }

addUp Solution public class AddUp { public static void main() { System.out.println(addUp(10)); } public static int addUp(int numberToCount) { if (numberToCount == 1) { return numberToCount;} else { return numberToCount + addUp(numberToCount-1); }

Alternative: For public static int addUp(int numberToCount) { int sum = 0; for (int count =1; count <= numberToCount;count++) { sum = sum + count; } return sum; }

Create a string of stars YOU TRY: starString routine -- Input – number of stars needed -- Ouput – String of that many stars Logic: If the number I am counting is 1, just return 1 star. If the number I am counting is anything else, return one star + all the rest of the stars (the number I have – 1) Requires: Trust the function will do its job A stopper A way to reach the stopper (decrement or increment) Code the starString method and call it to create starString(10)

String of Stars public static String starString (int numberStars) { if (numberStars == 0) { return(""); } else { return "*" + starString(numberStars-1); }

Work with shapes Divide a rectangle up 4 levels deep –First print 1 big square –Then divide that one square –Test fully –Then create another method to printInsideSquares with input: level, paintbrush, starting x,y and size. Stop at level 1 – just print the square with no division At every other level, print a big square and printInsideSquares for the smaller squares

Work with Shapes First hint: Here is how to draw 1 level of 4 squares inside 1 square: public static void printOneInsideSquares(int level, Graphics g, int x, int y, int size) { g.drawRect(x,y,size,size); g.drawRect( x,y,size/2,size/2); // top left g.drawRect( x+(size/2), y,size/2,size/2); // top right g.drawRect( x,y+size/2,size/2,size/2); // bottom left g.drawRect( x+size/2,y+size/2,size/2,size/2); // bottom right } Now – add the level logic: stopper: If level == 1, just draw the main rectangle do a job and call itself to do the rest: If level higher, draw main rectangle and printInsideSquares of the small squares at the next level.