MATLAB question: Write a function days in month.m to display the number of days
ID: 3790215 • Letter: M
Question
MATLAB question:
Write a function days in month.m to display the number of days in a given month. The function should have the declaration: function days = days in month(month,leap) where the input month is an all-lower-case string denoting the first three letters of the month. The input leap has logical value (0 or 1) indicating the leap year. The output days displays the number of days in the input month. February has 28 days ( 29 days in leap year). The following months have 30 days: April, June, September and November. Other months have 31 days. In cases where the inputs are invalid, the output days should be the string 'Invalid inputs'. The function should include a description. Use nested switch statements.
Explanation / Answer
% matlab code
function days = daysInmonth(month,leap)
switch(month)
case 'jan'
days = 31;
case 'feb'
switch(leap)
case 1
days = 29;
case 0
days = 28;
end
case 'mar'
days = 31;
case 'apr'
days = 30;
case 'may'
days = 31;
case 'jun'
days = 30;
case 'jul'
days = 31;
case 'aug'
days = 31;
case 'sep'
days = 30;
case 'oct'
days = 31;
case 'nov'
days = 30;
case 'dec'
days = 31;
otherwise
fprintf('Invalid inputs ' );
quit
end
end
month = input("Input month(all-lower-case string denoting the first three letters of the month): ", 's');
leap = input("Input leap (has logical value (0 or 1) indicating the leap year): ");
fprintf("%s has %d days ",month, daysInmonth(month,leap));
%{
output:
Input month(all-lower-case string denoting the first three letters of the month): feb
Input leap (has logical value (0 or 1) indicating the leap year): 0
feb has 28 days
%}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.