In: Advanced Math
The R-rule for oysters is that you should only eat them in months that contain the letter R. Assuming that you like to get oysters on Saturday evenings with your friends write a program that uses the year as an input to determine how many times in that year you can get an oyster dinner and make a list of those dates for that year. Do this in a fully automated way and include a consideration of leap-years. In other words use logical statements to determine if the month has an R in it. Hint: my program is not that long, less than 20 lines, but it uses a separate function that I wrote called isleapyear, that is just a few lines. Also, I changed to format of datestr so that the month is fully spelled out. (Complete in MATLAB)
Required Matlab code with explanatory comments is given below:
function [notimes] = Q_eatoyster(Year)
pattern='r'; %pattern to check
months=["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"];
TR=contains(months,pattern);
if not(mod(Year,4)==0) || (mod(Year,100)==0 &&
not(mod(Year,400)==0))
nodays=[31,28,31,30,31,30,31,31,30,31,30,31];
notimes=sum(nodays.*TR);
else %if it is a leap year
nodays=[31,29,31,30,31,30,31,31,30,31,30,31];
%number of days in Feb changes, no other changes
notimes=sum(nodays.*TR);
end
Sample usages:
Hope this was helpful. Please do leave a positive rating if you liked this answer. Thanks and have a good day!