My code keeps breaking for a few data input, Please help! The Question is: 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 fa

1 visualizzazione (ultimi 30 giorni)
function valid = valid_date(year, month, day)
valid = false;
if isscalar(year) && year>0 && isscalar(month) && month~=0 && isscalar(day) && day>0
if mod(year,400)==0 || (mod(year,4) == 0 && mod(year,100) ~= 0)
if month==2
if day <= 29
valid = true;
end
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =true;
fprintf('good\n')
end
elseif (month==1||month==3||month==5||month==7||month==9||month==11)
if day<=31
valid = true;
end
else
valid = false;
end
else
if month==2
if day <= 28
valid = true;
end
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =false;
end
elseif (month==1||month==3||month==5||month==7||month==9||month==11)
if day<=31
valid = true;
end
else
valid = false;
end
end
end
end
  1 Commento
Faris Shaikh
Faris Shaikh il 17 Giu 2019
The last day of every month
Variable valid has an incorrect value. valid_date(2014, 8, 31) failed...
Random dates
Variable valid has an incorrect value. valid_date(1399, 8, 31) failed...

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 17 Giu 2019
Modificato: Image Analyst il 17 Giu 2019
Step through it with the debugger, like we all do. If you don't know how, see this link
So when month is 8, and day is 31, what do you think this will return:
elseif (month==4||month==6||month==8||month==10||month==12)
if day<=30
valid = true;
end
if day==31
valid =false;
end
Do you think it will return true or false for "valid"? All those months should have valid true if (day <= 31) && (day >= 1), not day<=30. You don't even need the next if.
elseif (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
if (day >= 1) && (day <= 31)
valid = true;
else
valid =false;
end
Also, I would not name variables year, month, or day since those are built-in functions.
  3 Commenti
Image Analyst
Image Analyst il 17 Giu 2019
Well, I know it does. But I thought that seeing my code would make you realize that you had completely wrong days for many of the months and then you, being a bright engineer, would correct it. Sounds like homework and I'm not sure how much of your homework you're allowed to let others do for your. If you still can't figure it out let me know.
I've gotten ill-advised suggestions from people before but nobody has ever told me to sue them over it ?. Sounds a bit harsh. Maybe just an apology from them would be fine and you could avoid the litigation. ?
Faris Shaikh
Faris Shaikh il 17 Giu 2019
Thank you for your resposnse, it has made me realize how at fault I am and how I need to correct me behaviour. I appreciate that you are still willing to help. I am not a computer science engineer, actuallly studying aerospace instead, and find computer programming difficult. I am better at other classes. Please forgive me for my inappropriate behaviour and thanks for the tips.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by