Problem 2) Write a MATLAB script that will build a triangle of symbols. This pro
ID: 3602010 • Letter: P
Question
Problem 2) Write a MATLAB script that will build a triangle of symbols. This program should allow for the user to specity the symbol to use, and should accept any character or string value. It should also enable the user to select the height of the triangle built. Finally, the program should enforce the user to only use a single character as the symbol. An example of the execution and output is below. Specify a character to print in your loop: Tx How tall would you like your image? 4 Please input only a single character: Ty Please input only a single character: T Note -it is possible to do this with either for or while loops, but you will need a loop inside of a loop. https://iu.instructure.com courses/1 664789/assignments/7664878#submitExplanation / Answer
% Taking user input
letter = input("Specify a characater to print in your loop: ",'s');
n = input("How tall would you like your image? ");
while numel(letter)!=1
letter = input("Please input only a single character: ",'s');
end
% looping for n times
for i = 1:n
% looping for i times and concatenating characters to line and printing it
line = "";
for j = 1: i
line = strcat(line,letter);
end
disp(line)
end
SAMPLE OUTPUT
Specify a characater to print in your loop: Tx
How tall would you like your image? 4
Please input only a single character: Ty
Please input only a single character: T
T
TT
TTT
TTTT
% Taking user input
letter = input("Specify a characater to print in your loop: ",'s');
n = input("How tall would you like your image? ");
while numel(letter)!=1
letter = input("Please input only a single character: ",'s');
end
% looping for n times
for i = 1:n
% looping for i times and concatenating characters to line and printing it
line = "";
for j = 1: i
line = strcat(line,letter);
end
disp(line)
end
SAMPLE OUTPUT
Specify a characater to print in your loop: Tx
How tall would you like your image? 4
Please input only a single character: Ty
Please input only a single character: T
T
TT
TTT
TTTT
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.