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

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

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

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

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

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

CSCE 110: Programming I

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

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

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

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

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

Conditionals !

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

University of Dublin

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

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Mathematical Induction

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

Lab Manual. Program Design and File Structures (P): IT-219

Week 1 Questions Question Options Answer & Explanation A. 10 B. 20 C. 21 D. 11. A. 97 B. 98 C. 99 D. a

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

Lab Instructor : Jean Lai

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

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

COT 3100 Spring 2010 Midterm 2

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

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

Introduction to Computer Science Midterm 3 Fall, Points

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

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Section A Arithmetic ( 5) Exercise A

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

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

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

Intro to Computational Programming in C Engineering For Kids!

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CS 3410 Ch 7 Recursion

CSCE 110 PROGRAMMING FUNDAMENTALS

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

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

Solutions to Assessment

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

REPETITION CONTROL STRUCTURE LOGO

CSCE 206: Structured Programming in C++

Names and Functions. Chapter 2

CS205: Scalable Software Systems

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

Pointers, References and Arrays

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

15. Pointers, Algorithms, Iterators and Containers II

ALGORITHM 2-1 Solution for Exercise 4

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

Iosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C

Structured programming

Module 7 Highlights. Mastered Reviewed. Sections ,

Comp 182 Data Structures Sample Midterm Examination

BEng (Hons) Telecommunications. Examinations for / Semester 2

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

Laboratorio di Algoritmi e Strutture Dati

Lecture 6: Recursion 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:

COP 4516: Math for Programming Contest Notes

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

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

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

CS2141 Software Development using C/C++ C++ Basics

Recursion. Example R1

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

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

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

Recursion. Recursion is: Recursion splits a problem:

Increment and the While. Class 15

COS 126 General Computer Science Spring Written Exam 1

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

Ch 6. Functions. Example: function calls function

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

ESc101 : Fundamental of Computing

CSCI 135 Software Design and Analysis, C++ Homework 1 Solution

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

CS/CE 2336 Computer Science II

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

ISC 2011 COMPUTER SCIENCE PAPER 1 THEORY

Assertions & Verification & Example Loop Invariants Example Exam Questions

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

Suggestive List of C++ Programs

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Assertions & Verification Example Exam Questions

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Loop Invariant Examples

Transcription:

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions) 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? X=104 X=88 what (104,16) X=72 X=56 X=40 X=24 X=8 y=8 X=8 what (15,35) X=15 y=35 X=15 y=20 X=15 y=5 X=10 y=5 X=5 y=5 what (63,18) X=63 X=45 X=27 X=9 X=9 y=9 The function finds the GCD for two integers This is the recursive algorithm for the GCD (Greatest common Divisor of two integers). 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. bool palindrome(string s, int first, int last) if (first>=last) return true; else if(s[first]!= s[last]) return false; else return palindrome(s, first1, last-1); dad true Tracing Palindrome dad and daa true F=0, L=2 F=1, L=1 dabd false F=0, L=3 false F=1, L=2

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, x1)); m = process (a, 12, 10 ); 0 5 3 0 17 10 9 20 0 1 2 3 7 X=8 A[8] = 0 X=7 X=9 0 0 X=9 A[9] = 0 0 n=12, x=10 A[10] = 0 X=11 A[11] = 0 10 0 0 X=10 X=10 X=12 m = 4 This algorithm will return the size of the non-zero sequence of elements of which the element at x is a member. It will replace these elements by zeros. Hence, the size of the non-zero sequence of elements of which element at location 10 is a member will be 4 (values 1, 2, 3, 7). The final array will be: 5 3 0 17 10 9 20 0 0 0 0 0

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. long int z = Func( 5, 3 ); z=10 4 n=5, m=3 6 n=4, m=3 n=4, m=2 1 3 3 3 n=3, m=3 n=3, m=2 n=3, m=2 n=3, m=1 1 2 1 2 2 1 n=2, m=2 n=2, m=1 n=2, m=2 n=2, m=1 n=2, m=1 n=2, m=0 1 1 1 1 1 1 1,1 1,0 1,1 1,0 1,1 1,0 This is the recursive algorithm for finding the number of combinations of n objects taken m at a time (Binomial Coefficient). 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

int sum ( int a[ ], int s, int e) if ( s > e ) return 0; else if ( s == e ) return a [ s ]; else int m = ( s e ) / 2; return sum ( a, s, m ) sum ( a, m1, e ); Tracing sum (a, 0, 6): result is the sum of elements in array = 29 sum (Divide & Conquer) 15 s=0, e=6 M=3 29 14 s=0, e=3 M=1 s=4, e=6 M=5 A[0] s=0,e=0 6 s=0, e=1 M=0 A[1] A[2] 9 s=2, e=3 M=2 A[3] A[4] s=4, e=5 M=4 12 A[6] A[5] s=1,e=1 s=2,e=2 s=3,e=3 s=4,e=4 s=5,e=5 s=6, e=6 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. int oddcount(int a[ ], int s, int e) if (s > e) return 0; else return ( a[s]%2 ) oddcount (a, s1, 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. //Blanks Included int asciisum (string s, int first, int last) if( first > last) return 0; return int ( s [first] ) asciisum (s, first 1, last); // Blanks Excluded int asciisum (string s, int first, int last) if( first > last) return 0; if( s[first] == ) return asciisum (s, first 1, last); return int ( s [first] ) asciisum (s, first 1, last);

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. //Recursive Sequential Search of x in array A from location s through location e. bool LinSearch (int A[ ], int x, int s, int e) if (x == A[s]) return true; else if (s == e) return false; else return LinSearch (A,x,s1,e); Call this function as: LinSearch(A, x, 0, n-1); 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, m1, 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, m1, e);