Error for the Code is :
Operands to the || and && operators must be convertible to logical scalar values.
Error in valid_date (line 4)
if month==2 & day<=29 || month==[4,6,9,11] & day<=30 || month==[1,3,5,7,8,10,12] & day<=31
Where the Code is :
function valid = valid_date(year,month,day)
if isscalar(year) && year>0 && isscalar(month) && month<=12 && isscalar(day) && day>0
if mod(year,400)==0 || mod(year,100)~=0 && mod(year,4)==0
if month==2 & day<=29 || month==[4,6,9,11] & day<=30 || month==[1,3,5,7,8,10,12] & day<=31
valid = true;
elseif month==2 & day>29 || month==[4,6,9,11] & day>30 || month==[1,3,5,7,8,10,12] & day>31
valid = false;
end
else
valid = false;
end
else
valid = false;
end

 採用された回答

Rik
Rik 2020 年 5 月 27 日

0 投票

You are close, but you forgot that the code below returns an array.
month==[4,6,9,11]
Use parantheses to group your coditions. That should also make it more clear that you don't need that elseif.
Consider create a variable to store the number of days in Feb. Currently your code will reject all non-leap years.
Also, you should replace all & and | by && and || as well.

4 件のコメント

Akash Pawar
Akash Pawar 2020 年 5 月 27 日
Thank you Rik, I have made chages accordingly as you suggested and code runs successfully for non leap year but when we enter a leap year same error occurs. What more changes should I do in this code...
function valid = valid_date(year,month,day)
if (isscalar(year) && year>0) && (isscalar(month) && month<=12 && month>0) && (isscalar(day) && day>0)
if mod(year,400)==0 || (mod(year,100)~=0 && mod(year,4)==0 )
if (month== 2 && day<=29) || (month==[4,6,9,11] && day<=30) || (month==[1,3,5,7,8,10,12] && day<=31)
valid = true;
else
valid = false;
end
else
valid = false;
end
else
valid = false;
end
Error occuring is :
Operands to the || and && operators must be convertible to logical scalar values.
Error in valid_date (line 4)
if (month==2 && day<=29) || (month==[4,6,9,11] && day<=30) ||
(month==[1,3,5,7,8,10,12] && day<=31)
Rik
Rik 2020 年 5 月 27 日
You still haven't made those month comparisons scalar. You should look into the any function.
You also ignored my remark about February. Your current code will say all non-leap year dates are not valid.
Akash Pawar
Akash Pawar 2020 年 5 月 31 日
Hi Rik, I have made some changes in my code and it works correctly for both Leap year and Non-Leap Year but in Coursera it shows a wrong code for certain condition like for valid = valid_date(2018,4,1) my code shows Logical 0 but even though Coursera says 'Invalid solution....'
Code :
function valid = valid_date(year,month,day)
fc = (month==2 && day<=29);
m_thirty = (month == 4||6||9||11 && day<=30);
m_thirty_one = (month == 1||3||5||7||8||10||12 && day<=31);
if (isscalar(year) && year>0) && (isscalar(month) && month<=12 && month>0) && (isscalar(day) && day>0)
if mod(year,400)==0 || (mod(year,100)~=0 && mod(year,4)==0)
if fc==1 || m_thirty==1 || m_thirty_one==1
valid = true;
else
valid = false;
end
else
valid = false;
end
else
valid = false;
end
Rik
Rik 2020 年 5 月 31 日
Those or statements don't do what you think they are doing. Your previous solution was closer, you only had to find a function that would check if any of the elements was true (hint: use the any function on that vector).
You should first try your code line by line. Is the output of every function what you expect? For the example you give, there is a valid date, but your code returns 0. Can you see why?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 5 月 27 日

コメント済み:

Rik
2020 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by