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 日
最新アクティビティ Nijita Kesavan Namboothiri さんによって 回答されました 約9時間前
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

4 件の回答

回答者: 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 日

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.


回答者: mayank ghugretkar 2019 年 6 月 7 日 16:10

this can work too...
A bit compact approach
function too_young = under_age(age, limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young=true;
else
too_young=false;
return
end
end

  2 件のコメント

function too_young=under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young=true;
else age>=limit
too_young=false;
end
if nargin==2
age<limit
too_young=true;
elseif age==limit
too_young=false;
else age>limit
too_young=false;
end
why this code is not working?
its not working for 18 vs 18 and 20
Ajith - look at your second if statement
if nargin==2
age<limit
too_young=true;
Do you really need the nargin check? And the age < limit does nothing. I would just remove this block (the if/elseif/else) and use the one that you have already coded at
if age<limit
too_young=true;
else age>=limit
too_young=false;
end
There should be no reason to use both.

サインイン to comment.



function too_young= under_age(age, limit)
if (nargin<2)
limit=21;
end
if (age<limit)
too_young=true
else
too_young=false
end
end

  0 件のコメント

サインイン to comment.



Translated by