Drawing this shape using *
显示 更早的评论
How to draw This Shape
*
* *
* *
* *
* *
I tried to play around some nested loops and didn't work for me
采纳的回答
This takes me back to my FORTRAN days in the late 1960s when line printer code similar to this was the only option for plotting.
Try this:
spc = uint16(' ');
ast = uint16('*');
tringl = char(ones(6,6,'uint8')*32);
for k1 = 2:size(tringl,1)
tringl(k1,[8-k1,4+k1]) = ast;
end
for k1 = 1:size(tringl,1)
fprintf(1, '%s\n', tringl(k1,:))
end
Experiment to get the result you want.
7 个评论
is there by any means a chance that this could be possible using nested loops ? I created a code in C++ tried to convert it line by line to matlab but didn’t fet the correct output
Sure.
Define a matrix of spaces, then in each iteration of the loop insert an asterisk at the correct location, and print the matrix. You can do the same line-by-line, printing each line as you create it, without first creating the matrix.
There are many different ways to do this.
can you show me how to do the line by line thing i'm kinda new to MATLAB I did this on c++ tried to translate it to MATLAB but didn't give the same output
#include <iostream>
using namespace std;
int main()
{
int n = 5;//number of rows
int i, j, k = 0;
for (i = 1; i <= n; i++) // <=n <=5 number of rows is 5 This loop will repeat 5 times for each line print
{
//The loop to Print spaces
for (j = i; j < n; j++) {
cout << " ";
}
//The loop to Print *
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
cout << '*';
else
cout << " ";
k++;
}
k = 0;
cout << endl; // print next row
}
}
I haven’t programmed in any version of C since I began with MATLAB more than two decades ago. Unfortunately, I cannot help you convert your C++ code to MATLAB code.
Kenan Baira’s ‘Answer’ moved here:
i did it myself i know how to create a for loop with starting value and increment and when to stop and while loop with the != which means not equal to made it ~= and all that but it didn’t work at all i got the first line 1 star correct then every other line contains 2 stars touching
Anyways thanks for the help
What is your current code?
My pleasure.
Index the character array to fill stars from the centre to each side, incrementing by 1 in each iteration.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
