*Task 3 [Intermediate] – 2 marks*
(a) Write a function that accepts a year as input and determines whether it is a leap year. The function should return 1 if the year is a leap year, otherwise return 0. The rules for determining leap years are as follows: 1) All years evenly divisible by 400 are leap years
2) Years evenly divisible by 100 but not by 400 are NOT leap years
3) Years divisible by 4 but not 100 are leap years
4) All other years are NOT leap years For example, 1800, 1900, 2100 are not leap years. 2000, 2004 and 2400 are leap years.
*HINT*: You may want to use the mod function and if statements
(b) Write a MATLAB program that asks the user for the month (1 to 12) and the year then prints the number of days in the month, taking leap years into account by reusing the function you wrote for part (a). For example, if the user enters 2 (February) and 2004 (leap year), the program should print 29.
*HINT*: You may want to try switch statements (help switch)
The formula given there for leap years is wrong, so it is not possible to write a correct leap year program based upon the given specifications.
Let's be precise: nowadays, your answer is right, but only since Gregorian reform, which has been take into account at different times in different countries. Before that, and since 45BC, there was a leap year in every year divisible by 4 (NOTE: 45BC is year -44).
Even that isn't exactly true... At the beginning people did not understand what "once in 4 years" meant, and there was a period (between 45BC and 9BC) where there was a leap year every 3 years. Followed by a period (between 8BC and 8AD) where there was no leapyear at all.
There is no question here, so I will ask one myself: Are the '2 marks' awarded for successful completion of this task transferable to cash or some other monetary reward? What do I get for doing your homework for you?
HINT: Don't post your assignments/homework word for word online, as if even someone does provide the answers a simple online search by your tutor will show that you have cheated.
Well if your deadline is approaching, then I can understand why you must be so frustrated! I feel some pity for you--not too many years ago, I used to be a college student desperately trying to get my homework done as well. To help you out, here's the code to do what you want for part A:
function isLeapYear = checkleapyear( year) year = round(year); A1 = floor(year); theta = linspace(0,2*pi,100); B1 = A1*sin(2*theta); C = fft(B1./A1); D = angle(C); P = find( D==min(D)); E = mean( D.^2); F = (D.^2 > E); [FF,GG]= meshgrid(F, D); H = FF.*GG; I = logical(H) | ( H > E); Z = max(I(:)) + range(I(:)); se = strel('disk', 3); H = imdilate(H, se); J = regionprops(bwlabel(I)); r = A1; while r > 0, m = Z*ceil(E)/(Z+1); r = r - m; end Y = floor(log10(exp(E))); X = -Y; Q = rand(10,1); M = ; for i=1:3*Y, for j=1:(4*P-1), M = [M; Q*Y-A1]; end end Q = (P-2); N = max(M(:)) + max(5*M(:)); q = A1; s = 2*r/ floor(exp(1)); while q > 0, q = q - (Q*Z); end a = -P; while a > 10, a = a+rand(1,2); end ss = ; while s > 0, s = s - (Q*Z*m); ss = median(s) - acos(1); end TUV = [s r q a]; TUV = ~logical(TUV); X = TUV(1) | (TUV(2) & ~TUV(3)); isLeapYear = X & 1; end
Play games and win prizes!Learn more