MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Write a function called under_age that takes two positive integer scalar arguments: age that represents someone's age, and limit that represents an age limit. The function returns true if the person is younger than the age limit. If the second arg

Abhishek singh さんによって質問されました 2019 年 3 月 22 日
最新アクティビティ Saurabh Kumar さんによって 回答されました 2019 年 3 月 28 日 9:52
function too_young = under_age(age,limit)
limit = 21
if age <= limit
too_young = true;
elseif age >= limit
too_young = false;
else
fprintf('invalid\n')
end

  7 件のコメント

Because you are setting the variable limit to 21 inside the function, it will not matter what value you pass in for limit. limit will ALWAYS be 21. That is not a default that can be overridden as you want to do.
Think about it. Suppose you pass in the number 35 for limit. What will happen?
The very first thing is limit will be replaced by the number 21.
Anyway, if you think you are getting an error, always paste in the COMPLETE ERROR message, everything in red. Show us the complete error text.

サインイン to comment.

タグ

製品


リリース

R2017b

2 件の回答

回答者: John D'Errico
2019 年 3 月 23 日
 採用された回答

This is not an error. It is a failure of your code to work as it is supposed to work by your goals.
What did I say? What has EVERYONE said so far? You cannot set a default as you did.
Even though you pass in the number 18 as the limit, the first thing you do inside is reset that value to 21.
Instead, you need to think about how to test to see if limit was provided at all.

  3 件のコメント

Please use comments. Moved to a comment by Abhishek singh:
function too_young = under_age(age,limit)
if age >= 21 || age == limit
too_young = false;
elseif age < 21 && age < limit
too_young = true;
elseif age > 21 && age > limit
too_young = false;
else
too_young = false;
end
# not getting output for age = 20
If you do not get a result for age = 20, when limit was not provided, then you need to learn about HOW to specify a default for limit. That means you need to provide a new if clause to create the variable limit at the very beginning of your code. They will typically look like this:
if (nargin < 2) || isempty(limit)
limit = 21;
end
Do you see the difference between what you did initially, when you had ONLY the single line limit=21, with out the if clause around it, and this test?
This test only creates the variable limit when limit either does not exist, or when it was provided as an empty input, so as [].

サインイン to comment.


回答者: Saurabh Kumar 2019 年 3 月 28 日 9:52

Write a function called under_age that takes two positive integer scalar arguments:
  1. age that represents someone's age, and
  2. limit that represents an age limit.
The function returns true if the person is younger than the age limit. If the second argument, limit, is not provided, it defaults to 21. You do not need to check that the inputs are positive integer scalars.
function x = under_age(age,limit)
if nargin < 2
limit = 21;
if age < limit
x = true;
else
x = false;
end
end
if nargin == 2
if age < limit
x = true;
else
x = false;
end
end

  0 件のコメント

サインイン to comment.



Translated by