Question: Analysis the following code: int RunningSum ( int n ) { if ( n = = 1 ) return 1 ; else return ( n

Analysis the following code:
int RunningSum(int n)
{
if ( n==1)
return 1;
else
return (n + RunningSum(n-1));
}
During execution of the function call RunningSum(4), RunningSum makes a function call to itself, with an argument of 3(i.e., RunningSum(3)). However, before RunningSum(3) ends, it makes a call to RunningSum(2). And before RunningSum(2) ends, it makes a call to RunningSum(1). RunningSum(1), however, makes no additional recursive calls and returns the value 1 to RunningSum(2), which enables RunningSum(2) to end and return the value 2+1 back to RunningSum(3). This enables RunningSum(3) to end and pass a value of 3+2+1 to RunningSum(4). The image pictorially shows how the execution of RunningSum(4) proceeds.
Now, answer the following:
1. How many calls to RunningSum are made for the call RunningSum(10)? Explain your answer.
2. How about for the call RunningSum(n)? Give your answer in terms of n. Explain your answer.
Analysis the following code: int RunningSum ( int

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!