How to write a program to draw a triangle of stars in MATLAB?

조회 수: 92 (최근 30일)
Ali Salim
Ali Salim 2015년 3월 20일
댓글: Muhammad 2022년 10월 17일
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
Stephen23
Stephen23 2017년 9월 20일
편집: Stephen23 2017년 9월 20일
@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 =
*
**
***
****
*****
rohit kumar
rohit kumar 2018년 10월 15일
For r=1:4 Forc=1:r fprintf('*'); end fprintf('\n) end

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2015년 3월 20일
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
Kunal  Kabi
Kunal Kabi 2017년 6월 3일
편집: Kunal Kabi 2017년 6월 3일
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
Jorge Rocha
Jorge Rocha 2017년 6월 5일
But how can I make the third one? (equilateral).

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Konstantinos Sofos
Konstantinos Sofos 2015년 3월 20일
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
Dan Po
Dan Po 2016년 9월 26일
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
Image Analyst 2016년 9월 26일
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
Kunal Kabi 2017년 6월 3일
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

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by