I don't understand where I'm going wrong

2 views (last 30 days)
Shruti
Shruti on 26 Jan 2024
Edited: Stephen23 on 27 Jan 2024
function valid=valid_date(year,month,day)
if nargin~=3
valid=false;
end
if ~isscalar(year) || year<1 || year~= fix(year)
valid=false;
elseif ~isscalar(month) || month<1 || month~= fix(month) || month>12
valid=false;
elseif ~isscalar(day) || day<1 || day~= fix(day) || month>31
valid=false;
end
% leap year
if mod(year,4)==0 && mod(year,100)~= 0 || mod(year,400)== 0
if month==2
if ~(1<=day&&day<=29)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
end
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
  4 Comments
Shruti
Shruti on 27 Jan 2024
hello! below is the assignment question.
Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false. The name of the output argument is valid. If any of the inputs is not a positive integer scalar, return false as well. Note that every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100. However, years that are exactly divisible by 400 are also leap years. For example, the year 1900 was not leap year, but the year 2000 was. Note that your solution must not contain any of the date related built-in MATLAB functions.
Stephen23
Stephen23 on 27 Jan 2024
Edited: Stephen23 on 27 Jan 2024
Note that || is a bivariate operator: it is a function with exactly two inputs and has exactly one output. You cannot chain it into arbitrarily long lists of values and expect it to operate on all of the values simultaneously. So your code:
(1||3||5||7||8||10||12)
is exactly equivalent to writing
((((((1||3)||5)||7)||8)||10)||12)
which because all of the values are non-zero is basically equivalent to
((((((true||true)||true)||true)||true)||true)||true)
which is exactly equivalent to
true
So your code
month ~= (1||3||5||7||8||10||12)
reduces down to
month ~= true
which because TRUE is equivalent to 1 your code is exactly equivalent to
month ~= 1
In short: your invented syntax does not do what you think it does and will not work.
Tip: use ISMEMBER instead. It works.

Sign in to comment.

Answers (1)

Sai Teja G
Sai Teja G on 26 Jan 2024
Edited: Sai Teja G on 26 Jan 2024
Hi Shruti,
I assume that you are facing the "not enough input arguments" error, it occurs because the function is being executed without supplying the necessary arguments; in other words, no input arguments are provided when the function is called. This is why the error appears during the evaluation of the second "if" condition. To rectify this issue, you can introduce a `return` statement at the end of the first "if" condition. Additionally, it's important to note that the variable `valid` has not been initialized prior to the check for the number of input arguments (`nargin`). If the correct number of arguments is not provided, the variable `valid` will remain undefined, potentially leading to further errors.
function valid=valid_date(year,month,day)
valid=true;
if nargin~=3
valid=false;
return; % exit your code if there are not enough arguments
end
if ~isscalar(year) || year<1 || year~= fix(year)
valid=false;
elseif ~isscalar(month) || month<1 || month~= fix(month) || month>12
valid=false;
elseif ~isscalar(day) || day<1 || day~= fix(day) || month>31
valid=false;
end
% leap year
if mod(year,4)==0 && mod(year,100)~= 0 || mod(year,400)== 0
if month==2
if ~(1<=day&&day<=29)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
end
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
Hope this clarifies your doubt!
  3 Comments
Walter Roberson
Walter Roberson on 27 Jan 2024
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
If the condition does not hold, you do not set valid to true
Shruti
Shruti on 27 Jan 2024
I have put else valid=true; end at the end of that loop, so why doesn't that work?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!