Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Help Writing Recursive Functions (Code Included) lab6_part1.cpp [ https://pastebin.com/kcjFqiEv ] lab6_part2.cpp [ https://pastebin.com/9FbfzNPW ] Writing Recursive Functions For this part of the lab, download
C++ Help Writing Recursive Functions (Code Included)
lab6_part1.cpp [ https://pastebin.com/kcjFqiEv ]
lab6_part2.cpp [ https://pastebin.com/9FbfzNPW ]
Writing Recursive Functions For this part of the lab, download the lab6 part1.cpp file that contains starting code. There are four functions that you will need to implement. Do not modify the function prototypes! The main function has already been completely written for you to run test cases. Do not modify this either. Your job in this part is to solve the following two questions: 1. Given two positive integers x and y, how can we calculate the product of the numbers in between x and y (inclusive)? For example, if x = 3 and y=6, we should calculate 3*4*5*6 somehow. 2. Given an array and a target value, how can we determine if the target is an element of the array? You should start by solving these problems iteratively (i.e., using loops). These implementations should be written in the functions product and member, respectively. Then, once you have the iterative approach solved, come up with recursive solutions. Like in lecture, try to use the information from your iterative solution to come up with the framework for the recursion. Your recursive implementations should be written in productRec and member Rec, respectively. Make sure to also conduct an asymptotic analysis for each function! Sample Run If you implemented these functions correctly, you should get the following output from the program: Iterative answer: 2520 Recursive answer: 2520 Found the target 24.99 using iterative solution. Found the target 24.99 using recursive solution. Target 'q' not found using iterative solution. Target 'q' not found using recursive solution. Writing Recursive Mathematical Functions For this part of the lab, download the lab6 part2.cpp file that contains starting code. There are three functions that you will need to implement. Do not modify the function prototypes! The main function has already been completely written for you to run test cases. Do not modify this either. unsigned mult(unsigned x, unsigned y) This recursive function should implement the multiplication algorithm. Recall that multiplication is just repeated addition. We can define an algorithm that multiplies two non-negative integers x and y by adding x to itself a total of y times. For example, to multiply 5 and 4, the algorithm would keep adding 5 to itself a total of 4 times: 5* 4 = 5+5+5+5 unsigned power(unsigned x, unsigned y) This recursive function should implement the exponentiation (power) algorithm. Recall that exponentiation is just repeated multiplication. We can define an algorithm that raises the base x to the power of y by multiplying x to itself a total of y times. For example, to exponentiate 3 with 6, the algorithm would keep multiplying 3 to itself a total of 6 times: 36 = 3*3*3*3*3*3 unsigned fact(unsigned n) This recursive function should implement the following definition of the factorial: n! = (1 n=0 1 n=1 nx (n-1)! n > 2 Or, in a slightly more functional style: Fact(n) = (1 n=0 1 n=1 (nx Fact(n-1) n > 2 Make sure to also conduct an asymptotic analysis for each function! Sample Run If you implemented these functions correctly, you should get the following output from the program: 5* O = 7 * 3 = 21 0 * 4 = 0 10 * 6 = 60 2 Or, in a slightly more functional style: Fact(n) = (1 n=0 1 n=1 (nx Fact(n-1) n > 2 Make sure to also conduct an asymptotic analysis for each function! Sample Run If you implemented these functions correctly, you should get the following output from the program: 5* O = 7 * 3 = 21 0 * 4 = 0 10 * 6 = 60Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started