Applying LeapYear function into the Matlab Scipt

Creat a function called isLeapYear(intYear) that determines if the input year is a leap year or not. Write the main program to calculate the total days passed in a year for a given date. Prompt the user for the (integer) inputs: year, month, and day. The output is the total days passed since Jan 1st of that year excluding the input date (e.g., if the user gives 2012, 01, 23, then the output is 22). Call the function isLeapYear(intYear) to determine if the year is leap or not, if necessary.
Here's my function:
function LeapYear = isLeapYear(intYear)
a = mod(intYear,400);
b = mod(intYear,100);
c = mod(intYear,4);
LeapYear = ((a==0)||(b~=0 && c==0));
end
Here's my main code:
clear,clc
%1. Get Inputs
intYear = input('Enter Year: ');
intMonth = input('Enter Month: ');
intDay = input('Enter Day: ');
%2. Calculate days passed
if isLeapYear==0
intDaysPassed = ((((intMonth - 1)*30)+intDay) - 1);
else
strMessage = 'Please enter a valid leap year';
msgbox(strMessage)
return
end
I tried using "isLeapYear==0" but it's not working. How would I be able to define if a function is true or not in order to proceed with the process?
Thank you so much !!

 Accepted Answer

Put your isLeapYear function code (everything from the function LeapYear etc line to the corresponding end line) in a separate file called isLeapYear.m and make sure this file is on the MATLAB path somewhere (or in your current directory) so MATLAB can find it when you call the function.
Change your main code call from
if isLeapYear == 0
to
if isLeapYear(intYear)
Modify your intDaysPassed formula to account for months that do not have exactly 30 days in them. You might consider starting with a 12 element array that has the number of days in each month, and then work out the logic from there.
Modify your if isLeapYear etc logic so that it prints out the days passed for all user inputs, not just the ones that happen to be leap years.

3 Comments

Thank yo so much for the help ! I tried to create two more functions, for the months and the dates. The one for month seems to be working fine, but the one for date, not really.
Here are the function for the month
function Month = isValidMonth(intMonth)
a = (intMonth <=12);
Month = (a~=0);
end
Here is for the Date
function Day = isValidDate(intDay)
if (intMonth==1||intMonth==3||intMonth==5||intMonth==7||intMonth==8||intMonth==10||intMonth==12)
a = (intDay >1||intDay<31);
Day = (a~=0);
elseif (intMonth==4||intMonth==6||intMonth==9||intMonth==11)
a = (intDay >1||intDay<30);
Day = (a~=0);
elseif (intMonth==2)
a = (intDay >1||intDay<=29);
Day = (a~=0);
end
end
Here is the main code after I've applied all the functions(Years,Month,Dates)
clear,clc
%1. Get Inputs
intYear = input('Enter Year: ');
%2. Calculate days passed
if isLeapYear(intYear)
intMonth = input('Enter Month: ');
if isValidMonth(intMonth)
intDay = input('Enter Day: ');
if isValidDate(intDay)
intDaysPassed = ((((intMonth - 1)*30)+intDay) - 1);
else
strMessage2='Please enter a valid Date'
msgbox(strMessage2)
return
end
else
strMessage1='Pleaes enter a valid Month';
msgbox(strMessage1)
return
end
else
strMessage = 'Please enter a valid leap year';
msgbox(strMessage)
return
end
I haven't learned array yet, so I really don't understand how would I be able to use it. Please check my code and see what the problem is.
Thank you so much !
You should disallow negative month inputs. E.g., you might change your code to something like this:
function Month = isValidMonth(intMonth)
Month = (intMonth >= 1) && (intMonth <=12);
end
For the day number, you should allow 1 to be a valid entry, so use >= 1 in the test instead of > 1. Also you are using '||' in your test when it should be '&&'. Using arrays in MATLAB is fundamental to the language, so you might as well get used to them sooner rather than later. Also, you need to pass in all the variables that the function will be using. E.g., here is a way to write the function using an array:
function Day = isValidDate(intDay,intMonth,intYear)
DaysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31]; % A 1x12 array
if isLeapYear(intYear)
DaysInMonth(2) = 29;
end
Day = (intDay >= 1) && (intDay <= DaysInMonth(intMonth));
end
Finally, it appears to me that the assignment is to print a result for all valid dates entered, not just the ones that are leap years. So I would advise redoing the main logic to something like this:
clear,clc
%\
% 1. Get Inputs
%/
intYear = input('Enter Year: ');
%\
% Consider adding a check for a valid year entry here
% I.e., make a isValidYear function and call it here
%/
intMonth = input('Enter Month: ');
if ~isValidMonth(intMonth)
msgbox('Pleaes enter a valid Month');
return
end
intDay = input('Enter Day: ');
if ~isValidDate(intDay,intMonth,intYear)
msgbox('Pleaes enter a valid Day');
return
end
%\
% 2. Calculate days passed
%/
DaysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31]; % A 1x12 array
if isLeapYear(intYear)
DaysInMonth(2) = 29;
end
% Now add some logic here using the DaysInMonth array above to calculate
% the intDaysPassed value and display the result. You might look into
% the cumsum function for this. What do you get when you call cumsum(DaysInMonth)?
end
You're awesome ! Thank you so much !

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!