Question: ( 3 . 1 . 4 ) An algorithm is said to be tail recursive when all of its recursive calls are in tail position

(3.1.4) An algorithm is said to be tail recursive when all of its recursive calls are in tail position: when each recursive call is the final operation that the algorithm must perform before returning. Such algorithms can often be more aggressively optimized into more efficient machine code. Consider the following recursive algorithm: MaxDifference(A =(a0 a1 an 1)) Input: A finite sequence A of n 2 integers Output: The largest difference between consecutive integers in A 1: if n =2 then 2: return a0 a13: else 4: return max a0 a1 MaxDifference((a1 a2 an 1)) This algorithm finds the largest difference between consecutive elements in a sequence. For example, given A =(82951), it returns 29=7. However, it is not tail recursive, because it must perform the max a0 a1 operations after its recursive call. Rewrite this algorithm tail recursively (and also non-generatively).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!