Lecture 1 -- 1Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick How do Calculators Computer Square Roots? rWhen you enter 29 into your calculator and push the square.
Advertisements

Lecture Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything.
Lecture Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where.
Lecture Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient.
Lecture Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool.
Lecture Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream.
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Lecture Computer Science I - Martin Hardwick Bank Simulation (1) #include using namespace std; // Create bank account data type struct acct { //
Beauty in Recursion, Fractals Gur Saran Adhar Computer Science Department.
For(int i = 1; i
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
More About Recursion - 2 Java. Looking at more recursion in Java A simple math example Fractals.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Lecture 8. MIPS Instructions #4 – Branch Instructions #2
Recursion in Scala. 2 Definitions A recursive method is a method that calls itself A method is indirectly recursive if it calls a method that calls a.
1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion.
Lecture 8 of Computer Science II Recursions Instructor: Mr.Ahmed Al Astal.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Lecture 32 CSE 331 Nov 18, HW 8 solutions Friday.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Recursion Jordi Cortadella Department of Computer Science.
computer
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
Recursion Function calling itself
Templated recursive linked lists
Intro to Programming Week # 6 Repetition Structure Lecture # 10
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
10.3 Details of Recursion.
Solve: 1. 4<
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
دور المجتمع المدني في ترشيد وتحسين كفاءة الطاقة آفاق جديدة ومتجددة...
Microprocessor and Assembly Language
Functions Recursion CSCI 230
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
do/while Selection Structure
CS148 Introduction to Programming II
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
CS150 Introduction to Computer Science 1
Programming Fundamental
Computer Graphics, KKU. Lecture 11
Presentation transcript:

Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the problem without recursion l A part that makes the problem simpler and then uses recursion rHere is a simple recursive function int factorial (int val) { cout << Entering with val = << val << endl; if (val == 1) {// simple part cout << Simple case << endl; return 1; { else {// simplification part int n = val * factorial (val-1); cout << Leaving n = << n << endl; return (n); }

Lecture Computer Science I - Martin Hardwick Factorial 5 rAs we enter the function we print the current val rWhen we get to the simple case we print Simple case rAs we exit we print the value of n

Lecture Computer Science I - Martin Hardwick The call stack rAs each function is entered a frame is added to the call stack rThe frame contains the values of the local variables rThe frame is created for both recursive and non-recursive functions CALL Stack: f3 () F3: f2 () F2: call f1 () F1: call return

Lecture Computer Science I - Martin Hardwick The call stack for a recursive function rSame thing as the non-recursive case except that at some point the function (F3) must execute its simple case or the stack will overflow CALL Stack: F3 call F3 call F3 call F3 call return

Lecture Computer Science I - Martin Hardwick The call stack and local variables rThe call stack stores the values of the function variables rEach frame in the call stack has its own copy rFor the factorial function this allows the first frame to have the value 4, the second to have the value 3, the third to have the value 2 and the last to have the value 1. Factorial: Val = 4 Factorial: Val = 3 Factorial: Val = 2 Factorial: Val = 1 N =Factorial (val -1) Stops because Val =

Lecture Computer Science I - Martin Hardwick The call stack and return values rEach function call returns a value to the calling function using return rFor a recursive function this value holds the in-progress answer Factorial: N: Factorial: N: Factorial: N: Factorial: N: Factorial (1) Factorial (2) Factorial (3) Main: :24 Factorial (4)

Lecture Computer Science I - Martin Hardwick Recursive programming rThe hard part of recursive programming is recursive thinking. rYou have to learn to divide the program into: l A part that makes the problem simpler l A part that solves a simple case rConsider Pascals triangle How do we compute a value for row =5 Col = 2? The value of P[5][2] is P[4][1]+P[4][2] In general P[R][C] = P[R-1][C-1]+P[R-1][C]

Lecture Computer Science I - Martin Hardwick Pascal Triangle (continued) rWhat is the simple case l If (C = 0 || R == 0) then P[R][C] = 1. rSo our program is as follows if (C == 0 || R == 0) return 1 else return (p(R- 1, C- 1) + p(R – 1, C)); The value of P[5][2] is P[4][1]+P[4][2]

Lecture Computer Science I - Martin Hardwick Or is it? rWhat about P[4][4] l The value of P[3][4] is unknown! rSo our program is as follows if (C == 0 || R == C) return 1 else return (p(R- 1, C- 1) + p(R – 1, C)); The value of P[5][2] is P[4][1]+P[4][2]