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

3 件のコメント

DGM
DGM 2021 年 5 月 8 日
What happened to the prior post on this same problem?
Sean Russell Basaen
Sean Russell Basaen 2021 年 5 月 8 日
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 日

0 投票

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 ExchangeLoops 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