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

91 Ansichten (letzte 30 Tage)
Ali Salim
Ali Salim am 20 Mär. 2015
Kommentiert: Muhammad am 17 Okt. 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 Kommentare
Stephen23
Stephen23 am 20 Sep. 2017
Bearbeitet: Stephen23 am 20 Sep. 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 =
*
**
***
****
*****
rohit kumar
rohit kumar am 15 Okt. 2018
For r=1:4 Forc=1:r fprintf('*'); end fprintf('\n) end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Mär. 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 Kommentare
Kunal  Kabi
Kunal Kabi am 3 Jun. 2017
Bearbeitet: Kunal Kabi am 3 Jun. 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
Jorge Rocha
Jorge Rocha am 5 Jun. 2017
But how can I make the third one? (equilateral).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Konstantinos Sofos
Konstantinos Sofos am 20 Mär. 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 Kommentare
Dan Po
Dan Po am 26 Sep. 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
Image Analyst am 26 Sep. 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.

Melden Sie sich an, um zu kommentieren.


Kunal  Kabi
Kunal Kabi am 3 Jun. 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

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by