Q:

Write a function that will print a random integer. If no arguments are passed to the function, it will print an integer in the inclusive range from 1 to 100

0

Write a function that will print a random integer. If no arguments are passed to the function, it will print an integer in the inclusive range from 1 to 100. If one argument is passed, it is the max and the integer will be in the inclusive range from 1 to max. If two arguments are passed, they represent the min and max and it will print an integer in the inclusive range from min to max. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

prtran.m

function prtran(varargin)

% Prints a random integer

% If no arguments are passed, the range is random

% otherwise the input argument(s) specify the range

% Format of call: prtran or prtran(maximum)

% or prtran(minimum, maximum)

% Does not return any values

n = nargin; % number of input arguments

% If no argument passed, range is 1-100

if n == 0

 myran = randi([1, 100]);

elseif n == 1

 % one argument is max, range is 1-max

 myran = randi([1 varargin{1}]);

elseif n == 2

 % two arguments are min-max

 myran = randi([varargin{1} varargin{2}]);

end

fprintf('The random integer is %d\n', myran)

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now