Pascal\'s triangle, illustrated on page 419 in the text, has rows that begin and
ID: 3843605 • Letter: P
Question
Pascal's triangle, illustrated on page 419 in the text, has rows that begin and end with "1". Within that, the numbers in each row are the pairwise sums of the numbers in the row above. Consider the following recursive algorithm intended to return row n of Pascal's triangle, if the row at the top is designated to be the 0 row. Triangle(n) If n = 0 return [1] b = Triangle(n-l) c is a vector of length n+1 with starting index equal to 1. c[1]=1 c[n+1]=1 for (j in 1 to n-l){c[j+1] = b[j]+b[j+1]} return c a. What is the base case? Does the algorithm return correctly in the base case? b. Does the algorithm terminate on non-negative integer input? If so, why? If not, give an example of input on which it fails to terminate. c. If the recursive call returns correctly, does the algorithm return the correct output? d. Does the algorithm perform correctly? If so, why? If not, why not?Explanation / Answer
2. a) Base case is the scenario in which the function returns the answer trivially i.e. it returns the answer without any recursion. So here the base case is n=0 or Triangle (0). So here the function returns a vector<int>{1} which is correct.
2.b) So the algorithm terminates for any non negetive input as we can see it starting from n it follows the call like n-1 the n-2 until it reaches 0, where it finally returns the value and It doesn't go beyond 0. using the previous steps output it builds the current steps output.
2.c) Here if the recursive call returns correctly then using the above logic it returns the result correctly.
2.d) Yes the algorithm performs correctly as If we take a close look into Pascal triangle we can observe the property that at row n the i th value can be obtained by adding the two consecutive value just above it i.e. i th and i+1 th value of n-1 th row. And in the case where n not equal to 0 it just skip the conditional execution of first two line. So both these conditions are satisfied by the algorithm so it gives correct output.