Write a script that will prompt the user to input a number from 1 – 9 only and display the
figure below.

댓글 수: 3

What happened to the prior post on this same problem?
I deleted it i thought i asked the wrong problem
DGM
DGM 2021년 5월 8일
편집: DGM 2021년 5월 8일
Nah. I don't think you did. I just wanted to make sure that someone else didn't delete it.
FWIW, when posting things that look like homework, most people here are going to look for some indication that the poster has made an effort to solve the problem at hand. It's not just a matter of proving you did the work; it tells us where you are in the problem solving process and it makes for less redundant discussion.
In the future, just post or attach your current attempt along with your question, even if it's incomplete. Homework questions by themselves get little traction.

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

 채택된 답변

DGM
DGM 2021년 5월 8일
편집: DGM 2021년 5월 8일
Since you've already demonstrated that you have actually made an (almost) working solution of your own, I'll offer this as a more succinct approach. You can avoid needing routines for each case by generating the string programmatically.
a = 5; % use your input command or whatever here
a = min(max(a,1),9); % or handle these cases with an error or something
for b = [a:-1:2 1:a]
outerpad = repmat(' ',[1 9-(a-b+1)]);
if b==a
fprintf('%s%d%s\n',outerpad,b,outerpad)
else
innerpad = repmat(' ',[1 17-(2*numel(outerpad)+2)]);
fprintf('%s%d%s%d%s\n',outerpad,b,innerpad,b,outerpad)
end
end
I don't still have a copy of your original code to tell you specifically how the indexing was wrong. Maybe the indexing here would help as an example.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 5월 8일

편집:

DGM
2021년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by