# Using the-switch Structure for Calendar Calculauons Matlab Help

Using the-switch Structure for Calendar Calculauons

Use the swi tch structure to compute the total elapsed days III .1year, given the number (1-12) of the month, the day, and an indication of whether or nut the year l~ d kJr vear

• Solution
Note that February has an extra day if the year is a leap year. The following function
computes the total elapsed number of days in a year, given the month, the day of the month, and the value of extra_day, which is I for a leap year, and 0 otherwise.
function total_days = total (month,day,extra_day) total_days = day; for k = l:month – 1
switch k
case {1,3,5,7,8,10,12}
total_days = total_days + 31;
case {4,6,9,11 }
total_days total_days + 30;
case 2
total_days total_days + 28 + extra_day;
end
end
The function can be used as shown in the following program.
month = input(‘Enter month (1 – 12): ‘);
day = input (‘Enter day (1 – 31): ‘I;
extra_day = input(‘Enter 1 for leap year; 0 otherwise: ‘I;
total_days = total (month,day, extra_day)

Posted on July 30, 2015 in Programming with MATLAB