I made a recursive code to print a tree but the tree is printed backwards

function Affarbre2(n,N)
if n>0
for j = 1:(N+2)
if (abs(N-j))<n
fprintf('*');
else
fprintf('
end
end
disp(' ')
Affarbre2(n-1,N)
end

回答(1 个)

Affarbre_right(5,5)
***** **** *** ** *
Affarbre_left(5,5)
***** **** *** ** *
function Affarbre_right(n,N)
if n > 0
for j = 1:(N+2)
if abs(N+2-j) < n
fprintf('*');
else
fprintf(' ');
end
end
% disp(' ')
fprintf('\n');
Affarbre_right(n-1,N);
end
end
function Affarbre_left(n,N)
if n > 0
for j = 1:(N+2)
if abs(1-j) < n
fprintf('*');
else
fprintf(' ');
end
end
% disp(' ')
fprintf('\n');
Affarbre_left(n-1,N);
end
end

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

标签

回答:

2022-3-4

Community Treasure Hunt

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

Start Hunting!

Translated by