CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 5. 150121. CARRANO CH 2, APPENDIX E.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Programming with Recursion
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Chapter 19 Recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CARRANO CH ; CUSACK CH 8: OPTIONAL.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
COSC 2006 Data Structures I Recursion II
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 MT258 Computer Programming and Problem Solving Unit 7.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT 12.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CUSACK CHAPT 4.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPTER 11.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Program Development and Design Using C++, Third Edition
Function Recursion to understand recursion you must understand recursion.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion CENG 707.
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion: The Mirrors
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Announcements Final Exam on August 17th Wednesday at 16:00.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion: The Mirrors
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion Chapter 11.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Announcements Final Exam on August 19th Saturday at 16:00.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion: The Mirrors
Presentation transcript:

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.

Agenda HW1 Grading ◦Rubric ◦PiggyBank.h detailed walk-through QUIZ: Next Week. Moved to Wednesday. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion.. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion. Recursion Lecture.

#include using namespace std; class PiggyBank { public: // Constructors PiggyBank(); PiggyBank(const string &name); PiggyBank(const int &pennies, const int &nickels, const int &dimes, const int &quarters); PiggyBank(const string &name, const int &pennies, const int &nickels, const int &dimes, const int &quarters); ~PiggyBank(); //getters/setters int getPennies() const; int getNickels() const; int getDimes() const; int getQuarters() const; int getTotal() const; int getName() const; bool setPennies(const int &pennies); bool setNickels(const int &nickels); bool setDimes(const int &dimes); bool setQuarters(const int &quarters); bool setName(const string &name); //Actions bool AddCoins(const int &pennies, const int &nickels, const int &dimes, const int &quarters); int Withdraw(int &pennies, int &nickels, int &dimes, int &quarters); //Operator Overloads bool operator==(const PiggyBank &piggy) const; bool operator!=(const PiggyBank &piggy) const; bool operator<=(const PiggyBank &piggy) const; bool operator<(const PiggyBank &piggy) const; bool operator>=(const PiggyBank &piggy) const; bool operator>(const PiggyBank &piggy) const; private: int pennies, nickels, dimes, quarters; string name; };

Quiz on Wednesday. Some likely topics. Tenants of OOP/C++ Development lifecycle Interface Design Encapsulation C++ fundamentals Constructors Operator Overloading: When/How Pass by value, ref, const ref Recursion Templates Algorithms: bubble sort, insertion sort Data structures: string, vector, array

In order to understand recursion, it helps to understand recursion. In order to understand recursion, it helps to understand recursion

In order to understand recursion, it helps to understand recursion. In order to understand recursion, it helps to understand recursion

Recursive Solutions Recursion breaks a problem into smaller identical problems Some recursive solutions are inefficient, impractical Complex problems can have simple recursive solutions DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

Recursive Solutions FIGURE 2-1 A RECURSIVE SOLUTION Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Recursive Solutions A recursive solution calls itself Each recursive call solves an identical, smaller problem Test for base case enables recursive calls to stop Eventually one of smaller calls will be base case DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

Compute n! recursively Base Case: n=1 Recursive: n! = n * (n-1) CODE:

The Box Trace FIGURE 2-5 BOX TRACE OF FACT(3) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Box Trace FIGURE 2-5 BOX TRACE OF FACT(3) … CONTINUED Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Box Trace FIGURE 2-5 BOX TRACE OF FACT(3) … CONTINUED Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

The Stack (and recursion) Stack Location in process which stores temp variables, call stack frames, One per thread of execution Heap Location in process which stores dynamically allocated memory (coming soon) Recursion Each call to a function creates stack frame Frame holds temp variables and locals Data in the BOX of the BOX trace Deep Recursion can allocate lots of space on the stack

Time of Invocation (constructors) Automatic Local Each time block is executed Static Local Once –first time it is hit Global In order of declaration in translation unit Typically before main() is entered Destroyed in reverse order of construction Dynamic (tbd) malloc/free new/delete

n! puzzle If an algorithm takes n! operations on a data set of size n, how big of a data set can can today’s computers solve?

Computer Scientist of the week Donald Knuth Born Milwaukee, WI Professor Emeritus at Stanford Father of Analysis of Algorithms Formalization of mathematical study of complexity of programs Wrote “Art of Computer Programming”, the definitive text on computer algorithms. Creator of TeX Won Grace Hopper Award, Turning Award, John von Neumann Medal. Opposed to granting computer patents

Recursive example 2.

=teaching%20programming%20recursion&sc=017&sp=1&sk=#view=detail&mid=17C444E18A3FFB1146E 017C444E18A3FFB1146E0 Recursion w/Alice

Quick review of base conversions = = 37 more on board…

Printing Numbers in Any Base How to convert a decimal to a hexadecimal Dividend Remainder 16) (10) ……..… 16) (10) ………6 16) (10) ………1 16) (10) …13(D) 16) 1883 (10) …12(C) 16) 117 (10) …11(B) 16) 7 (10) …… ) 0 (10) …… Divide by a base(16) 2. Print each reminder from the bottom: 75BCD16(16)

Printing numbers in any base (recursively) Base Case: if (num < base) { print out num } Recursive: Pre-pend to the answer ( num % base ) Recursively call with num/base

void PrintBase(int num, int base, string &ans) { string static DIGIT_ARRAY = " ABCDEF"; if (num < base) { ans = DIGIT_ARRAY[num] + ans; cout << ans; } else { ans = DIGIT_ARRAY[num % base] + ans; PrintBase(num / base, base, ans); } Printing numbers in any base (recursively)

Binary Search First half An entire sorted list First halfSecond half Find a value in an array.

Binary Search Base Case: low or high index finds number Recursive: Determine midpoint (high+low)/2 Search either lower or upper array

int BinarySearch(int value, int arr[], int index1, int index2) { if (value == arr[index1]) return index1; else if (value == arr[index2]) return index2; else if (index2 <= index1 + 1) return -1; else { int midpoint = (index1 + index2) / 2; if (arr[midpoint] > value) return BinarySearch(value, arr, index1, midpoint); else return BinarySearch(value, arr, midpoint, index2); } Binary Search

What does the box trace look like for binary search?