Write a function, using arrays-vectors,' input'& 'fprintf','if' statement', 'end', 'fgetl' etc with good programming practices...like putting comments %

1 view (last 30 days)
*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)
  1 Comment
the cyclist
the cyclist on 27 Mar 2012
HINT: You might want to do something other than lazily post your homework, if you want help.
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Mar 2012
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.
  2 Comments
Geoff
Geoff on 27 Mar 2012
But there's something to be said for solving your homework using only the information provided. =)
Walter Roberson
Walter Roberson on 27 Mar 2012
Beh. You don't get to be a world-class honeydipper by taking statements for granted!
http://www.youtube.com/watch?v=s8cVYg7RNx4

Sign in to comment.

More Answers (3)

Matt Kindig
Matt Kindig on 28 Mar 2012
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
  2 Comments
Matt Tearle
Matt Tearle on 28 Mar 2012
It's also just terrible code. You forgot to preallocate M! Not to mention that I is logical, so the call to bwlabel is unnecessary. N00b.
(Everything else is fine, obviously...)

Sign in to comment.


Matt Kindig
Matt Kindig on 27 Mar 2012
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?
  1 Comment
Matt Tearle
Matt Tearle on 27 Mar 2012
Apparently you get 2 Marks. Not sure if German banks will still change those to Euros for you. Either way, it hardly seems worth the effort.

Sign in to comment.


C.J. Harris
C.J. Harris on 27 Mar 2012
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.
  2 Comments
wanchoongchin
wanchoongchin on 27 Mar 2012
WHAT IF YOU ARE STILL CONFUSED AND THE NEXT 2 DAYS IS YOUR DATELINE?
Of course you won't be confused
Walter Roberson
Walter Roberson on 27 Mar 2012
Postings here are "forever", so if next year someone notices that you copied one of your assignments from here, your institution could still go back and yank your course credit (and possibly your degree.)

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!