I don't understand where I'm going wrong

조회 수: 2 (최근 30일)
Shruti
Shruti 2024년 1월 26일
편집: Stephen23 2024년 1월 27일
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
Shruti
Shruti 2024년 1월 27일
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 2024년 1월 27일
편집: Stephen23 2024년 1월 27일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sai Teja G
Sai Teja G 2024년 1월 26일
편집: Sai Teja G 2024년 1월 26일
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
Walter Roberson
Walter Roberson 2024년 1월 27일
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 2024년 1월 27일
I have put else valid=true; end at the end of that loop, so why doesn't that work?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by