Can you guys help write a code for my machine problem

7 Ansichten (letzte 30 Tage)
Sean Russell Basaen
Sean Russell Basaen am 8 Mai 2021
Bearbeitet: DGM am 8 Mai 2021
Write a script that will prompt the user to input a number from 1 – 9 only and display the
figure below.
  3 Kommentare
Sean Russell Basaen
Sean Russell Basaen am 8 Mai 2021
I deleted it i thought i asked the wrong problem
DGM
DGM am 8 Mai 2021
Bearbeitet: DGM am 8 Mai 2021
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 8 Mai 2021
Bearbeitet: DGM am 8 Mai 2021
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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by