I made a recursive code to print a tree but the tree is printed backwards
Show older comments
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
Answers (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
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!