Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Similar documents
Arrays in C++ Instructor: Andy Abreu

Declaring a 2D Array

4. Java language basics: Function. Minhaeng Lee

Recursion. CSCI 112: Programming in C

CSCE 206: Structured Programming in C++

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 03

COS 126 General Computer Science Spring Written Exam 1

UNIVERSITY REGULATIONS

1 Anatomy of a Program 4

Arrays in Functions!

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Expressions and Casting

Exam 3 Chapters 7 & 9

CS536 Spring 2011 FINAL ID: Page 2 of 11

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

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

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

Name: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Data Structures CS 315 Spring 2017

Chapter 17 - Notes Recursion

Programming Language. Functions. Eng. Anis Nazer First Semester

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

CSCE 121 ENGR 112 List of Topics for Exam 1

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CSCE 2014 Final Exam Spring Version A

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Exam I Review Questions Fall 2010

PROBLEM 1 : (Trouble?: (14 pts)) Part A: (6 points) Consider the function Trouble below. int Trouble (const Vector<int> & numbers, int size) if (size

Memory and Pointers written by Cathy Saxton

Midterm Practice TA: Brian Choi Section Webpage:

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

Midterm Practice Exam

CMSC 341 Lecture 10 Binary Search Trees

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

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

Data structures and libraries

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Review Questions I Spring 2010

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS16 Exam #1 7/17/ Minutes 100 Points total

Study Guide for Test 2

Data Structures Lecture 3 Order Notation and Recursion

Exercise: Using Numbers

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

Decision Making -Branching. Class Incharge: S. Sasirekha

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Chapter 4: Making Decisions

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Programming Languages and Techniques (CIS120)

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Problem 1: Get val soon! (15 marks)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

CS 103 Unit 13 Slides

I. True/False: (2 points each)

CS61B Lecture #2. Public Service Announcements:

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Lecture 5: Making Decisions

CSE413 Midterm. Question Max Points Total 100

Chapter 4: Making Decisions

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements

ComS 228 Exam 1. September 27, 2004

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Problem Score Max Score 1 Syntax directed translation & type

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

A Foundation for Programming

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

Object-Oriented Programming, Iouliia Skliarova

APCS Semester #1 Final Exam Practice Problems

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Sorting. Bubble Sort. Selection Sort

Getting to know Greenfoot

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Fun facts about recursion

CS205: Scalable Software Systems

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

Algorithm Design And Analysis Asst. Prof. Ali Kadhum Idrees The Sum-of-Subsets Problem Department of Computer Science College of Science for Women

CSE 2123 Recursion. Jeremy Morris

Title: Counting While Loops

Final Exam Solutions PIC 10B, Spring 2016

Transcription:

Recursion [ Why is it important?] ~7 easy marks in Exam Paper Seemingly Different Coding Approach In Fact: Strengthen Top-down Thinking Get Mature in - Setting parameters - Function calls - return + work with return value Further Big Jump in programming after learning Debugger Learning Recursion Step 1. Understand Code Step 2. Understand Execution Step 3. Know how to write Repeat practicing the coding approach: showdigits (Notes P.6) + getlargestdigit (Q2) Until get clicked with confidence of coding steps. Don t hurry for Q3 (a)-(e) Required in some other core courses

// return the largest digit in a positive integer n int getlargestdigit(int n) Figure out the signature of the function

// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) The easiest case(s) which doesn t need recursion

// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n; Handle the easiest case(s) -- doesn t need recursion

// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n;

// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n; Recursive call (Has return value => save in a variable) getlargesetdigit( );

// return the largest digit in a positive integer n int getlargestdigit(int n) if (n<10) return n; Recursive call (Has return value => save in a variable) int m1; m1=getlargesetdigit(n/10);

// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 if (n<10) return n; Illustrate with example for further planning. int m1; m1=getlargesetdigit(n/10); //5673 => 7

// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 if (n<10) return n; Illustrate with example for further planning. int m1; m1=getlargesetdigit(n/10);//5673 => 7 Now, m1 and n%10 (ie. 7 and 2) can decide the final result.

// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 if (n<10) return n; int m1; m1=getlargesetdigit(n/10);//5673 => 7 if... return m1; return n%10; Illustrate with example for further planning. Now, m1 and n%10 (ie. 7 and 2) can decide the final result.

// return the largest digit in a positive integer n int getlargestdigit(int n)//56732 => 7 X if (n<10) For Q2 and Q3(a)-(e): return n; int m1; X * Do not add any code here (like int m1, return..). Reason: the if-part and -part have different logic. If we force them to share common code, then we add confusing complexity rather than m1=getlargesetdigit(n/10);//5673 giving significant advantage. => 7 if... return m1; return n%10;

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) Figure out the signature of the function

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) The easiest case(s) which doesn t need recursion

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) if (x%2==0) return true; return false; Handle the easiest case(s) -- doesn t need recursion

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); Simplify from: if (x%2==0) return true; return false;

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); Recursive call (Has return value => save in a variable)

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); bool m1=containeven(x/10);

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); //56732 => true bool m1=containeven(x/10); Illustrate with example for further planning. //5673 => true

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); //56732 => true bool m1=containeven(x/10); Illustrate with example for further planning. //5673 => true Now, m1 tells true/false about the leading digits. Only n%10 has not been checked yet. Therefore, using m1 and n%10, we can decide the final result.

// determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); //56732 => true bool m1=containeven(x/10); Illustrate with example for further planning. //5673 => true if (x%2==0) return true; return m1; Now, m1 tells true/false about the leading digits. Only n%10 has not been checked yet. Therefore, using m1 and n%10, we can decide the final result.

Or we can write the recursive part as: Then simply eliminate m1: // determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); if (x%2==0) return true; int m1=containeven(x/10) ; return m1; // determine whether an integer x contains even digit(s) (0,2,4,6,8) bool containeven(int x) return (x%2==0); if (x%2==0) return true; return containeven(x/10);

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) Figure out the signature of the function

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) The easiest case(s) which doesn t need recursion

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; Handle the easiest case(s) -- doesn t need recursion

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; Recursive call (no return value here)

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; showdigitsreverse(x/10); Recursive call (no return value here)

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; showdigitsreverse(x/10); //56732 => 2 3 7 6 5 Illustrate with example for further planning. //5673 => 3 7 6 5

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; showdigitsreverse(x/10); //56732 => 2 3 7 6 5 Illustrate with example for further planning. //5673 => 3 7 6 5 In the code, the recursive call can output 3 7 6 5. Only n%10 has not been handled yet. Since the required output is 2 3 7 6 5, we can easily decide what is to be done and when.

// show the digits of an integer x reversely (each followed by a space) void showdigitsreverse(int x) cout << x << " "; cout << x%10; showdigitsreverse(x/10); //56732 => 2 3 7 6 5 Illustrate with example for further planning. //5673 => 3 7 6 5 In the code, the recursive call can output 3 7 6 5. Only n%10 has not been handled yet. Since the required output is 2 3 7 6 5, we can easily decide what is to be done and when.