"Fixing" a value in a recursive function

1 回表示 (過去 30 日間)
antoine royer
antoine royer 2020 年 2 月 23 日
Hi,
I'm trying to use a recursive function to create a specific shape as an output. My function must have only one input. The objective is that my output shows a by x triangle of a specific symbol, such as:
*
**
***
Here is my actual code:
function triangle(x)
if x==1
xc=repmat('*',1,x);
fprintf('%+*s\n',20,xc);
else
triangle(x-1);
xc=repmat('*',1,x);
fprintf('%+*s\n',20,xc);
end
end
I would want the "20" (shown in bold) to be fixed as the initial "x". And not to increment with the recursion occuring.
Thanks

回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by