CS 161 Intro to CS I. Finish Pointers/Start Recursion

Similar documents
CS103L SPRING 2017 UNIT 8: RECURSION

CS 310 Advanced Data Structures and Algorithms

Programming Abstractions

Algorithm. An algorithm is a computational process for solving a problem. computational: a computer must be able to perform the steps of the process

Programming Abstractions

Function with default arguments

NCS 301 DATA STRUCTURE USING C

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

1 Dynamic Memory continued: Memory Leaks

Recursion. Chapter 11. Chapter 11 1

Inductive Definition to Recursive Function

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

Recursion. Jordi Cortadella Department of Computer Science

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

EE 368. Weeks 4 (Notes)

1.7 Recursion. Department of CSE

Exercise(s) Solution(s) to the exercise(s)

Recursion. CSCI 112: Programming in C

EXAMGOOD QUESTION & ANSWER. Accurate study guides High passing rate! Exam Good provides update free of charge in one year!

Programming Abstractions

Help File on C++ and Programming at CSUSM by Rika Yoshii

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

CSci 1113 Midterm 2. Name: Student ID:

CSE 230 Intermediate Programming in C and C++ Recursion

Programming with Recursion. What Is Recursion?

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CS 101: Computer Programming and Utilization

Programming II (CS300)

Computer Programming

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

CS240: Programming in C

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Programming II (CS300)

Chapter 15: Recursion

recursive algorithms 1

COMP2611: Computer Organization MIPS function and recursion

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

FORM 2 (Please put your name and form # on the scantron!!!!)

Programming II (CS300)

What will the output of the following code snippet be? Why? What is a safer alternate implementation?

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

CS103 Unit 8. Recursion. Mark Redekopp

Reduction & Recursion Overview

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Lecture 8 Recursion. The Mirrors

CS103 Unit 8. Recursion. Mark Redekopp

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

EK131 E5 Introduction to Engineering

Discussion 2C Notes (Week 5, February 4) TA: Brian Choi Section Webpage:

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

CSE 214 Computer Science II Recursion

Computer Programming

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Recursive Problem Solving

G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

We cover recursion in 150. Why do it again in 151?

CSI 32. Lecture 15. Chapter 11 Recursion Functional Recursion 11.4 Binary Search

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

Recursion. Contents. Steven Zeil. November 25, Recursion 2. 2 Example: Compressing a Picture 4. 3 Example: Calculator 5

Section 1: True / False (2 points each, 30 pts total)

Unit 10: Data Structures CS 101, Fall 2018

Recursion. Example R1

Functions and Recursion

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Data Structures And Algorithms

CS200: Recursion and induction (recap from cs161)

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Functions and Recursion

RECURSION. Problem Solving with Computers-II 6

10/9/17. Using recursion to define objects. CS 220: Discrete Structures and their Applications

Pointers II. Class 31

Chapter 18 Recursion. Motivations

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

CS31 Discussion 1E Spring 17 : week 05

CS 161 Exam II Winter 2018 FORM 1

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1

Implementing Subprograms

Why use recursion? Some problems are more easily expressed in a recursive definition

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

CMPSCI 187: Programming With Data Structures. Lecture #16: Thinking About Recursion David Mix Barrington 12 October 2012

Transcription:

CS 161 Intro to CS I Finish Pointers/Start Recursion 1

In-class Exercise #3 Understanding Pointers Create a pointer to a double, i.e. double *d; and three doubles d1, d2, and, d3 that get the values 7.8, 10.0, and.009. Now, set the pointer, d, to point to each double variable, d1, d2, and d3, printing the address and contents of each double variable along the way. 2

duble*d has garbage, until set 3

In-class Exercise #3 Understanding Pointers What if you made a pointer that points to a pointer to a double, i.e. double **dp? Now, set dpto point to d, and use dpto print the address and contents of each double variable!!! 4

Demo 5

Recursion What is it? Function that calls itself 1 or more times (directly or indirectly) Has 1 or more base case for stopping Inductive reasoning: general case must eventually be reduced to a base case 6

Example: Drawing Rectangles Iterative Solution: void draw_rect(int i) { for( ; i> 0; i--){ cout<< "****** << endl; cout<< "* * << endl; cout<< "****** << endl<< endl; } } 7

Example: Drawing Rectangles Recursive Solution void draw_rect(int i) { if(i>0){ //Base case draw_rect(--i); //Recursive call cout<< "****** << endl; cout<< "* * << endl; cout<< "****** << endl<< endl; } } 8

What is different when we call after? Recursive Solution void draw_rect(int i) { if(i>0){ //Base case cout<< "****** << endl; cout<< "* * << endl; cout<< "****** << endl<< endl; draw_rect(--i); //Recursive call } } 9

Example: Factorial Definition 0!=1; n!=n * (n-1) * * (n-(n-1)) * 1 = n * (n-1)! ; n > 0 10

Iterative Factorial factorial(0) = 1; factorial(n) = n*n-1*n-2* *n-(n-1)*1; long factorial(int n) { long fact; if(n==0) fact=1; else for(fact=n; n > 1; n--) fact=fact*(n-1); return fact; } 11

Recursive Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); long factorial(int n) { if (n == 0) // Base case return 1; else return n * factorial(n -1); // Recursive call } 12

Computing Factorial Iteratively factorial(4) factorial(0) = 1; factorial(n) = n*(n-1)* *2*1;

Computing Factorial Iteratively factorial(4) = 4 * 3 factorial(0) = 1; factorial(n) = n*(n-1)* *2*1;

Computing Factorial Iteratively factorial(4) = 4 * 3 = 12 * 2 factorial(0) = 1; factorial(n) = n*(n-1)* *2*1;

Computing Factorial Iteratively factorial(4) = 4 * 3 = 12 * 2 = 24 * 1 factorial(0) = 1; factorial(n) = n*(n-1)* *2*1;

Computing Factorial Iteratively factorial(4) = 4 * 3 = 12 * 2 = 24 * 1 = 24 factorial(0) = 1; factorial(n) = n*(n-1)* *2*1;

Computing Factorial Recursively factorial(4) factorial(0) = 1; factorial(n) = n*factorial(n-1);

Computing Factorial Recursively factorial(4) = 4 * factorial(3) factorial(0) = 1; factorial(n) = n*factorial(n-1);

Computing Factorial Recursively factorial(4) = 4 * factorial(3) = 4 * ( 3 * factorial(2)) factorial(0) = 1; factorial(n) = n*factorial(n-1);

Computing Factorial Recursively factorial(0) = 1; factorial(4) = 4 * factorial(3) = 4 * ( 3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) factorial(n) = n*factorial(n-1);

Computing Factorial Recursively factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * ( 3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) = 4 * ( 3 * ( 2 * (1 * factorial(0))))

Computing Factorial Recursively factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) = 4 * ( 3 * ( 2 * (1 * factorial(0)))) = 4 * ( 3 * ( 2 * (1 *1)))

Computing Factorial Recursively factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) = 4 * ( 3 * ( 2 * (1 * factorial(0)))) = 4 * ( 3 * ( 2 * (1 *1))) = 4 * ( 3 * ( 2 * 1))

Computing Factorial Recursively factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) = 4 * ( 3 * ( 2 * (1 * factorial(0)))) = 4 * ( 3 * ( 2 * (1 *1))) = 4 * ( 3 * ( 2 * 1)) = 4 * (3 * 2)

Computing Factorial Recursively factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) = 4 * ( 3 * ( 2 * (1 * factorial(0)))) = 4 * ( 3 * ( 2 * (1 *1))) = 4 * ( 3 * ( 2 * 1)) = 4 * (3 * 2) = 4 * 6

Computing Factorial Recursively factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * (3 * factorial(2)) = 4 * ( 3 * (2 * factorial(1))) = 4 * ( 3 * ( 2 * (1 * factorial(0)))) = 4 * ( 3 * ( 2 * (1 *1))) = 4 * ( 3 * ( 2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24

Differences Pros Readability Cons Efficiency Memory

Recursive Factorial factorial(4) Executes factorial(4) Step 9: return 24 Step 8: return 6 return 4 * factorial(3) Step 0: executes factorial(4) Step 1: executes factorial(3) Step 7: return 2 return 3 * factorial(2) return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 6: return 1 Step 3: executes factorial(1) return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method

Recursive Factorial factorial(4) Step 9: return 24 Step 8: return 6 Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(3) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(2) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(1) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(2) for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(0) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(1) for factorial(2) for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns 1 Step 2: executes factorial(2) Step 7: return 2 return 2 * factorial(1) Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Stack for factorial(0) for factorial(1) for factorial(2) for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(0) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(1) for factorial(2) for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(1) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(2) for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(2) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(3) for factorial(4) Main method

Recursive Factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(3) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(4) Main method

Recursive Factorial returns factorial(4) factorial(4) Step 9: return 24 Step 8: return 6 return 4 * factorial(3) Step 0: executes factorial(4) Step 1: executes factorial(3) Step 7: return 2 return 3 * factorial(2) return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method