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

2,357 ビュー (過去 30 日間)
Abhishek singh 2019 年 3 月 22 日
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

9 件のコメント

Abhishek singh 2019 年 3 月 23 日
Narendran S 約14時間 前
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
Walter Roberson 約12時間 前
No, this always uses 21 as the limit no matter what the user passes in.

サインイン to comment.

採用された回答

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 件のコメント

John D'Errico 2019 年 3 月 24 日
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
John D'Errico 2019 年 3 月 24 日
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 [].
Abhishek singh 2019 年 3 月 24 日
getting error for 22

サインイン to comment.

その他の回答 (14 件)

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

3 件のコメント

ARUN KUMAR GUPTA 2019 年 10 月 4 日
function [too_young] = under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young = true;
else too_young = false;
end
Rohan Singla 2020 年 4 月 18 日
function [too_young] = under_age(age,limit)
if nargin<2
limit=21;
end
too_young =age<limit
end
Rohan Singla 2020 年 4 月 18 日
why to have a long codes when same function can be performed by small ones

サインイン to comment.

mayank ghugretkar 2019 年 6 月 7 日
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

3 件のコメント

Ajith Thomas 2019 年 6 月 20 日
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
Geoff Hayes 2019 年 6 月 21 日
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.
Ajith Thomas 2019 年 6 月 29 日
thank you goeff :)

サインイン to comment.

Astr 2019 年 9 月 8 日
function too_young = under_age(age, limit)
if nargin == 1
limit = 21;
end
too_young = gt(limit, age);

0 件のコメント

サインイン to comment.

Nijita Kesavan Namboothiri 2019 年 6 月 25 日
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

6 件のコメント

The problem is the less than or equal to operator. It should be only less than. The right resolution is:
function [a too_young] = under_age(age,limit)
a = true;
too_young = true;
if nargin < 2
limit = 21
end
if nargin == 2 && age < limit
a = true;
elseif nargin < 2 && age < limit
a = true;
else
a = false;
end
if nargout == 2 && age < limit
too_young = true;
else
too_young = false;
end
end
Kilaru Venkata Krishna 2020 年 5 月 2 日 15:12
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
Kilaru Venkata Krishna 2020 年 5 月 2 日 15:14
its taking age >= limit as older
.......and ......age<limit as young

サインイン to comment.

Sai Hitesh Gorantla 2020 年 2 月 1 日
function [too_young] = under_age(age,limit)
if nargin == 2
if age<limit
too_young = true;
else
too_young = false;
end
elseif nargin<2
if age<=21
too_young = true;
else
too_young = false;
end
end

0 件のコメント

サインイン to comment.

mohammad elyoussef 2020 年 4 月 4 日
function c = under_age(a,b)
if nargin < 2
b = 21;
end
if b > a
c = true;
else
c = false;
end

0 件のコメント

サインイン to comment.

maha khan 2020 年 4 月 9 日
function [too_young]= under_age(age,limit)
if (nargin < 2) || isempty(limit)
limit = 21;
end
if age>21
too_young=false;
elseif age < limit
too_young=true;
elseif age==age
too_young=false;
elseif age<=21
too_young=true;
elseif age < age
too_young=false;
elseif age<=21
too_young=true;
else
too_young=true;
end

1 件のコメント

Walter Roberson 2020 年 4 月 9 日
Suppose the user passes in a limit of 35, such as testing for eligibility to be President of the United States. And suppose the age passed in is 29. Then if age>21 would be if 29>21 and that would be true, so you would declare too_young=false but clearly the answer should be true: 29 < the specified limit.

サインイン to comment.

Siddharth Joshi 2020 年 4 月 23 日
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
too_young = under_age(18,18)
too_young = under_age(20)
too_young =
logical
0
too_young =
logical
1

0 件のコメント

サインイン to comment.

sudeep lamichhane 2020 年 4 月 27 日
function too_young = under_age(age, limit)
if nargin<2
limit=21;
end
if age < limit
too_young= true;
else
too_young= false;
end

1 件のコメント

Krashank Kulshrestha 2020 年 5 月 14 日 8:46
correct but how u do it

サインイン to comment.

Jack Sadat 2020 年 5 月 3 日 16:25
guys how to write their functions anyone can help me?

6 件のコメント

Jack Sadat 2020 年 5 月 3 日 23:01
do you have time to write its function for me because tomorrow i should submit it.
Walter Roberson 2020 年 5 月 3 日 23:14
No, I never have time to write code for students to submit as their own code. However, I often have time to assist students when they ask questions.
Jack Sadat 2020 年 5 月 3 日 23:29
ok anyway thank you :)
i will manage to do it!

サインイン to comment.

Mir Mahim 2020 年 5 月 7 日 18:25
function a = under_age(age,limit)
if nargin < 2
limit = 21;
end
a = age < limit;
end

0 件のコメント

サインイン to comment.

MD.Ismail Emon 2020 年 5 月 16 日 16:18
function too_young = under_age(age,limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end

0 件のコメント

サインイン to comment.

Aasma Shaikh 2020 年 5 月 26 日 12:16
function too_young= under_age (age, limit)
if nargin<2
limit=21;
if (age<limit)
too_young = true;
else
too_young = false;
end
elseif ((nargin==2) && (age<limit))
too_young = true;
else
too_young = false;
end
end
% Copy, paste and Run

0 件のコメント

サインイン to comment.

jaya shankar veeramalla 2020 年 5 月 29 日 16:56
function too_young = under_age(age,limit)
if (nargin < 2) || isempty(limit)
limit = 21;
end
if age < limit
too_young = true;
else age >= limit
too_young = false;
end

0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

R2017b

Translated by