MATLAB Answers

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

1,044 views (last 30 days)
Abhishek singh
Abhishek singh on 22 Mar 2019
Answered: Jessica Trehan on 21 Sep 2020 at 19:29
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

  13 Comments

Rik
Rik on 21 Aug 2020
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
Shagun Agarwal on 29 Aug 2020 at 11:04
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
Rik on 29 Aug 2020 at 13:47
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.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 23 Mar 2019
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.

  5 Comments

Show 2 older comments
Rik
Rik on 5 Aug 2020
Have you read the documentation for the nargin function to see what it does? It returns the n[umber of] arg[uments you provide as] in[put]. So if it is smaller than 2, that means the limit was not provided as an input argument.

Sign in to comment.

More Answers (23)

Saurabh Kumar
Saurabh Kumar on 28 Mar 2019
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 Comments

ARUN KUMAR GUPTA
ARUN KUMAR GUPTA on 4 Oct 2019
function [too_young] = under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young = true;
else too_young = false;
end

Sign in to comment.


mayank ghugretkar
mayank ghugretkar on 7 Jun 2019
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 Comments

Ajith Thomas
Ajith Thomas on 20 Jun 2019
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
Geoff Hayes on 21 Jun 2019
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.

Sign in to comment.


Astr
Astr on 8 Sep 2019
function too_young = under_age(age, limit)
if nargin == 1
limit = 21;
end
too_young = gt(limit, age);

  0 Comments

Sign in to comment.


Nijita Kesavan Namboothiri
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 Comments

Show 3 older comments
Maria Clara Miguel Descendente de Melo Silva
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
Kilaru Venkata Krishna on 2 May 2020
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

Sign in to comment.


Siddharth Joshi
Siddharth Joshi on 23 Apr 2020
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 Comments

Sign in to comment.



Sai Hitesh Gorantla
Sai Hitesh Gorantla on 1 Feb 2020
Edited: Rik on 17 Jun 2020
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 Comments

Sign in to comment.



maha khan
maha khan on 9 Apr 2020
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 Comment

Walter Roberson
Walter Roberson on 9 Apr 2020
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.

Sign in to comment.


Jack Derz
Jack Derz on 3 May 2020
guys how to write their functions anyone can help me?

  6 Comments

Show 3 older comments

Sign in to comment.



MD.Ismail Emon
MD.Ismail Emon on 16 May 2020
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 Comments

Sign in to comment.


Aasma Shaikh
Aasma Shaikh on 26 May 2020
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 Comments

Sign in to comment.


jaya shankar veeramalla
jaya shankar veeramalla on 29 May 2020
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 Comments

Sign in to comment.


AYUSH MISHRA
AYUSH MISHRA on 4 Jun 2020
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 Comments

Sign in to comment.


Keshav Patel
Keshav Patel on 8 Jun 2020
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

  0 Comments

Sign in to comment.


Kulasekaran N
Kulasekaran N on 15 Jun 2020
#Accepted Answer
function too_young = under_age(age,limit)
if nargin < 2
limit = 21;
end
if age < limit
too_young = true;
else
too_young = false;
end

saurav Tiwari
saurav Tiwari on 17 Jun 2020
Edited: Rik on 17 Jun 2020
function [a]=under_age(n,m)
a=n<m;
if a==1;
fprintf('true')
end
if nargin<2;
m=21;
end
end

AKASH SHELKE
AKASH SHELKE on 9 Aug 2020
Edited: AKASH SHELKE on 9 Aug 2020
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 Comments

Sign in to comment.


Muhammad Akmal Afibuddin Putra
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 Comments

Rik
Rik on 10 Aug 2020
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.

Sign in to comment.


Omkar Gharat
Omkar Gharat on 11 Aug 2020
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

  0 Comments

Sign in to comment.


Khom Raj Thapa Magar
Khom Raj Thapa Magar on 6 Sep 2020 at 11:02
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 Comments

Sign in to comment.


Jessica Trehan
Jessica Trehan on 21 Sep 2020 at 19: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
if age<limit
too_young=true;
else
too_young=false;
end
end
%THE PERFECT CODE

  0 Comments

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by