use octave or MATLAB as programs to code. do not usedisp or num2str. also, do not use break or continuestatements.

1. In this problem we will investigate recursively computing a general exponential function, For a an integer and n a nonnegative integer the exponential function can be defined recursively as for n=0 a.,-i for n > 0 = Write a program that calls a function get integer to get a value for a and which should • keep prompting the user and giving error messages until an integer is entered . return the resulting value The main program should then call a function get nonnegative integer to get a value for n and which should keep prompting the user and giving error messages until a nonnegative integer is entered return the resulting value The main program should then call a recursive function exponential function which should • take in a value for a and a value for n • call itself recursively as needed to calculate a” according to the formula above not use the built-in exponential operator • return the resulting value The main program should then print the result. Sample output: Enter an integer: 3.5 Invalid entry 3.5 Enter an integer: 2 Enter a nonnegative integer: -4 Invalid entry-4 Enter a nonnegative integer: 3 The result of -23 18 -8 2. In this problem we will modify the code for the Fibonacci sequence developed in class to give more output. Recall that the Fibonacci numbers are 0,1,1,2,3,5,8,… and can be defined recursively for n 3 as Fibonacci(n)= Fibonacci(n-1) + Fibonacci(n-2) with Fibonacci(1) = 0 and Fibonacci(2) = 1. Write a program that calls a function get positive integer which should • keep prompting the user and giving error messages until a positive integer is entered • return the resulting value The main program should then call a recursive function fibonacci which should • take in an integer n for which Fibonacci number to compute and an integer indicating how many times including the current call) the function has been called • upon entry, print a debug message displaying the values of the input variables call itself recursively as needed to calculate the specified Fibonacci number return the requested Fibonacci number and the counter of the number of calls to the fibonacci function The main program should then display the final result. Hint: Remember how to call a function that returns two variables Sample output: Enter positive integer: -2 Invalid entry – 2 Enter a positive integer 3.5 Invalid entry 3.5 Enter a positive integer: 5 Finace ter call to cepute Fibonacci 5 Fibonacci: Enter on call 2 to compute Fibonaci( 4 ) Fibonacci Enter Coll 3 to cepute Fibonacci 3 ) Fibonacel Enter call to comput. Fibonaco 2 ) REGLE Sater on call 5 to compete Fibonacci Piomacci: Enter on call to compete Fibenci 22 Fonec tor call 7 ta contrate Fibonacci 3 Fibonacci: Enter on call to compete Fibonacci (2) Fibonne Enter on all to compete Fibonace(1) P Below is a figure that might help explain the above sample output. vihen Pabananais renment ayant make Vibona Free me I ribonacotta ribanacast 3. In this problem we will investigate using recursion to determine whether ed vector is sorter (either nondecreasing or non increasing given a length 1 Vector is clearly is sorted and we will say it’s nondecreasing en g e wor easily determine if it’s sorted by comparing the two elements. Given a vector of length > 2, we can break the problem down by noting that if we knew the status and smallest and largest elements of the subvector of elements 2 through the end, we could use them und the value of the first element to determine the status of the given Vector rite a program that calls the function get vector which should call the function et positive integer (passing appropriate tring) once to obtain a value for the length of the vector use a loop to prompt the user to enter the vector one element time • return the entered vector and its length. The function get-positive integer should • take in a string input variable to use in the prompt • keep prompting the user and giving error messages using Xd) until a positive integer is entered • return the resulting value The main program should then call the function checks sorted which would • take in a vector of numbers and its length • print an entering status message giving the input variable values • call itself recursively to determine if the vector is nondecreasing nonincreasing not sorted (if it is both nondecreasing and nonincreasing call it nondecreasing) • print a “leaving a message giving the output variable values • return Da status integer that is equal to 1 if the vector is rondecreasing. -1 if it is increasing, and if it is not sorted the smallest integer present the input wtor if it is sorted or otherwise set equal to -99999 the largest integer present in the input vector if it is sorted or otherwise set equal to 99999 The main program should the print message based on the status. Sample output Enter a positive integer for the length: -4 Invalid entry -4 Enter a positive integer for the length: 2.4 Invalid entry 2.4 Enter a positive integer for the length: 6 Enter number 1: 5 Enter number 2 -3 Enter number 3: Enter number 4: Enter number 5: 10 Enter number 6: 13 check.it sorted: Entering with length 6 vector: -5.00 -3.00 -3.00 2.00 10.00 13.00 check.11.sorted: Entering with length 5 vector: -3.00 -3.00 2.00 10.00 13.00 check if sorted! Entering with length 4 vector: -3.00 2.00 10.00 13.00 check_11_sorted: Entering with length 3 vector: 2.00 10.00 13.00 check if sorted: Entering with length 2 vector: 10.00 13.00 check.11.sorted! Leaving for length 2 vector with status 1. smallest 10.00, largest 13.00 check_if_sorted: Leaving for length 3 vector with status 1, smallest 2.00, largest 13.00 check if sorted: Leaving for length 4 vector with status -3.00, largest 13.00 check.if.sorted: Leaving for length 5 vector with status 1. smallest -3.00, largest 13.00 check. 1.sorted: Leaving for length 6 vector with status 1. smallest -5.00, largest 13.00 The vector is nondecreasing HIM smallest Enter a positive integer for the length: 7 Enter number 1: 21.5 Enter number 2: 15 Enter number 3: 9.245 Enter number 4: 6 Enter number 5: 6 Enter number 6: 3 Enter number 7: -5.5 check.it.sorted: Entering with length 7 vector: 21.50 15.00 9.24 6.00 6.00 -3700 -5.50 check it sorted: Entering with length 6 vector: 15.00 9.24 6.00 6.00 -3.00 -5:50 check.if.sorted: Entering with length 5 vector: 9.24 6.00 6.00 -3.00 -5.50 check.1f .sorted: Entering with length 4 vector: 6.00 6.00 -3.00 -5.50 check.11.sorted: Entering with length 3 vector: 6.00 -3.00 -5.50 check if sorted: Entering with length 2 vector: vector: -3.00 -3.00 -5.50 check 11 sorted: Leaving for length2 vector with status -1, smallest -5.50, large check_11_sorted: Leaving for length 3 vector with status -1, smallest -5.50, large check if sorted: Leaving for length 4 vector with status -1, smallest -5.50, large check it sorted: Leaving for length 5 vector with a 1, smallest -5.50, large 9.24 check it sorted: Leaving for length 6 vector with a smallest -5.50, largest 15.00 check.1f .sorted: Leaving for length 7 vector with status -1, smallest -5.50, largest 21.50 The vector is nonincreasing 00 00 00 tus- 00 tus Enter a positive integer for the length: 7 Enter number 1: 2 Enter number 2: 4 Enter number 3: -5.6 Enter number 4: 15 Enter number 5: 13 Enter number 6: 12 Enter number 7: 10 check_if_sorted: Entering with length 7 vector -2.00 4.00 -5.60 15.00 13.00 12.00 10.00 check_if_sorted: Entering with length 6 vector: 4.00 -5.60 15.00 13.00 12.00 10.00 check if sorted: Entering with length 5 vector: -5.60 15.00 13.00 12.00 10.00 check if sorted: Entering with length 4 vector 15.00 13.00 12.00 10.00 check if sorted: Entering with length 3 vector: 13.00 12.00 10.00 check if sorted: Entering with length 2 vector: 12.00 10.00 check if sorted: Leaving for length 2 vector with status -1, smallest 10.00, largest 12.00 check_11_sorted: Leaving for length 3 vector with status -1, smallest 10.00, largest 13.00 check if sorted: Leaving for length 4 vector with status -1, smallest 10.00, largest 15.00 check_if_sorted: Leaving for length 5 vector with status 0. smallest -99999.00, largest 99999.00 check if sorted: Leaving for length 6 vector with s tatus o, smallest -99999.00, largest 99999.00 check_if_sorted: Leaving for length 7 vector with status 0. smallest -99999.00, largest 99999.00 The vector is not sorted 4. In this problem we will implement the depth first search of the Write a program calls a function get data which should pramt the user for the name of a kata file containing readjacency matrix and store it is a string variable achFileNameAdjacency the adjacency matrix from the specified file into the matrix aan Adjacency using anadjacency load -ascii’, achFileName Adjacency prompt the use for the name of a data file containing a tree dati vetor and store it in a string variable achFileNameVector load the data vector from the specified file into the vector andata using the command anData = load(-ascii, achFileNameVector keep prompting the user and giving error messages until an integer search value) is entered • return the adjacency matrix, data vector, and integer value The main program should then call the function depth first search which (as discussed in class) should • take in the number of nodes in the graph the adjacency matrix, the data vector, the number of the current node, and the search value • upon entry, print a debug message displaying the number of the current mode • call itself recursively as needed to implement depth first search for the specified integer • immediately before making the recursive call, print a debug message displaying the number of the node to be recursed upon • before exiting the function, print a vebug message displaying the number of the current node and the values of the two output variables return a Boolean variable indicating whether or not the search value was found and variable giving the mode number at which the search value was found (if it was otherwise – 1) The main program should then print the results of the search. Sample output: 1: adjacency.txt data vector: data.tz 31.5 recurs on child hode 2 to recurs on child bode 5 Leaving with bound – 0 and allode Foundat – – Node 2: haut to recurse on child node 6 Node 16 Entered Node 6: Leaving with Found – O and nodeFoundat — Nede 2: Leaving with bound – 0 and NodeFoundAt – -1 Node 1: About to recurse on child sode 3 Node 3 Entered Node 3: About to recurse on child node Node 7: Leaving with bound and nkode Foundat- Node 3: Leaving with bound 1 and NodeFound- Node 1. Leaving with Found = 1 And Nede Foundat- 31 was found at mode Enter filename for adjacency tri dirence Enter filenane for data vector: data.txt Enter an integer to search for: 3 Entered 1: About to recurse on child Entered 2: About to recurs Node 5 Entered Node 51 Leaving with bound and Nadeo 21 hout to Node 6: Leaving with bound – and NodeFound Node 2: Leaving with bound And odeFoundat Mode 1 About to recurs on child node 3 1 3: Aboat to recurso child sode Node Leaving with bound and nodeFoundat- Node 3: Leaving with bound and lodeFoundat – – ode 1 About to recurse on child med Mode 4: About to recurse on child node d ode -1 Node 9 Lei with Foun Node 4: About Node 10 Entered Node 10: Leavin with Found- Node 4: Leaving with found o Node 1: Leaving with found u Show transcribed image text 1. In this problem we will investigate recursively computing a general exponential function, For a an integer and n a nonnegative integer the exponential function can be defined recursively as for n=0 a.,-i for n > 0 = Write a program that calls a function get integer to get a value for a and which should • keep prompting the user and giving error messages until an integer is entered . return the resulting value The main program should then call a function get nonnegative integer to get a value for n and which should keep prompting the user and giving error messages until a nonnegative integer is entered return the resulting value The main program should then call a recursive function exponential function which should • take in a value for a and a value for n • call itself recursively as needed to calculate a” according to the formula above not use the built-in exponential operator • return the resulting value The main program should then print the result. Sample output: Enter an integer: 3.5 Invalid entry 3.5 Enter an integer: 2 Enter a nonnegative integer: -4 Invalid entry-4 Enter a nonnegative integer: 3 The result of -23 18 -8

2. In this problem we will modify the code for the Fibonacci sequence developed in class to give more output. Recall that the Fibonacci numbers are 0,1,1,2,3,5,8,… and can be defined recursively for n 3 as Fibonacci(n)= Fibonacci(n-1) + Fibonacci(n-2) with Fibonacci(1) = 0 and Fibonacci(2) = 1. Write a program that calls a function get positive integer which should • keep prompting the user and giving error messages until a positive integer is entered • return the resulting value The main program should then call a recursive function fibonacci which should • take in an integer n for which Fibonacci number to compute and an integer indicating how many times including the current call) the function has been called • upon entry, print a debug message displaying the values of the input variables call itself recursively as needed to calculate the specified Fibonacci number return the requested Fibonacci number and the counter of the number of calls to the fibonacci function The main program should then display the final result. Hint: Remember how to call a function that returns two variables Sample output: Enter positive integer: -2 Invalid entry – 2 Enter a positive integer 3.5 Invalid entry 3.5 Enter a positive integer: 5 Finace ter call to cepute Fibonacci 5 Fibonacci: Enter on call 2 to compute Fibonaci( 4 ) Fibonacci Enter Coll 3 to cepute Fibonacci 3 ) Fibonacel Enter call to comput. Fibonaco 2 ) REGLE Sater on call 5 to compete Fibonacci Piomacci: Enter on call to compete Fibenci 22 Fonec tor call 7 ta contrate Fibonacci 3 Fibonacci: Enter on call to compete Fibonacci (2) Fibonne Enter on all to compete Fibonace(1) P Below is a figure that might help explain the above sample output. vihen Pabananais renment ayant make Vibona Free me I ribonacotta ribanacast

3. In this problem we will investigate using recursion to determine whether ed vector is sorter (either nondecreasing or non increasing given a length 1 Vector is clearly is sorted and we will say it’s nondecreasing en g e wor easily determine if it’s sorted by comparing the two elements. Given a vector of length > 2, we can break the problem down by noting that if we knew the status and smallest and largest elements of the subvector of elements 2 through the end, we could use them und the value of the first element to determine the status of the given Vector rite a program that calls the function get vector which should call the function et positive integer (passing appropriate tring) once to obtain a value for the length of the vector use a loop to prompt the user to enter the vector one element time • return the entered vector and its length. The function get-positive integer should • take in a string input variable to use in the prompt • keep prompting the user and giving error messages using Xd) until a positive integer is entered • return the resulting value The main program should then call the function checks sorted which would • take in a vector of numbers and its length • print an entering status message giving the input variable values • call itself recursively to determine if the vector is nondecreasing nonincreasing not sorted (if it is both nondecreasing and nonincreasing call it nondecreasing) • print a “leaving a message giving the output variable values • return Da status integer that is equal to 1 if the vector is rondecreasing. -1 if it is increasing, and if it is not sorted the smallest integer present the input wtor if it is sorted or otherwise set equal to -99999 the largest integer present in the input vector if it is sorted or otherwise set equal to 99999 The main program should the print message based on the status. Sample output

Enter a positive integer for the length: -4 Invalid entry -4 Enter a positive integer for the length: 2.4 Invalid entry 2.4 Enter a positive integer for the length: 6 Enter number 1: 5 Enter number 2 -3 Enter number 3: Enter number 4: Enter number 5: 10 Enter number 6: 13 check.it sorted: Entering with length 6 vector: -5.00 -3.00 -3.00 2.00 10.00 13.00 check.11.sorted: Entering with length 5 vector: -3.00 -3.00 2.00 10.00 13.00 check if sorted! Entering with length 4 vector: -3.00 2.00 10.00 13.00 check_11_sorted: Entering with length 3 vector: 2.00 10.00 13.00 check if sorted: Entering with length 2 vector: 10.00 13.00 check.11.sorted! Leaving for length 2 vector with status 1. smallest 10.00, largest 13.00 check_if_sorted: Leaving for length 3 vector with status 1, smallest 2.00, largest 13.00 check if sorted: Leaving for length 4 vector with status -3.00, largest 13.00 check.if.sorted: Leaving for length 5 vector with status 1. smallest -3.00, largest 13.00 check. 1.sorted: Leaving for length 6 vector with status 1. smallest -5.00, largest 13.00 The vector is nondecreasing HIM smallest Enter a positive integer for the length: 7 Enter number 1: 21.5 Enter number 2: 15 Enter number 3: 9.245 Enter number 4: 6 Enter number 5: 6 Enter number 6: 3 Enter number 7: -5.5 check.it.sorted: Entering with length 7 vector: 21.50 15.00 9.24 6.00 6.00 -3700 -5.50 check it sorted: Entering with length 6 vector: 15.00 9.24 6.00 6.00 -3.00 -5:50 check.if.sorted: Entering with length 5 vector: 9.24 6.00 6.00 -3.00 -5.50 check.1f .sorted: Entering with length 4 vector: 6.00 6.00 -3.00 -5.50 check.11.sorted: Entering with length 3 vector: 6.00 -3.00 -5.50 check if sorted: Entering with length 2 vector: vector: -3.00 -3.00 -5.50 check 11 sorted: Leaving for length2 vector with status -1, smallest -5.50, large check_11_sorted: Leaving for length 3 vector with status -1, smallest -5.50, large check if sorted: Leaving for length 4 vector with status -1, smallest -5.50, large check it sorted: Leaving for length 5 vector with a 1, smallest -5.50, large 9.24 check it sorted: Leaving for length 6 vector with a smallest -5.50, largest 15.00 check.1f .sorted: Leaving for length 7 vector with status -1, smallest -5.50, largest 21.50 The vector is nonincreasing 00 00 00 tus- 00 tus

Enter a positive integer for the length: 7 Enter number 1: 2 Enter number 2: 4 Enter number 3: -5.6 Enter number 4: 15 Enter number 5: 13 Enter number 6: 12 Enter number 7: 10 check_if_sorted: Entering with length 7 vector -2.00 4.00 -5.60 15.00 13.00 12.00 10.00 check_if_sorted: Entering with length 6 vector: 4.00 -5.60 15.00 13.00 12.00 10.00 check if sorted: Entering with length 5 vector: -5.60 15.00 13.00 12.00 10.00 check if sorted: Entering with length 4 vector 15.00 13.00 12.00 10.00 check if sorted: Entering with length 3 vector: 13.00 12.00 10.00 check if sorted: Entering with length 2 vector: 12.00 10.00 check if sorted: Leaving for length 2 vector with status -1, smallest 10.00, largest 12.00 check_11_sorted: Leaving for length 3 vector with status -1, smallest 10.00, largest 13.00 check if sorted: Leaving for length 4 vector with status -1, smallest 10.00, largest 15.00 check_if_sorted: Leaving for length 5 vector with status 0. smallest -99999.00, largest 99999.00 check if sorted: Leaving for length 6 vector with s tatus o, smallest -99999.00, largest 99999.00 check_if_sorted: Leaving for length 7 vector with status 0. smallest -99999.00, largest 99999.00 The vector is not sorted

4. In this problem we will implement the depth first search of the Write a program calls a function get data which should pramt the user for the name of a kata file containing readjacency matrix and store it is a string variable achFileNameAdjacency the adjacency matrix from the specified file into the matrix aan Adjacency using anadjacency load -ascii’, achFileName Adjacency prompt the use for the name of a data file containing a tree dati vetor and store it in a string variable achFileNameVector load the data vector from the specified file into the vector andata using the command anData = load(-ascii, achFileNameVector keep prompting the user and giving error messages until an integer search value) is entered • return the adjacency matrix, data vector, and integer value The main program should then call the function depth first search which (as discussed in class) should • take in the number of nodes in the graph the adjacency matrix, the data vector, the number of the current node, and the search value • upon entry, print a debug message displaying the number of the current mode • call itself recursively as needed to implement depth first search for the specified integer • immediately before making the recursive call, print a debug message displaying the number of the node to be recursed upon • before exiting the function, print a vebug message displaying the number of the current node and the values of the two output variables return a Boolean variable indicating whether or not the search value was found and variable giving the mode number at which the search value was found (if it was otherwise – 1) The main program should then print the results of the search. Sample output:

1: adjacency.txt data vector: data.tz 31.5 recurs on child hode 2 to recurs on child bode 5 Leaving with bound – 0 and allode Foundat – – Node 2: haut to recurse on child node 6 Node 16 Entered Node 6: Leaving with Found – O and nodeFoundat — Nede 2: Leaving with bound – 0 and NodeFoundAt – -1 Node 1: About to recurse on child sode 3 Node 3 Entered Node 3: About to recurse on child node Node 7: Leaving with bound and nkode Foundat- Node 3: Leaving with bound 1 and NodeFound- Node 1. Leaving with Found = 1 And Nede Foundat- 31 was found at mode Enter filename for adjacency tri dirence Enter filenane for data vector: data.txt Enter an integer to search for: 3 Entered 1: About to recurse on child Entered 2: About to recurs Node 5 Entered Node 51 Leaving with bound and Nadeo 21 hout to Node 6: Leaving with bound – and NodeFound Node 2: Leaving with bound And odeFoundat Mode 1 About to recurs on child node 3 1 3: Aboat to recurso child sode Node Leaving with bound and nodeFoundat- Node 3: Leaving with bound and lodeFoundat – – ode 1 About to recurse on child med Mode 4: About to recurse on child node d ode -1 Node 9 Lei with Foun Node 4: About Node 10 Entered Node 10: Leavin with Found- Node 4: Leaving with found o Node 1: Leaving with found u

## Expert Answer

Answer to use octave or MATLAB as programs to code. do not use disp or num2str. also, do not use break or continue statements. …