Kindly help me to find error in attached code to find leap years for a range
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
function leapyr = leapyears(startYear, endYear)
%UNTITLED8 Summary of this function goes here
leapyr = 0;
i = 0;
for i = startYear:4:endYear
if mod(startYear, 4) == 0 && mod(endYear, 4) == 0
fprintf('%i \n', startYear)
elseif mod(startYear, 400) == 0 && mod(endYear, 400) == 0
fprintf('%i \n', startYear)
else mod(startYear, 100) == 0 && mod(endYear, 100) == 0
fprintf('%i \n', startYear)
end
end
end
4 Kommentare
dpb
am 8 Nov. 2020
As written, it requires two input variables -- show us how you called it.
What do you expect/want the results to be? While the above will run, all it's going to produce is a 0 return because the output variable is never updated except for the initialization line...other than that it only prints a line to the command window which isn't going to be very useful as it simply echos the first input variable no matter what.
The logic isn't what is really intended, either, with the "&&" operators.
Unless it's a requirement of the assignment to write the algorithm explicitly, using the shortcut or one similar as shown would be "the MATLAB way" -- using the logical addressing vector to return the desired elements from the input array.
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!