Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Recursion / Exceptions Answer the following questions. THIS ASSIGNMENT WILL BE DUE AT A DATE TO BE ANNOUNCED IN CLASS. (points as noted - 100

Recursion / Exceptions Answer the following questions. THIS ASSIGNMENT WILL BE DUE AT A DATE TO BE ANNOUNCED IN CLASS. (points as noted - 100 points total) 1. What is the output of the following code? (10 points) def function (num): if (num > 0): for x in range(0,num): print('*',end=' ') print(" ") function(num-1) def main (): x = 10 function(x) main() 2. Ackermanns function is a recursive mathematical algorithm that can be used to test how well a computer preforms recursion. Write a function A(m,n) which solves Ackermanns Function. Use the following logic in your function: If m = 0 then return n+1 If n = 0 then return A(m-1, 1) Otherwise, return A(m-1, A(m, n-1)) Test your function in a program that displays the following values: A(0, 0), A(0,1), A(1,1), A(1,2), A(1,4), A(2,2), A(3,2). (10 points) 3. Write a recursive function to compute the nth term of the sequence defined by the recursive relation an = an-1 + an-2 + an-3, where a0 = 1, a1 = 1, and a2 = 1, and n = 3, 4, 5, ... . Then, using the approach that we used in Class 15, identify what happens to the ratio an/an-1 as n gets larger. 4. What is the output of the following code, assuming that the following values are inputted: 0, 8, aad43, and -3. (10 points) numerator = 5 try: denominator = float(input("Please enter your denominator: ")) if(denominator<0): throw value = ((5/denominator)**0.5) print("The square root of 5.0/",denominator," is ",sep='',end='') print(format(value,'.4f'),".",sep='') except IOError: print("Error opening file!") except ValueError: print("Non-numerical value entered!") except ZeroDivisionError: print("Cannot divide by zero!!!") except: print("Other error!") else: print("It worked! ",end='') finally: print("Done.") 5. Write an exception handler to handle the natural logarithm function. Your code should prompt the user to enter a positive value, then have the exception handler take care of the case where the argument is not positive. Have the program output the natural logarithm of the input value with 4 decimal places displayed. Prompt the user to enter additional values if the user so desires. (40 points)Does there appear to be a golden ratio for this recursively defined function? (30 points)

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

What is TCO?

Answered: 1 week ago