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일
0 개 추천
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
