Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS240 Lab Exercise 07 Due: In Lab, the week of March 13th Main topics: User Defi

ID: 3601663 • Letter: C

Question

CS240 Lab Exercise 07 Due: In Lab, the week of March 13th Main topics: User Defined Functions Function m files 1. Open MATLAB 2. Create a Function file named Lab07 (a) Right click Current Folder title of that window (b) Select New File by moving mouse to that option (c) Click on Function in the pop-up menu (d) Type Lab07 in the highlight field and hit the enter key 3. Double click the Lab07.m entry (that you just created) in the Current Folder window 4. We have seen that we can generate a random number in the range [1,6 to represent the roll of a six sided die, by using: randi [1, 6], 1) Modify the Function file so that it: (a) Takes three such die values as input (b) Determines if those three die values constitute a Straight they can be arranged to form a sequence of consecutive values c)Returns true if they constitute a Straight, and false if not. 5. Create a Function file named Lab07 (a) Right click Current Folder title of that window (b) Select New File by moving mouse to that option c) Click on Script in the pop-up menu (d) Type Driver07 in the highlight field and hit the enter key

Explanation / Answer

Matlab function Lab07.m

function [ OUTVAL ] = Lab07(D1,D2,D3)
%LAB07 Summary of this function goes here
%   Detailed explanation goes here
% Check the difference between two adjusent element of sorted array is [1 1] or not
if( diff(sort([D1,D2,D3])) == [1 1])
    OUTVAL = true; % If it is [1 1] means the elements are consecutive values
else % Otherwise
    OUTVAL = false; %the elements are not consecutive values
end
end


Matlab Script Driver07.m

clc;clear; % Clearing the workspace and command window
D1 = randi([1,6],1);% Roll of first die
D2 = randi([1,6],1);% Roll of second die
D3 = randi([1,6],1);% Roll of third die
[ OUTVAL ] = Lab07(D1,D2,D3); % Calling the function Lab07
% OUTPUT
fprintf('OUTOUT of first die = %d OUTOUT of second die = %d OUTOUT of third die= %d ',D1,D2,D3);
fprintf('OUTPUT from the function Lab07.m = %d ',OUTVAL);
if OUTVAL
    fprintf('%s ','That is the values can be arranges to from a sequence of consecutive values');
else
        fprintf('%s ','That is the values can not be arranges to from a sequence of consecutive values');
end


OUTPUT1

OUTOUT of first die = 5
OUTOUT of second die = 3
OUTOUT of third die= 6
OUTPUT from the function Lab07.m = 0
That is the values can not be arranges to from a sequence of consecutive values

OUTPUT2

OUTOUT of first die = 3
OUTOUT of second die = 1
OUTOUT of third die= 2
OUTPUT from the function Lab07.m = 1
That is the values can be arranges to from a sequence of consecutive values