CODE IN MATLAB Complete the following prompts: Ask a user to input a number betw
ID: 3915406 • Letter: C
Question
CODE IN MATLAB
Complete the following prompts: Ask a user to input a number between 20 and 80. Validate the data and if it is outside the range have the user input another number. Repeat this until a valid number is input. a) b) Create a 5x5 array of random integer numbers. Use loops to display every element of the array in any order one element at a time c) Display every other number between 1 and 20 using only a single for loop and a single disp function (see below). You can only modify the expression and the variable. Everything else MUST remain the same, for (expression) disp (variable) The WILLIAM STATES LEE COLLEGE of ENGI endExplanation / Answer
num = input("Enter a number between 20 and 80: ");
while num <20 || num >80
num = input(" Invalid Number. Enter a number between 20 and 80: ");
end
%{ SAMPLE OUTPUT
Enter a number between 20 and 80: 10
Invalid Number.
Enter a number between 20 and 80: 90
Invalid Number.
Enter a number between 20 and 80: 56
}%
r = rand(5);
% LOOPS through rows
for i = 1:5
for j = 1:5 % loops through columns
fprintf("%d ",r(i, j))
end
fprintf(" ")
end
%{ SAMPLE OUTPUT
0.0700739 0.949811 0.401248 0.88412 0.774244
0.374714 0.916272 0.859212 0.751385 0.502945
0.530722 0.249074 0.544644 0.857053 0.141467
0.947237 0.555626 0.927831 0.0578718 0.358159
0.552434 0.0538467 0.321996 0.448949 0.781077
}%
% this loops from 1 to 20 and increments by 2 each time
for num = 1:2:20
disp(num)
end
%{
1
3
5
7
9
11
13
15
17
19
}%
num = input("Enter a number between 20 and 80: ");
while num <20 || num >80
num = input(" Invalid Number. Enter a number between 20 and 80: ");
end
%{ SAMPLE OUTPUT
Enter a number between 20 and 80: 10
Invalid Number.
Enter a number between 20 and 80: 90
Invalid Number.
Enter a number between 20 and 80: 56
}%
r = rand(5);
% LOOPS through rows
for i = 1:5
for j = 1:5 % loops through columns
fprintf("%d ",r(i, j))
end
fprintf(" ")
end
%{ SAMPLE OUTPUT
0.0700739 0.949811 0.401248 0.88412 0.774244
0.374714 0.916272 0.859212 0.751385 0.502945
0.530722 0.249074 0.544644 0.857053 0.141467
0.947237 0.555626 0.927831 0.0578718 0.358159
0.552434 0.0538467 0.321996 0.448949 0.781077
}%
% this loops from 1 to 20 and increments by 2 each time
for num = 1:2:20
disp(num)
end
%{
1
3
5
7
9
11
13
15
17
19
}%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.