Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*** C Programming *** Exercise: Recursion We have gone through many examples in the class and those examples are available in the slides and uploaded

*** C Programming ***

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Exercise: Recursion We have gone through many examples in the class and those examples are available in the slides and uploaded codes. Try to test those codes and modify as you wish for getting more clarification. In addition try the following: 1) What would be the output of the following recursive function if we call rec2(5) ? void rec2 (int x) if (x==0) return; rec2 (x-1); printf("%d ", x); Answer: 2) Write a recursive function that calculates the sum 1' +2+3+...+o, given an integer value of n in between 1 and 9. You can write a separate power function in this process and call that power function as needed: int crazySum(int n); 3) Given the function below, what would the function call question3(10, 101) return? int questions (int a, int b) { if (a == 0) return b; if (b == 0) return a; return question3 (10*a+b$10, b/10); Answer: 4) Write a recursive function that converts a decimal number to an octal number 5) The code below returns the number of zeros at the end of n! (factorial n] int zeros (int n) int res = 0; while (n!=0) res n /= += n/5; 5; return res; Rewrite this method recursively: 6. Write a recursive function that returns the product of the digits of its integer input parameter, n. You omay assume that n is non-negative. For example, productDigits (243) should retum 24. since 2 x 4x3 = 24. int product Digits (int n) { 7. Let us define the weighted sum of an integer array a[0], a[1], a[2], ..., a[n-1] be a[0]*1 + a[1]*2+ a[2]*3 + .+a[n-1]*n. For example, the weighted sum of the array(5,2,6] would be 5*1+2*2+6*3 = 27. Write a recursive function that takes in an array numbers and its length n, and returns its weighter sum. You can assume n is non-negative integer. int weightedSum(int numbers [], int n) { 8. Mathematically, given a function f, we recursively define fin) as follows: if k= 1, f'(n) = f(n). Otherwise, for k > 1, f(n) = f(f(n)). Assume that there is an existing function f, which takes in a single integer and returns an integer. Write a recursive function fcomp, which takes in both n and k (k > 0), and returns f(n). int fint n); int fcomplint n, int k){ 9. What would be the value of fun(7) for the following function? int fun(int x) if(x==0) return; if(x3 ==0) return fun(x/3); return fun(x-1) + X; 10. Draw the recursion tree to find out the value of f(5) int fint n) int ans; int i; if(n

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

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Consider this article:...

Answered: 1 week ago