Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Programming II Recursion - Lecture Slides | CS 112, Study notes of Computer Science

Material Type: Notes; Class: Intro to Computer Science II; Subject: Computer Science; University: University of San Francisco (CA); Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-9p5wt
koofers-user-9p5wt 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intro to Programming II
Recursion
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science University of San Francisco p.1/??
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Introduction to Programming II Recursion - Lecture Slides | CS 112 and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II

Recursion

Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science — University of San Francisco – p.1/

Recursion

Recursion is a fundamental problem-solving technique Involves decomposing a problem into: A base case that can be solved directly A recursive step that indicates how to handle morecomplex cases. A common recursive example is factorial: long factorial(int input) if (input ==

return 1; else return input

factorial(input

1); Department of Computer Science — University of San Francisco – p.2/

Infinite recursion

A common error in recursion is forgetting the base case. This can lead to infinite recursion double factorial(int input) return input

factorial(input

1); This eill eventually have a stack overflow. Department of Computer Science — University of San Francisco – p.4/

Exercise: Fibonacci numbers

The Fibonacci numbers are defined as follows: f

f

f

n

f

n

f

n

The first few numbers are 1,1,2,3,5,8,13,21,... Write a class called Fibonacci. It should have a methodcalled getFib(int n) that calculates the nth Fibonaccinumber, plus a main method to test it. Department of Computer Science — University of San Francisco – p.5/

Recursion: Traversing a Maze

Solving a maze is the sort of problem that requirestrial-and-error. When you’re stuck, back up and undo the last thing youdid. This sort of approach works well with recursion. We’ll represent the maze as a two-dimensional array. 1 = clear, 0 = blocked. Start in the upper left, get to the lower right. Department of Computer Science — University of San Francisco – p.7/

Exercise: Solving a maze

Change the rules so that you always try to go left, thenright, then up, then down. Write a Maze constructor that takes two arguments: rowand col and generates a random maze of that size. Department of Computer Science — University of San Francisco – p.8/

Recursion in graphics: Exercise

Add your own pictures to the applet. Change the applet so that the recursive part of the pictureis in the lower right. Department of Computer Science — University of San Francisco – p.10/

Fractals

We can also use recursion to draw fractals Example: Koch snowflake Rule: Each line segment is replaced by a “wedge” withsides that are the same length as the replaced piece. As we increase the depth, it begins to look like asnowflake. Department of Computer Science — University of San Francisco – p.11/