Question: Write a program that prompts the user for a positive integer N Then compute the value of triangle numbers Triangle (N) using a recursive
Write a program that prompts the user for a positive integer N Then compute the value of triangle numbers Triangle (N) using a recursive subroutine. The definition of a triangle number is: o Triangle(N 1 ) = N + Triangle(N-1) O For example: I I Triangle(1) = 1 Triangle( 2 ) = 3 Triangle( 3 ) = 6 Triangle( 4 ) = 10 For this, each function invocation has its own parameter N in its stack frame. Then it gets the return value from the next invocation and adds it into its N (stored in the stack frame) before retrieving that N (holding the sum) and putting the result into $v0 to return to the caller Your program will prompt the user for N and calculate and print Triangle(N). Test with several values, including a large value like 100. The square of an integer N is equal to Triangle(N) + Triangle( N-1 ). Write a recursive function using the Triangle function from part 1 to calculate Square (N). Your program has to prompt the user for a positive integer N and then print the result of Square (N). The definition of a square number is: Square(N 1 ) = Triangle(N) + Triangle(N-1) Call Square using a full stack frame, and have Square call Triangle twice, passing its argument on the stack. For the recursion, create automatic local variables on the stack and save both recursive results in the variables, then retrieve the values to add together to return to the caller.
Step by Step Solution
There are 3 Steps involved in it
It seems that the images ... View full answer
Get step-by-step solutions from verified subject matter experts
