Ask Question
23 December, 05:26

Given a recursive function as below. Predict the output for the inputs given. Show the steps.

int X (int n)

{

if (n==1) return 5;

else

return n + X (n/2);

}

Predict the value of X (10), show the steps of finding that out.

+3
Answers (1)
  1. 23 December, 05:40
    0
    22.

    Explanation:

    First call will be made to X (10). Since 10 is not equal to 1 else will be executed

    X (10) = 10+X (5) ... (1)

    X (5) will be called in the call stack again else will be executed and it will return

    X (5) = 5+X (2) ... (2)

    X (2) will be called and else will get executed and it will return.

    X (2) = 2+X (1) ... (3)

    Now the argument is 1 if will be executed. Hence X (1) will return 5.

    The value of X (2) will be 7. So the value of X (5) will be 5+7=12.

    X (10) = 10 + 12=22.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Given a recursive function as below. Predict the output for the inputs given. Show the steps. int X (int n) { if (n==1) return 5; else ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers