Kindly help me to find error in attached code to find leap years for a range

1 Ansicht (letzte 30 Tage)
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
Riaz Jamil
Riaz Jamil am 8 Nov. 2020
The code gives an error when i run code on command widnow. i.e. the not enough input arguments. Kindly let me know what to do now?
dpb
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 7 Nov. 2020
Bearbeitet: dpb am 8 Nov. 2020
isleapyr=@(yr) (eomday(yr,2)==29);

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by