Q:

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

0

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. The function should error-check to make sure that the user’s input is in the correct range. 

All Answers

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

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now