Drawing this shape using *
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
How to draw This Shape
*
* *
* *
* *
* *
I tried to play around some nested loops and didn't work for me
採用された回答
Star Strider
2019 年 2 月 23 日
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 件のコメント
Kenan Baira
2019 年 2 月 24 日
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
Star Strider
2019 年 2 月 24 日
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.
Kenan Baira
2019 年 2 月 24 日
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
}
}
Star Strider
2019 年 2 月 24 日
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.
Star Strider
2019 年 2 月 24 日
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
Walter Roberson
2019 年 2 月 24 日
What is your current code?
Star Strider
2019 年 2 月 24 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
