Write a function that will receive one input argument, which is an integer n. The function will prompt the user for a number in the range from 1 to n (the actual value of n should be printed in the prompt) and return the user’s input
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:7| Question number:12.7
All Answers
total answers (1)
pickInRange.m
function choice = pickInRange(n)
%Prompts user to enter an integer in the range
% from 1 to the input argument n
%Error-checks to make sure input is in range
% Format of call: pickInRange(n)
% Returns one integer in the range 1-n
prompt = ...
sprintf('Enter an integer in the range from 1 to %d: ',n);
choice = input(prompt);
while choice < 1 || choice > n
disp('Error! Value not in range.')
choice = input(prompt);
end
end
need an explanation for this answer? contact us directly to get an explanation for this answer