What is the output after each recursive function call?
古いコメントを表示
Consider the following code:
function [out] = myRecurfn(num)
if floor(num) == 0
out = 2
else
out = 4 + myRecurfn(num-1)
end
end
When the above function is called with myRecurfn(3.5), what is the value of the output variable? Show your work, by showing what is ‘out’ for every call of the function and then how the final value is reached.
Since the value of out is only given for floor(num) == 0 (i.e. when num = 0.5) does that mean that the output for all the calls up to this point would just be: out = 4 + myRecurfn(3.5-1), out = myRecurfn(2.5-1), out = myRecurfn(1.5-1), and then the first output is out = 2? I am confused what is meant by a function call in this case, or what the output should look like for each call.
1 件のコメント
Jonas
2023 年 5 月 8 日
you are on the right way, but floor() does not mean -1 as you wrote. floor of 3.5 is 3, not 2.5.
floor calcultes the nearest smaller integer of the input if the input is not already integer
3.5 -> 3
2 -> 2
-1.5 -> -2
and so on
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!