Need help on this question
24 views (last 30 days)
Show older comments
Code has been provided to define a function named numberChecker that accepts a single input variable named RandomNumber. Add commands to check if the value is between 0 and 10 (consider values of exactly 0 and exactly 10 to be in range). If the value is in this range, assign the value 'The number is in range.' to the output character vector CheckRange. If the value is not in this range, assign the value 'The number is out of range.' to the output character vector CheckRange.
Use an if-else structure in your solution.
Note the variable RandomNumber is defined as an input to the function. Do not overwrite its value in your code.
My Code so far:
function CheckRange = numberChecker(RandomNumber)
% TO-DO: on line 5 add the following, right beside the word "if"
% check that input argument variable for numberChecker is
% between 0 and 10 (including both 0 and 10).
if(RandomNumber>=0 & RandomNumber<=10)
% TO-DO: make the character array below say The number is in range.
CheckRange = 'The number is in range';
else
% TO-DO: make the character array below say The number is out of range.
CheckRange = 'The number is out of range';
end
RandomNumberIn = 1 + 15*rand(1,1) %creates random number to test function
CheckRangeOut = numberChecker(RandomNumberIn)
What's wrong about the code that I don't know how to fix:
Assessment: 1 of 4 Tests Passed
What I got wrong:
- Assessment result: incorrect Does code correctly identify a number that is in range? Variable CheckRange has an incorrect value.Double check spelling and capitalization to ensure the output text is exactly as specified in the problem statement.
- Assessment result: incorrect Does code correctly identify a number that is out of range? Variable CheckRange has an incorrect value.Double check spelling and capitalization to ensure the output text is exactly as specified in the problem statement. Make sure your function will work correctly for negative input values.
- Assessment result: incorrect Does code correctly identify a number that is on the upper or lower boundary of the range? Variable CheckRange0 has an incorrect value. Be sure you have used the correct relational operators to define the range as including the upper and lower boundaries.
2 Comments
Walter Roberson
on 29 Oct 2022
RandomNumberIn = 1 + 15*rand(1,1) %creates random number to test function
CheckRangeOut = numberChecker(RandomNumberIn)
Those two lines do not belong inside of the function.
If you remove them then the code appears to work when I test
Answers (1)
Steven Lord
on 29 Oct 2022
Look very closely at the character vectors you're supposed to assign to the variable. You're missing the periods at the end of the character vectors you assign in your code.
0 Comments
See Also
Categories
Find more on Testing Frameworks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!