Need help on this question

27 次查看(过去 30 天)
Batuhan Yildiz
Batuhan Yildiz 2022-10-29
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 个评论
Walter Roberson
Walter Roberson 2022-10-29
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

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2022-10-29
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.

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by