CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Similar documents
CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)

CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

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

Lecture 6: Recursion RECURSION

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Lecture 6 CS2110 Spring 2013 RECURSION

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

The power of logarithmic computations. Recursive x y. Calculate x y. Can we reduce the number of multiplications?

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

Data Structures And Algorithms

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

COT 3100 Spring 2010 Midterm 2

CSCE 110 Dr. Amr Goneid Exercise Sheet (6): Exercises on Structs and Dynamic Lists

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Recursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).

RECURSION. Many Slides from Ken Birman, Cornell University

DM502 Programming A. Peter Schneider-Kamp.

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

CS1 Recitation. Week 2

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Lecture 7 CS2110 Fall 2014 RECURSION

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

CSCE 210 Dr. Amr Goneid Exercises (1): Stacks, Queues. Stacks

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

Overview of UE 101: Algorithms and Programming

Name :. Roll No. :... Invigilator s Signature :.. CS/B.TECH (NEW)/SEM-2/CS-201/ BASIC COMPUTATION & PRINCIPLES OF COMPUTER PROGRAMMING

Recursion. Example R1

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

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

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

Anatomy of a static method, S&W, 2.1, figure page 188. Class.name or just name Scope, S&W, 2.1, figure page 189. Overloading

CSE100 Principles of Programming with C++

Mathematical Induction

Task 1: Print a series of random integers between 0 and 99 until the value 77 is printed. Use the Random class to generate random numbers.

CITS1001 exam 2013 SAMPLE SOLUTIONS O cial cover page to be substituted here. May 10, 2013

Unit #2: Recursion, Induction, and Loop Invariants

COS 126 General Computer Science Fall Exam 1

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 6 Discussion:

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

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

DM536 Introduction to Programming. Peter Schneider-Kamp.

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Recursive Definitions

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

ALGORITHM 2-1 Solution for Exercise 4

Types of Recursive Methods

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

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Flow Chart. The diagrammatic representation shows a solution to a given problem.

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CSCE 110 PROGRAMMING FUNDAMENTALS

Recursion. Recursion [Bono] 1

Recursion. John Ross Wallrabenstein

Module 7 Highlights. Mastered Reviewed. Sections ,

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Alexandria University

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Lab Instructor : Jean Lai

Introduction to Computer Science Midterm 3 Fall, Points

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

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

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

CS201 Discussion 7 MARKOV AND RECURSION

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Exercise 1 ( = 24 points)

Lecture 10: Recursive Functions. Computer System and programming in C 1

Functionally Modular. Self-Review Questions

Measuring Efficiency

COS 126 General Computer Science Spring Written Exam 1

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

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

CSCE 110 PROGRAMMING FUNDAMENTALS

CS 310 Advanced Data Structures and Algorithms

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

Unit #3: Recursion, Induction, and Loop Invariants

Solutions to Assessment

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Transcription:

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return what (x, y-x); else return x; Trace the above function for the following calls: what (104,16) what (15,35) what (63,18) What is the objective of the above function? Write a recursive Boolean function palindrome ( s, first, last ) to return true if the string s is a palindrome, that is, the string reads the same backwards as forwards. Examples are dad and bob. The parameters first, last are the indices of the first and last elements of the part of the string being checked, respectively. Validate your function using examples of your choice. Given the following array a[ ] of integers: 5 3 0 17 10 9 20 0 1 2 3 7 Trace the function call m = process(a, 12, 10 ) to determine the value of m and the final contents of a[ ] returned by the following recursive function: int process (int a[ ], int n, int x ) if ((x < 0) (x > n-1)) return 0; else if (a[x] == 0) return 0; else a[x] = 0; return (1 + process(a, n, x-1) + process(a, n, x+1));

Consider the following recursive function : long int Func ( int n, int m ) if ((m == 0) (m == n)) return 1 ; else return ( Func ( n-1, m) + Func( n-1, m-1) ) ; If long int z = Func( 5, 3 ); trace the above function to determine the value of z. Consider an array a[ ] of non-zero integers. Implement a recursive function sum (a[ ], s, e) based on the Divide & Conquer method to return the sum of all the elements in the array between index (s) and index (e) inclusive (use division at the approximate middle). Test your function by tracing it for the call sum (a, 0, 6) given the following array: 2 4 1 8 4 8 2 Consider the following main function that invokes the recursive function value. Trace the function call to determine the final contents of the array a[ ]. int main( ) int a[6] = 5, 7, 12, 3, 14, 3 ; int k = 1; for ( int i = 0; i < 6; i++) a[ i ] = value (k) / (k*k); k *= 2; int value ( int n ) if ( n < 2 ) return 0; else return ( 4 * value (n / 2) + n * n ); Answer: i k value(k) a[i]

Write a recursive function oddcount ( int a[ ], int s, int e) to receive an integer array a [ ] a start index (s), and an end index (e) and return the number of odd integers in that array between s and e. Write a recursive function that receives a string of characters and returns the accumulating sum of the ASCII values of the characters in the string, excluding blanks from the sum. Write a recursive function to return the number of zeros in an array A of integers of size n Write a recursive boolean function to return true if an integer x is found in the first n elements of an array of integers A, and false otherwise. Write a recursive function Sum(int X[ ], int N) to return the sum of the values in an integer array X with subscripts from 1 to N. Let x be positive real. To calculate the square root of x by Newton's Method, we start with an initial approximation a = x/2. If abs (a*a-x) < epsilon, we stop and return with the result (a). Otherwise, we replace (a) by (a + x/a) / 2 and repeat the process until we find an approximation close enough to stop. Write a recursive function Newton (x, a, epsilon) to compute the square root of x using the above method and taking epsilon = 10-6. Write a function Square_Root(x) to call the above function and return the final result of the square root of x. Assume that an array a[ ] of characters of size N = 7 has the following contents: O N E T W O Trace the following functions to show the output of the calls Show1(a, 0, N-1) and Show2(a, 0, N-1) void Show1 ( char a[ ], int s, int e) if (s < = e) int m = (s + e)/2; cout << a[m]; Show1 ( a, s, m-1); Show1 ( a, m+1, e); void Show2 ( char a[ ], int s, int e) if (s < = e) int m = (s + e)/2; Show2 ( a, s, m-1); cout << a[m]; Show2 ( a, m+1, e);

The Fibonacci numbers can be computed using the following recursive function: int Fib(int n) if (n < 2) return 1; else return Fib(n-2) + Fib(n-1); What is the value of Fib ( 8 )? How many recursive calls are needed to compute Fib( 8 )? The Binomial Coefficient is also the number of combinations of (m) things out of (n) things. It may be written as: n n 1 n 1 comb( n, m), m n m m 1 m with comb( n,0) comb( n, n) 1 This leads to the recursive function: int comb (int n, int m) if ((m == 0) (m == n)) return1; else return comb (n 1, m 1) + comb (n 1, m); What is the value of comb ( 6, 2 ) and how many calls are needed? Ackermann s function A(m,n) is defined as : n + 1 if m = 0 A(m,n) = A(m-1, 1) if n = 0 A(m-1, A(m, n-1)) otherwise What is the value of A(2,1)? How many recursive steps are used in this case?

Given a global array x[0..n-1] of integers and the following recursive function: void Ruler ( int L, int R, int h ); m = ( L + R ) / 2 ; If ( h > 0 ) x[m] = h ; Ruler ( L, m, h-1) ; Ruler (m, R, h-1 ) ; Trace the above algorithm with the call Ruler ( 0, 8, 3 ) and show the resulting first 7 elements of the array. Draw vertical lines with heights proportional to the values in the array locations. What do these lines represent?