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
정보
This question is locked. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
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
댓글 수: 16
Abhishek singh
2019년 3월 22일
Matt J
2019년 3월 22일
Not me.
If you are getting an error then post the error message, otherwise how are you expecting people to help?
Also show how you are calling the function since that is rather fundamental to what the result of it will/should be.
In future please put your question in the body of the question. The title is supposed to be a summary, not a truncated version of a pasted homework question!
Geoff Hayes
2019년 3월 22일
Abhishek - why is limit being set to 21? It is also the second input parameter so whatever you are passing in (if you are doing so) will be overwritten by 21.
Abhishek singh
2019년 3월 23일
John D'Errico
2019년 3월 23일
편집: John D'Errico
2019년 3월 23일
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.
Abhishek singh
2019년 3월 23일
Narendran S
2020년 5월 30일
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
2020년 5월 30일
No, this always uses 21 as the limit no matter what the user passes in.
Rik
2020년 8월 21일
Comment posted as answer by Natarajan M.A:
i have a question,i am a biggner and new to matlab.why do we put( nargin < 2) and (nargout == 2) in all of these programs.
Rik
2020년 8월 21일
Have you read the documentation for nargin? One of the requirements of this program is to work for either 1 or 2 inputs, so you need a way to distinguish those.
Shagun Agarwal
2020년 8월 29일
function too_young=under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin==2 && age<limit
too_young=true;
else
too_young=false;
end
The above code is giving a wrong output at age=20, can someone please help me out in finding the mistake.
Rik
2020년 8월 29일
Did you step through the code line by line? Then you will notice that that it is possible for the second to last line to run when it shouldn't. It also helps to use the smart indent to see the nesting:
function too_young=under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin==2 && age<limit
too_young=true;
else
too_young=false;
end
It is also generally a bad idea to repeat code. If you remove the repetition you will probably clearly see where your mistake is.
ANSHUMAN SARTHAK MEHER
2020년 10월 17일
function too_young = under_age(age,limit)
if nargin==1
limit=21;
end
if age<limit
too_young = true;
else too_young = false;
end
%might be this is the shortest
Jan
2022년 4월 4일
This has become a strange thread. We find a pile of suboptimal implementations.
We need 6 lines of code (including the trailing "end"). John has condensed the IF block into 1 line, but without doubt his implementation is clean and exhaustive.
So what is the reason to post a lot of other less elegant and not working versions?
Many beginners suggest:
if limit > age
too_young = true;
else
too_young = false;
end
instead of the compact and efficient:
too_young = limit > age;
Sometimes when clearing out tangled dry underbrush, one is overcome with a profound appreciation for the simple elegance of fire. I think I'd better take a break.
If future me (or anyone else) wants to pick up where I left off, here are my notes on a subset of the answers which very closely follow a common form:
378292 the first of the type, has extra return statement, inconsistent indentation
380788 has extraneous ()
427771 randomly indented
428601 indented, but otherwise identical to 427771
423954 all variable names are replaced with letters
442503 has extra isempty() test, logic is flipped but correct
446375 identical to 428601 excepting whitespace (includes console dump)
477187 identical to 428601 excepting whitespace
477322 trying too hard to say "it's totally different"
478066 identical to 428601 excepting whitespace and unused code as comments
1074038 identical to 428601 excepting whitespace and a comment that says "the shortest way"
EDIT: these also constitute a set of trivial variations of a common answer:
Find the ones that look the most like copypasta, see if user posted other answers, find more threads full of samey copypasta, fall down rabbit hole.
채택된 답변
추가 답변 (26개)
Saurabh Kumar
2019년 3월 28일
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 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
댓글 수: 7
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
Andres
2020년 10월 14일
Great question jajajajaj Excellent answer!
Quyen Le
2021년 6월 20일
Hello, why when i put the first arguement like that it wouldn't go through?
function too_young = under_age(age, limit)
if age < limit;
too_young = true
if nargin < 2;
limit = 21;
end
else too_young = false
end
Thank you so much. I wonder why?
Quyen Le
2021년 6월 20일
Ofc I tried your answer and it worked. but i would want to understand clearly
Walter Roberson
2021년 6월 20일
MATLAB (and nearly all programming languages... but not all) are Procedural Languages, in which the order of statements is important.
Suppose you were climbing a ladder. For the most part, you take one step upward at a time. Now suppose you program it that way,
"Move foot 50 cm higher and put weight on it"
But what about when you get to the top?
"Move foot 50 cm higher and put weight on it"
"If there was no higher rung, don't take that step"
Too late. You already put your weight in mid-air before checking whether there was something to put your weight onto.
You instead need a check like
"If there is a higher rung, move foot 50 cm higher and put weight on it"
Check first, before relying on it being there.
The way you coded
if age < limit
if nargin < 2
limit = 21
end
end
is similar to not having checked for a higher rung existing before putting your weight where it would be.
===
It is better programming practice to check for exceptions first, check to see whether a calculation is going to be valid before doing the calculation.
However, there are some situations in which it is valid to "patch up" if you encounter an exception. For example, better programming practice for "1/x if x is not 0, 0 if x is 0" would be
y = zeros(size(x));
mask = x ~= 0;
y(mask) = 1./x(mask);
This code never does any division by 0.
But if you are sure you are using IEEE 754, you can count on the fact that IEEE 754 defines that 1/x has some result for 1/0 (rather than crashing the program), and you can instead do a patch-up-later version:
y = 1./x;
y(x == 0) = 0;
The patch-up-later version of the question would be like,
function too_young = under_age(age, limit)
too_young = age < 21;
if nargin > 1
too_young = age < limit;
end
end
Notice that the variable limit is not used until after it has been checked to be sure that it is present.
Astr
2019년 9월 8일
function too_young = under_age(age, limit)
if nargin == 1
limit = 21;
end
too_young = gt(limit, age);
댓글 수: 0
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
댓글 수: 5
Ajith Thomas
2019년 6월 20일
편집: 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 :)
Karina Medina Barzola
2021년 6월 3일
nargin is a variable? if i change it to an another variable, it sent me an error. what nargin is. Thanks
help nargin
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
Ajith Thomas
2019년 6월 29일
thank you nijita :)
Chech Joseph
2019년 9월 5일
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

Chech Joseph
2019년 9월 5일
Never mind. Its resolved. Used <= instead of just < operator
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일
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일
its taking age >= limit as older
.......and ......age<limit as young
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
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일
correct but how u do it
Sai Hitesh Gorantla
2020년 2월 1일
편집: Rik
2020년 6월 17일
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
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
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.
Mir Mahim
2020년 5월 7일
function a = under_age(age,limit)
if nargin < 2
limit = 21;
end
a = age < limit;
end
댓글 수: 0
Aasma Shaikh
2020년 5월 26일
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
댓글 수: 1
DGM
2023년 2월 21일
Returns a false result regardless of the age if three arguments are passed.
jaya shankar veeramalla
2020년 5월 29일
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
AYUSH MISHRA
2020년 6월 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
SOLUTION ;
under_age(18,18)
ans =
logical
0
under_age(20)
ans =
logical
1
댓글 수: 0
Keshav Patel
2020년 6월 8일
function too_young =under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin ==2
if age<limit
too_young=true;
else
too_young=false;
end
end
댓글 수: 1
DGM
2023년 2월 21일
This is essentially identical to @Saurabh Kumar's answer, except one of the variable names has been changed.
saurav Tiwari
2020년 6월 17일
편집: Rik
2020년 6월 17일
function [a]=under_age(n,m)
a=n<m;
if a==1;
fprintf('true')
end
if nargin<2;
m=21;
end
end
댓글 수: 2
saurav Tiwari
2020년 6월 17일
awesome
Rik
2020년 6월 17일
Why did you decide to put the part with nargin at the end?
AKASH SHELKE
2020년 8월 9일
편집: AKASH SHELKE
2020년 8월 9일
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
Muhammad Akmal Afibuddin Putra
2020년 8월 10일
function too_young = under_age(age,limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young = 1 == 1;
else
too_young = 1 ==2;
end
end
댓글 수: 3
saurav Tiwari
2020년 8월 10일
Cool bro
Rik
2020년 8월 10일
That's a creative way to write true and false.
But why did you decide to post it? It doesn't seem to show a new strategy to solve this problem. Note that Answers is not a homework submission service.
Walter Roberson
2020년 8월 10일
too_young = 1 == 1;
You should
doc true
doc false
Omkar Gharat
2020년 8월 11일
function [too_young] = under_age(age,limit);
% age = input('Enter age of applicant : ')
% limit = input('Enter age limit of applicant : ')
if nargin < 2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end
end
This is my code .And it is 100% correct
댓글 수: 1
Madan Kc
2020년 10월 2일
no its not correct for under_age(20)
Khom Raj Thapa Magar
2020년 9월 6일
Make sure your indentation is correct while coding
function too_young = under_age(age,limit)
if nargin<2
limit = 21;
if age < limit
too_young = true;
else
too_young = false;
end
end
if nargin == 2
if age < limit
too_young = true;
else
too_young = false;
end
end
Calling functions
>> too_young = under_age(18,18)
>> too_young = under_age(20)
Output:
too_young =
logical
0
too_young =
logical
1
댓글 수: 0
Jessica Trehan
2020년 9월 21일
function too_young=under_age(age,limit)
if nargin<2
limit=21;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin==2
if age<limit
too_young=true;
else
too_young=false;
end
end
%THE PERFECT CODE
댓글 수: 7
Madan Kc
2020년 10월 2일
no not for under_age(20)
Rik
2020년 10월 2일
It looks like it works. What line do you think is causing the problem?
Also, if you plan to cheat, you shouldn't complain about how some answers don't work.
Olha Skurikhina
2021년 1월 8일
편집: Olha Skurikhina
2021년 1월 9일
function too_young = under_age(age,limit)
if nargin<2
limit=21
end
too_young = (age>=limit)
end
It should work and it works correctbut still assesment_tool says that "too_young has an incorrect value". I can`t think of idea why.
Rik
2021년 1월 8일
Look at you code. What happens if age is 40 and limit is 21? What do you expect age>=limit to return?
Olha Skurikhina
2021년 1월 11일
I expect the value of too_young to be logical 1 and it gives the logical one when I run it in Matlab.
So I do not see why it is an incorrect value.
Rik
2021년 1월 11일
The variable name is 'too_young'. Your code is claiming that 40 is too young if the age limit is 21. Either the variable name is misleading, or your comparator is the wrong way around.
Olha Skurikhina
2021년 1월 11일
Thank you. It is very stupid. I already solved harder problems along the course but this one couldn`t tackle. That was my problem and plus if the age= limit, it is still should return false so '<=' is incorrect.
amjad ali
2021년 9월 6일
function too_young=under_age(age,limit);
switch nargin
case 1
limit=21;
end
if (limit>age);
too_young=true;
else
too_young=false;
end
switch nargin
case 2
end
if limit>age ;
too_young=true;
else
too_young=false;
end
댓글 수: 2
Walter Roberson
2021년 9월 6일
This code fails if nargin is 0 .
This code tests the age against the limit twice, and has a useless second swtich statement that does nothing.
amjad ali
2021년 9월 6일
thankx for your comment
i have no idea before that the second switch statement is useless,
VIGNESH B S
2021년 10월 13일
function [too_young] = under_age(age,limit)
if nargin == 2
too_young = compare(age,limit);
elseif nargin == 1
too_young = compare(age,21);
end
end
function [answer] = compare(x,y)
if x<y
answer = (1>0);
else
answer = (0>1);
end
end
댓글 수: 3
Walter Roberson
2021년 10월 13일
answer = (1>0)
That tells us you already know that comparisons return a value.
What is the reasoning for not just using the value of x<y ?
VIGNESH B S
2021년 10월 14일
answer = 1>0 is same as declaring true .... the question too asks us about returning either true or false base on some condition... also i wanted to have functions in it to look a bit elegant.. so i just declared other function and did calculations!! thanks all no more than that ..
Image Analyst
2021년 10월 14일
But it doesn't look elegant. An elegant program would do
answer = x < y;
instead of the clunky
if x<y
answer = (1>0);
else
answer = (0>1);
end
Alexandar
2022년 6월 29일
This worked well for me. Very short but I sort of don't understand why the "nargout" part worked.
function too_young = under_age(age, limit)
if nargin < 2
limit = 21
end
too_young = age<limit;
if nargout == 2
too_young = true(1);
end
댓글 수: 4
Rik
2022년 6월 29일
If you don't understand what nargout does, why did you use it?
That line will also never execute, as the number of output arguments will never be 2.
Alexandar
2022년 6월 29일
function too_young = under_age(age, limit)
if nargin < 2
limit = 21
end
too_young = age<limit;
if too_young == true(1);
else too_young == false(0);
end
Somehow what I had before worked, but I changed it to this since it makes much for sense because I only have one output.
Rik
2022년 6월 29일
Why did you add those last lines? Your output is already a logical. Also, false(0) will create an empty array.
And the double = is a comparison. For the if branch that means no code is executed (and the ; is superfluous), and for the else branch that means the too_young variable is compared to an empty array. Since those aren't equal, the result of that is false. That result is not assigned to a variable, so the ; prevents you from seeing it is stored in ans.
Are you using Matlab itself to write this code? There should be several mlint warnings.
Muhammad
2022년 7월 23일
%Help required
Error showing=Output argument "x" (and possibly others) not assigned a value in the execution with "under_age" function.
function x = under_age(age,limit)
if nargin < 2
limit=21;
if age < limit
x=true;
end
end
if nargin==2
if age<limit
x=true;
end
end
댓글 수: 3
Walter Roberson
2022년 7월 23일
편집: Walter Roberson
2022년 7월 23일
What will your code return if the age is more than the limit? Where do you define your x when the if fails?
Muhammad
2022년 7월 23일
In the statement above it's not asked for when x fails.That's why I never made statement when x fails. It simply shows nothing on command prompt when x fails.
Help me out and thanks for your response!
Muhammad
2022년 7월 23일
function too_young = under_age(age,limit)
if nargin < 2
limit=21;
if age < limit
too_young=true;
else
too_young=false;
end
end
if nargin==2
if age<limit
too_young=true;
else
too_young=false;
end
end
Thanks a lot Mr.Walter Roberson
MURSHED SK
2022년 10월 13일
편집: MURSHED SK
2022년 10월 13일
This might help:
function too_young = under_age(age, limit)
if nargin <2
limit =21;
end
if age<limit
too_young = true;
else
too_young = false;
end
% the shortest way
댓글 수: 1
DGM
2022년 10월 13일
Again, how is this different from the many existing answers?
Also, this isn't the shortest way. John's answer at the top of the thread is shorter, and is thoroughly explained.
JASON
2023년 12월 4일
function too_young=under_age(age,limit)
if nargin<2
limit = 21 ;
if age<limit
too_young=true;
else
too_young=false;
end
end
if nargin>1
if age < limit
too_young=true;
else
too_young=false;
end
end
% how about this ?
댓글 수: 2
Walter Roberson
2023년 12월 4일
How does this differ from https://www.mathworks.com/matlabcentral/answers/451726-write-a-function-called-under_age-that-takes-two-positive-integer-scalar-arguments-age-that-repres#comment_2280620 other than the fact you tested nargin>1 instead of nargin==2 ?
DGM
2023년 12월 4일
This is the sixth duplicate of 367872. Trivial changes to variable names, spacing, or contextually-equivalent logical tests don't constitute new information.
Added to the notes I posted above. If I don't prune this sooner, it'll get pruned whenever I decide to clean up the thread -- whenever that might be.
This question is locked.
카테고리
도움말 센터 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!.png)
