I am having a problem in solving a coursera assignment can you please help me

2 views (last 30 days)
The problem is as such: variable input function
Function called should take two arguments age and year,The function must return true if age is less than the limit. If limit is not mentioned it must have default as 21.Output argument name is too_young.
I have wrote a code can you please verify it,
function too_young=under_age(age,limit)
if nargin<2
limit=18;
end
if limit<18
too_young=true;
else
too_young=false;
end
Function call is as such:
too_young = under_age(18,18)
too_young = under_age(20)
I got the errors as such:
correct for 18 vs 18
Assessment result: incorrect20
Variable too_young has an incorrect value.
Assessment result: correct22
Assessment result: incorrect17 vs 18
Variable too_young has an incorrect value.
Assessment result: incorrect13 vs 12
Variable too_young has an incorrect value.
Assessment result: incorrectRandom tests
Variable too_young has an incorrect value.
under_age(21, 23) failed ...

Accepted Answer

Geoff Hayes
Geoff Hayes on 24 Apr 2020
Nikhil - Iook closely at this code
if limit<18
too_young=true;
else
too_young=false;
end
Why isn't age being used? What happens if the user has passed in a valid limit and now you are ignoring it by using 18? Try using the MATLAB debugger to step through the code and understand why the condition for your if statement is incorrect.

More Answers (2)

HARSHA VARDHAN
HARSHA VARDHAN on 10 Dec 2020
function too_young=under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young=true;
elseif age>=limit
too_young=false;
end

Nourhan
Nourhan on 10 Sep 2024
Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example:
>> A = randi(100,3,4)
A =
66 94 75 18
4 68 40 71
85 76 66 4
>> [x, y] = minimax(A)
x =
76 67 81
y =
90
[mmr, mmm] = minimax([1:4;5:8;9:12])

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!