1- Create a new script that is called HW9.m and save it. The script will do the following. Test your code for each of the cases and upload your script as a .m file. [15 points]
a. Create a random integer numbers that goes from 0 to 5 and assign it to a variable y using rand and round commands. Hint: Note that rand function only creates random number between 0 and 1. You need to scale the values to get random numbers from 0 to 5 (multiply by 5). You also need to round the generated random numbers to get an integer value. Use round command [5 points]
b. Use a switch-case statement that would switch on y such that it will evaluate the following cases: [10 points]
i. Case 0 → display ‘The random number is 0’
ii. Case 2 → display ‘The random number is 2’
iii. Case 4 → display ‘The random number is 4’
iv. Otherwise → display ‘The random number is odd’
(a). Use the following sequence of commands to obtain the required results in MATLAB:
X=rand(1,5) %generates random array of size
1x5 each lying in the rane of 0 to 1.
p=5*X %Takes the random values of y in the range
of 0 to 5
y=round(p) %Rounds of the given values to nearest
integer.
(b). The switch statement can be used to obtain the required output as follows:
switch y
case 0
disp('The random number is 0.')
case 2
disp('The random number is 2.')
case 4
disp('The random number is 4.')
otherwise
disp('The random number is odd.')
end
Write the above code just after the code of part a. Else you will have to mention the value of y just above this code.
Hope the answer was helpful. Feel free to comment if the problem still persists.
Your feedbacks help us perform better. Please upvote the answer if it was helpful.
Thank you!
Get Answers For Free
Most questions answered within 1 hours.