CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation

Slides:



Advertisements
Similar presentations
New Mexico Computer Science For All
Advertisements

Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Identify the Appropriate Method for Handling Repetition
Recursion CSE 2320 – Algorithms and Data Structures
Recursion Topic 5.
Algorithms and Problem Solving
Topic: Recursion – Part 2
Topic: Functions – Part 1
Introduction to Recursion
Lecture 1 Introduction Richard Gesick.
RECURSION.
Methods Chapter 6.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
10.3 Details of Recursion.
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
Topic: Functions – Part 2
JavaScript: Functions
Recursion CSE 2320 – Algorithms and Data Structures
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
University of Washington Computer Programming I
Functions.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 5 - Functions Outline 5.1 Introduction
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Recursion Chapter 11.
Chapter 8: Recursion Java Software Solutions
Functions Recursion CSCI 230
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Chapter 11 Recursion.
Algorithms and Problem Solving
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion Taken from notes by Dr. Neil Moore
Introduction to Computer Science
The structure of programming
Programming Fundamentals Lecture #7 Functions
Thinking procedurally
Java Software Solutions Foundations of Program Design Sixth Edition
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 4 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Presentation transcript:

CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation Python – More Turtle, Functions and Introducing Recursion

How about this! Problem statement: Write a program that draws a chocolate chip cookie with our Turtle

But what if I want many cookies? Solution 1 Could I use a for loop?

But what if I want many cookies? Solution 2: Could I copy and modify the code many times, each instance drawing one cookie? Hum… This solution would lead to a lot of repeating code, which is not a good idea! See GPS! Why is that?

But what if I want many cookies? Solution 3: Best solution would be to use a __________________

But what if I want many cookies? But our function draws many cookies in the same spot! We have not solve our problem! Solution 4: So, the ultimate solution would be to design our function such that it takes __________________ !

Function - Local Variables result is a local, temporary variable. It is destroyed once the function call is complete. Local variables cannot be used outside of their “scope”, i.e., the function in which they are defined Parameters are also local variables

Why creating functions? Functions make our program easier to … Implement and test -> Incremental development Dividing a long program into functions allows us to implement, test and debug the parts one at a time and then assemble them into a working program Read Encapsulate code fragment that has one specific task in one location, i.e., a “module” (function) and give this location a descriptive name Modify If we need to make a change to our program, we know “where to go” to find the code fragment we need to change Reuse Once we write, test and debug a function, we can reuse it in other programs that need this particular functionality No more repeated code Functions can make a program smaller by eliminating repeated code - Repeated code is very error-prone

Turtle Animation https://repl.it/repls/AvariciousWeepyFilename

Functions that call themselves Recursion Functions that call themselves

Recursion in the real world Russian dolls

Recursion in the real world Searching for the word “guffaw” in a dictionary Source: http://www.eslstation.net/ESL310L/310L_dict.htm

Recursion in the real world Droste Effect The picture is defined by the picture itself.

Recursion in the mathematical world Multiply two numbers Compute factorials Fractals The Sierpinski triangle A confined recursion of triangles that form a fractal https://en.wikipedia.org/wiki/Recursion

Recursion - Definition Recursion occurs when an object or a process is defined in terms of itself (or a version of itself)  In mathematics and computer science, a kind of objects or processes exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case Adapted from http://en.wikipedia.org/wiki/Recursion

Recursion in the software world So far, when solving problems, we have achieved repetition (i.e., repeating statements in our code) by using iterative statements -> loops By putting statements we wanted to repeat in a loop

Recursive functions Another way of achieving repetition (i.e., repeating statements in our code) is by putting statements we want to repeat in a function and calling the function itself a certain number of times Directly: Indirectly: functionA # recursive call functionA(…) function1 # recursive call function2(…) function 2 # recursive call function1(…)

Let’s give it a try! Problem statement: Let’s draw a tree recursively using our turtle

Turtle Examples Here are some resources https://michael0x2a.com/blog/turtle-examples (squares) https://trinket.io/python/82fe4d3bd0 (interactive) https://www.turtle.ox.ac.uk/downloads/docs/Turtle_Python_Exercises_1-12.pdf http://openbookproject.net/thinkcs/python/english3e/recursion.html http://www.101computing.net/astronomy-challenge/

I choose a for loop when I know how many times I need to iterate Review If you want an action to repeat itself until a certain condition is met When to choose a while loop? When to choose a for loop? What does it mean when variables in functions have local scope? I choose a for loop when I know how many times I need to iterate

Next Lecture We shall practice designing and implementing functions – all sorts of functions So, bring a laptop!!!