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.
function [valid]=valid_date(year, month, day)
if isscalar(year) && year>0 && year~=0 && isscalar(month) && month>0 && month~=0 && isscalar(day) && day>0 && ar
asad - why do you think there is something wrong with your code? Is there a particular case where it fails? What are the inputs that you are providing to this function? Please provide details.
let me tell you the true story ,i am doing a course from coursera and this is the question from one of its assignment
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.
its a copy code from someone else ,and i dont understand any shit from it ,so please guys help me with this
ar i dont know as i said i copied it from some where else
asad - please further the conversation with comments either here or at Jan's answer...rather than using "answers" to do the same.
As for the missing "end" you will need to show the code that you have now because that which you have posted above seems to have and "end" for each "if" block.
I have attached the code from your original post, modified to have appropriate () for the if statements. It does not have any problem with missing end statements.
The code has a lot of problems, which Jan explained to you, so you will need to improve it. But at least this will get you past the missing END problem.
can't be the same code...when I run the above I get the same failure that Jan indicated below (the undefined ar variable). And when I remove it and put in the brackets like I suggested, I get an answer (which may or may not be valid).
forget everything can anyone tell whats he is asking for ,what its has to do with year divisible by 4,check the example in the question read it .
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.
this code above is not mine ,i copied it from matlab answer ,someone else was also facing the same problem ,its not about runing the code its about a proper code so that your answer gets accepted in assignment ,can you guys tell me a new approuch towards the question?
IN QUESTION IT SAYS every year that is divisible 4 and 400 are leap year ,how should i write this in function give an example ,i think code is of like max 14 lines
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.
SOLUTION IS HERE
function valid = valid_date(year,month,day)
t = (isscalar(year) && isscalar(month) && isscalar(day));
If the year is not a multiple of 4, then you already know that this is not a leap year, so you can skip further examination. If the year is divisible by 4 but is not a multiple of 100, then you can be sure that it is a leap year without further checks. If the year is divisible by 4 and is divisible by 100 then it is a leap year if it is a multiple of 400 and is not a leap year otherwise (so 2000 -> leap year, 2100 -> not, 2200 -> not, 2300 -> not, 2400 -> leap year.)
You have a decent kind of humor, asad jaffar. If you are still confused, although you can find 4 completely working codes in this thread, the best option is to take a break and to drink a cup of coffee! :-)
"just write me the code from A TO Z in order"
sort('A':'Z')
Good luck.
@Users of coursera: Sorry for posting working solutions. After this lengthy discussion I lost my patients. Feel free to write your own code and see my solution only as an alternative for educational purpose.
i live in a place where i cant even ask anyone for help ,i appreciate your help jan ,i have completed almost 20 assignment own my own ,the only reason i am here is because i am facing problem and i dont have anyone to help me ,matlab is something new for me ,its hard for in getting it ,you are seeing things with your own perspective ,its easy for you not for me ,i am trying but i need help.
@asad: Your questions about Matlab are welcome and the topic of this forum. I'm glad if I can help you, but I'm not sure if posting a working solution is a real help, because it might reduce your chances to learn by your own.
Now you see 4 working solutions in this thread and it seems like you have problems with a copy&paste. Therefore a cup of coffee is a serious advice, while sort('A':'Z') is a joke only.
Please provide any information about the failing tests. This would allow for fixing the code. Do not let us guess, what the problems are. Please try to make it as easy as possible to help you. Just stating, that it does not work, is not helpful, most of all if e.g. Oleksandr tells you, that the same code is working for him successfully.
oleksandr is not working for only two questions and if kindly tell me what changes should i make in his code will be beneficial for me ,i dont like to brag but i am in need ,and if you say about learning ,jan everyone has its own ways of learning things ,i will learn after sucessful out .as i said i did other assignment all by myself ,you will not see any other question by me in this forum .just tell me what changes should i make in oleksandr code and where ,explain me easily plz
code of oleksandr is not giving desire answer for two inputs
@asad: What do you expect me to do? I've posted 3 methods which are working correctly on my computer. Oleksandr's version is fine also and it replies the correct answers for him, Walter and me. Therefore I assume, that the problem happens, when you copy&paste the code. You provide the vague description "the last day of every month and random dates" only, but I cannot guess, which test is exactly failing. Maybe the test considers 0 as valid year, or Inf or NaN is provided as input. I do not have any chance to guess this. As long as you do not explain, for which input the posted methods fail, I cannot help you. So please answer:
For which input is the method failing? If coursera does not reveal this detail, there is no chance that I can guess this. Contact the authors and ask for details. Remember, that I cannot even see the output of the coursera and it would be your job to share these information.
See this test:
for k = 2:48000 % 0001-31-01 to 4000-11-30
d = datenum([1, k, 1, 0, 0, 0]) - 1; % Last date of each month
v = datevec(d); % [Year, Month, Day, H, M, S]
if ~valid_date(v(1), v(2), v(3))
disp(v(1:3)); % Display failing dates
end
end
This shows, that Oleksandr's and my suggestion do work correctly for the last days of each month from January 0001 to November 4000, and if you like you can expand the tests. In consequence if the test for "the last day of every month" fails, this must be a problem of the test or you made an error during pasting the working solution.
You make it hard to help you. For me this thread is frustrating. If you do not provide an evendince that there is a problem in the codes, I will consider the problem as solved or not solvable.
i am moving forward doing other assignments by myself ,jan i will do this one in the end ,because it made me stressful ,thanks for the help everyone ,i will be back soon lol we will continue from here ,thanks guys much love to you all .if you guys want to follow me on instagram its (asadjaffar) guy with black wavy hair ,black jacket ,folllow me lol.
Hey Jan, really loved your very precise and explicit code, however I need some more clarifications. Could you please kindly comment out your code especially from the 'isLeap' output argument. ..Thanks
mod(year,400) is 0 if the year is a multiple of 400. ~mod(year,400) is 1 if mod(year,400) is 0 . Thus ~mod(year,400) is true if the year is a multiple of 400, and is the same as mod(year,400) == 0
Sorry, asad jaffar, this is your course. Start with the last paragraph of my answer. Add an equivalent method for the months with 31 days. Then append the check for month==2 and use the value of isleap obtained before.
If I write this code completely for you, you will not learn how to program. So try it at first by your own. If you still have problems, post the code again and ask a specific question.
@asad jaffar: And if I will solve this question for you, you will stuck in the next task also.
I've posted almost the complete solution already, so you can use copy&paste to set it together. All you have to do is to appende this:
...
elseif any(month == [1, 3, 5, 7, 8, 10, 12]
isvalid = (day <= 31);
elseif month == 2
if mod(year,4) == 0 && mod(year, 100)~= 0 || mod(year,400)==0
isvalid = (day <= 29);
else
isvalid = (day <= 28);
end
end
Include the check if all inputs are scalar and >0 on top:
valid = false; % Default value
if ~(isscalar(year) && year > 0 && SAME FOR THE OTHER INPUT)
return;
end
Now you have all you need. 16 lines. Well, add a trailing end, so 17 lines. All you have to do is to copy&paste the code in the right order.
If you really need 14 hours to write this, you need to learn the basics of programming. The forum cannot and will not be a cheap programming service for you.
The please post the code you have created and post the details of the problems. I can neither guess, what your final code is nor which problems it produces. Without knowing any details, I cannot suggest a solution.
looking at the screen capture of your code (you could attach the code itself...) it looks like there are some variables called isvalid rather than valid which is the output parameter (I guess this is for when you are checking for days in February). Have you tried to step through your code - with the debugger - to see why it might be failing for the two example calls that you have posted? Obviously the first date is valid and the second invalid...
i dont understand ,well guys i am doing a course "introduction to matlab' so definitely i am learning it for the first time ,help me out .i am getting agitated now ,my code is running but it is not giving desire results .
hayes assignment generate random non leap years by itself own ,hayes can you kindly to coursera and search for intorduction to matlab and enroll in course its free and then go diretly to week 6 ,open last assignment of week 6 you will understand better then ,about inputs and question.
what is the body of the first if statement in your above code? it looks like you just have an if with a bunch of conditions, no body and then and end. Do these conditions test the validity of your inputs (i.e. positive integers, etc.)? If so, shouldn't the remainder of the code that actually checks the date be the body within this if? So
valid = false;
if all three inputs are positive integer scalars then
jan as i said i cant copy the code it does not allow me to do it,and its the same code you told me to write it down ,jan go to corsera and go to introduction to matlab course ,then go to week 6 last assignment its free and write the code there and run ,you will exactly come to know what problem i am facing if you succed it in running it and got your code acccepted by
walter i know what you said and i followed jan ,now there is no problem of end .its running but out put if not which i need ,i told you guys to go to corsera website and search for introduction to matlab then go to week 6 ,its last assignment of week 6 ,solve it then you guys will know whats the main problem with code ,i know you guys are helping but there are a lot of confusion going on here
I am not referring to the code I posted that fixed the "end" problem. I posted a completely different algorithm for you to follow. Give up on your existing code and write following the outline I showed. It is going to look like
validate that the inputs are positive integer scalars, and that month number is not too much, and return false if they are not
assignment of number of days of month
test for leap year. Assignment of new value to number of days for february if so.
test if the day provided is less than the number of days per month indexed at the month number, and return true if it is
i am giving up , i have tried all ofyou guys code ,they all run but dont give desire result,i have done a lot of assignment but this one is confusing as hell.
Your code probably work (I haven't tested) but you need to learn look-up tables. That many case statements must have been a pain to write and would certainly be a pain to maintain.
Your code using a look-up table
%note that this code, like yours will not work properly with non-integer inputs
Hey Jan, really loved your very precise and explicit code, however I need some more clarifications. Could you please kindly comment out your code especially from the 'isLeap' output argument. ..Thanks
function valid = valid_date(year, month, day)
% scalar positive integer limit
if isscalar(year) && year > 0 && fix(year) == year && ...
The same question I posted on your other answer applies to this one as well: why post a function that doesn't seem to sattisfy the assignment? Several lines cannot be reached, it doesn't ever return an output if the date is invalid, and the return at the end is not needed.
And why did you decide to make 2019 as the last valid year?
So the conclusion is that your code only works for dates that are already valid, which is the thing this function is supposed to test. It will either error or it will return true if your date is not valid, so you can't distinguish valid dates from invalid ones with your implementation.
Comparing a logical value with true replies true if it is true and false otherwise. So this comparison is a waste of time. Simply use the logical value directly. The if can be omitted also.
The structure of the code is not clear. That is probably an important reason why you are unable to find the mistake (if there is one, I haven't run your code).
Make the steps in your program clear. Do one step at a time and write a comment explaining what happens.
In this case the readability will probably improve a lot if you use a vector for the number of days in a month. That will also reduce the chance of typos.
Have a look at what the other functions in this thread are doing. There are several complete working examples, so learn from them.
And next time post your question as a question instead of an answer. You can find guidelines for posting homework on this forum here.
I found the error, I have used date instead of day in line2. Thanks for the input and I also from now on I will follow the guidelines for posting homework.
you are checking for a leap year in an incorrect way. year/4 will not be equal to 0, unless year is 0, in which case that line will not be reached. You are also using leap and nonleap as variables or functions. You should use a logical scalar instead.
This code may indeed work, but why did you decide to post it?
Also, why are you testing if the input is 0? If it is smaller than 1 the code will already return false before.
And why did you copy-paste the code for the leap year switch? Why don't you put an if in there only for February? This code is relatively simple, but for more complex code you will have trouble finding all the places to correct a bug. Use code only once. You could even do that for your input validation:
if nargin < 3
valid = false;
end
if (nargin == 3)
if ~isscalar(year) || year < 1 || year ~= fix(year)
valid=false;
return;
end
if ~isscalar(month) || month < 1 || month ~= fix(month)
21 Comments
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687170
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687170
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687173
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687173
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687182
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687182
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687183
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687183
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687190
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687190
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687230
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687230
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687231
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687231
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687237
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687237
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687240
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687240
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687241
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687241
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687471
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687471
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687474
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687474
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687477
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687477
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687490
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_687490
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_718731
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_718731
SOLUTION IS HERE
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_719161
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_719161
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_719348
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_719348
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_848125
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_848125
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_848134
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_848134
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_951622
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_951622
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_953641
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/453107-how-can-i-solve-this-can-anyone-provide-me-with-code-whats-wrong-with-my-code#comment_953641
Sign in to comment.