Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ex. 1. Tracing and understanding recursion Consider the following recursive function, that we can assume to be called with non-negative numbers: int mystery(int n, int

image text in transcribed
Ex. 1. Tracing and understanding recursion Consider the following recursive function, that we can assume to be called with non-negative numbers: int mystery(int n, int m) { if (n == m) return n; else if (n == 0) return m; else if (m == 0) return n; else return mystery(m, n % m); } a. Tracing the execution Trace the execution ofthe function calls mystery( 363 , 55) , mystery( 126 , 49) , and mystery( 81 , 37) How many calls do each of them make until they return the answer? What is the value of the parameters in each of the calls? Which base case do they reach? b. Solve the mystery What does the function actually do? Can you connect it to a known algorithm for solving this problem? Ex. 2. Recursion Tree Consider the following recursive function, for which again, we can assume the parameters to be non-negative numbers. This function computes the combinations of k out of n objects using Pascal's triangle formula. int combinations(int k, int n) { if (k > n) return 0; else if (n == k || == 0) return 1; else return combinations(kl, nl) + combinations(k, nl); } a. Draw the recursion tree for computing combinations ( 4 , 7 ). b. How many nodes does the runtime stack contain at the most for the function call above, not including the main? c. How many repeated function calls can you obseive in the tree? Give an example. Is this an indication that the function is inefficient

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions