How to write a program to draw a triangle of stars in MATLAB?
113 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ali Salim
el 20 de Mzo. de 2015
Comentada: Muhammad
el 17 de Oct. de 2022
I have a exam and don't know how to write a program to draw a triangle of stars, for example :
*
**
***
****
*****
And also flip upside down, for example :
*
**
***
****
*****
And also in the form of isosceles triangle, for example :
*
***
*****
*******
And also in the form of a specific, for example :
*
***
*****
*******
*****
***
*
Please help me!
3 comentarios
Stephen23
el 20 de Sept. de 2017
Editada: Stephen23
el 20 de Sept. de 2017
@Mohammad Hamza: your comment made me laugh. Good work.
I am not sure why so many of the answers rely on nested loops and stacks of ifs. MATLAB code should be beautiful, not written like ugly C++. Like this:
>> char(32+10*tril(ones(5)))
ans =
*
**
***
****
*****
Respuesta aceptada
Image Analyst
el 20 de Mzo. de 2015
Hint:
string = ' '; % 5 spaces.
index1 = 2;
index2 = 4;
string(index1:index2) = '*';
fprintf('%s\n', string);
You need to figure out what index1 and index2 are for the various lines you want to print out.
5 comentarios
Kunal Kabi
el 3 de Jun. de 2017
Editada: Kunal Kabi
el 3 de Jun. de 2017
The program is little bit incorrect . The correct program is:
clc;
clear all;
close all;
n=input('Enter n=');
for i=2:n
for j=1:i-1
fprintf('*');
fprintf('');
end
fprintf('\n');
end
Más respuestas (2)
Konstantinos Sofos
el 20 de Mzo. de 2015
Hi,
For the first
x = [];
for i = 1 : 5
x = strcat(x,'*');
disp(x)
end
for the second
x = [];
for i = 1 : 5
x = strcat(x,'*');
s = [blanks(5-i) x];
disp(s)
end
for the third, the pyramid
x = [];
for i = 1 : 5
x = strcat(x,'*');
s = [blanks(5-i) x];
disp([s fliplr(s)])
end
5 comentarios
Dan Po
el 26 de Sept. de 2016
what is the 'x=[]' for? i tried looking this up and dont know what to search for. right now i am trying to use a for loop to do the same thing, but with symmetry.
x = [];
for i = 1 : 5
x = strcat(x,'*');
s = [blanks(5-i) x];
disp([s fliplr(s)])
end
my code is this, i want to make it shorter:
row=input('enter number: \n');
y=rem(row,2);
if y==0 ;
% fprintf(2,'ERROR: you entered an even number\n')
error('ERROR: %.2f an even number\n',row)
else y>0
for i= 1:row;
for j=1:row-i
fprintf(' ')
end
for k=1:2*i-1
fprintf('*')
end
fprintf('\n')
end
for j=row-1:-1:1
for q=1:row-j
fprintf(' ')
end
for k=2*j-1:-1:1
fprintf('*')
end
fprintf('\n')
end
end
Image Analyst
el 26 de Sept. de 2016
It's to make an x that you can append to. If you don't do that in advance, then what happens when it gets to this line:
x = strcat(x,'*');
It needs to take an already existing x and add a * to it. But if you didn't assign null to x, there is no x to add a star to. So even though x is initialized to null, it's really still there, and we can append stuff to it.
Kunal Kabi
el 3 de Jun. de 2017
For printing in this order
*****
***
**
*
Use this code'
clc;
clear all;
close all;
n=input('Enter n=');
for i=n:-1:2
for j=i-1:-1:1
fprintf('*');
end
fprintf('\n');
end
1 comentario
Muhammad
el 17 de Oct. de 2022
*
**
***
*****
What changes we do for this type of arrangements
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!