Can you guys help write a code for my machine problem

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

3 Comments

What happened to the prior post on this same problem?
I deleted it i thought i asked the wrong problem
DGM
DGM on 8 May 2021
Edited: DGM on 8 May 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.

Sign in to comment.

 Accepted Answer

DGM
DGM on 8 May 2021
Edited: DGM on 8 May 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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 8 May 2021

Edited:

DGM
on 8 May 2021

Community Treasure Hunt

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

Start Hunting!