Need help on this question

조회 수: 98 (최근 30일)
Batuhan Yildiz
Batuhan Yildiz 2022년 10월 29일
댓글: 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
Batuhan Yildiz
Batuhan Yildiz 2022년 10월 29일

댓글을 달려면 로그인하십시오.

답변 (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에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by