1. Consider the following algorithm: Algorithm rec Fun(x, y, n) ifn rec Function(val - 1)...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. Consider the following algorithm: Algorithm rec Fun(x, y, n) ifn<1 else if n-1 return Null return x + y else return (x + y) * rec Fun(x, y, n-1) a. What is the base condition? b. How do the recursive calls reach the base condition? c. Show a complete trace of the following algorithm and count the number of steps you perform. a. Using the input x=4 and n-3 b. Using the input x = 2 and n = 7 d. What does this algorithm do? e. Write an iterative algorithm that does the same function. 2. Consider the following algorithm: Algorithm rec Function(val) if (val <1) print"... else return print val+">>" rec Function(val - 1) // print is used to signify screen output. Anything between // the quotation marks t is displayed as is and the + // concatenates the two parts on the screen Ile.g. if you have x=253 then print x+" // 253---253 +x will print print "<<"+ val return a. What is the base condition? b. How do the recursive calls reach the base condition? c. Show a complete trace of the following algorithm and count the number of steps you perform. a. Using the input val - 2 b. Using the input val = 8 3. Write a recursive algorithm to calculate the Fibonacci number (https://en.wikipedia.org wiki Fibonacci number). The Fibonacci number of an input value x is calculated as fib(x) = fib(x-1)+ fib(x-2); Also it is known that fib(0) = 0 and fib(1) = 1 For example if x = 4 then fib(4) = fib(3) +fib(2) fib(2) + fib(1) + fib(1) + fib(0) fib(1) +0 +fib(0) +fib(1) +fib(1) +fib(0) +1 +1 +0 =3 1. Consider the following algorithm: Algorithm rec Fun(x, y, n) ifn<1 else if n-1 return Null return x + y else return (x + y) * rec Fun(x, y, n-1) a. What is the base condition? b. How do the recursive calls reach the base condition? c. Show a complete trace of the following algorithm and count the number of steps you perform. a. Using the input x=4 and n-3 b. Using the input x = 2 and n = 7 d. What does this algorithm do? e. Write an iterative algorithm that does the same function. 2. Consider the following algorithm: Algorithm rec Function(val) if (val <1) print"... else return print val+">>" rec Function(val - 1) // print is used to signify screen output. Anything between // the quotation marks t is displayed as is and the + // concatenates the two parts on the screen Ile.g. if you have x=253 then print x+" // 253---253 +x will print print "<<"+ val return a. What is the base condition? b. How do the recursive calls reach the base condition? c. Show a complete trace of the following algorithm and count the number of steps you perform. a. Using the input val - 2 b. Using the input val = 8 3. Write a recursive algorithm to calculate the Fibonacci number (https://en.wikipedia.org wiki Fibonacci number). The Fibonacci number of an input value x is calculated as fib(x) = fib(x-1)+ fib(x-2); Also it is known that fib(0) = 0 and fib(1) = 1 For example if x = 4 then fib(4) = fib(3) +fib(2) fib(2) + fib(1) + fib(1) + fib(0) fib(1) +0 +fib(0) +fib(1) +fib(1) +fib(0) +1 +1 +0 =3
Expert Answer:
Answer rating: 100% (QA)
1a Base condition if n 1 return NULL 1b for any integer value of n 1 we reach to the nested if else ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
Evaluate the limit using continuity. lim tan(x - y) (x,y) (2,3)
-
A cylinder with a movable piston contains 3.00 mol of N2 gas (assumed to he have like an ideal gas). (a) The N2 is heated at constant volume until 1557 J of heat have been added. Calculate the change...
-
Evaluate the claim made in this article that if inflationary expectations become strongly entrenched an economy will experience a persistent output gap. The indication is that inflationary...
-
In 2013, Thomas DePrince, a passenger aboard a cruise ship, visited the ships jewelry boutique, operated by Starboard Cruise Services, Inc. DePrince told the employees of the boutique that he was...
-
House Company is considering three capital expenditure projects. Relevant data for the projects are as follows. Annual income is constant over the life of the project. Each project is expected to...
-
The number of admissions to all types of hospitals in a country between 1989 and 1996 can be described by the function A(t) = 35.283t2-1053.78t+40,539.967 thousand people where t is the number of...
-
Zim Company uses the following cost function: Y = $4200 + $5.25X. If the number of units produced in a month is 100, what would be the total cost?
-
Given the equation ax2 + bx + c = 0. If b^2 4ac > 0, then its roots can be calculated with the use of the following two different types : (I) = (b (b^2 4ac))/2a (II) + =2c / (b + (b^2 4ac)), = c...
-
Monochromatic light incident on a slit of width 0.660 mm produces a divraction pattern on a screen 1.70 m away from the slit. If the third-order dark fringe of the pattern is 3.03 mm from the center...
-
An automobile has a mass of 900 kg (about a ton).It travels east at 25 m/s (just about 56 mph which is speeding, almost) when it hits a slippery stretch of highway.The driver loses control of the...
-
in "Dadi's Family" Documentary, what is an in-depth analysis of: How are the finances handled in the extended family? What does Dadi say would happen to the family without a mother-in-law?
-
Define instrumental and structural theories as part of the institution of socialization. Explain why theorists continue a debate of rich versus poor families and individuals with respect to laws and...
-
A chloride ion, with one excess electron, experiences an attractive force of 5.810 9 N when it is located 2.010 10 m away from a sodium ion. What is the charge on the sodium ion?
-
An FX call on the euro has an exercise price of 1.12 $/ and the current FX rate is 1.15$/. There are 36 days until expiration. The volatility of the euro is 17%. Assume the US rate is 4% and the euro...
-
Refer to Exercise 8.S.I. Construct a scatterplot of the data. Does the appearance of the scatterplot indicate that the pairing was effective? Explain. Exercise 8.S.I. A volunteer working at an animal...
-
What is the relationship between TPs, VPs, and VCs?
-
A certification authority (CA) is designed to solve the problem of distributing __________ keys. a. Secret b. Public c. Private
-
What is the minimum number of bits in a PN sequence if we use FHSS with a channel bandwidth of B = 4 KHz and Bss = 100 KHz?
-
There is another possible explanation for purchased goodwill appearing in a sole proprietor's statement of financial position. What do you think it might be?
-
Why do the assets need to be revalued in these cases? The business has not been sold.
-
The shown partners have always shared profits and losses in the ratio: Holt 4; Stott 2: Young 1. From 1 January the assets were to be revalued as the profit sharing ratios are to be altered soon. The...
Study smarter with the SolutionInn App