Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please complete this problem in matlab. Do not make it a function, thank you! Ho

ID: 3909032 • Letter: P

Question

Please complete this problem in matlab. Do not make it a function, thank you!

Homework Problem 3: Day of the Year In this problem, you will be given the day, month and year and be required to find what day of the year it is. To do this, first create a file named calcDayOfYear.m. The first three lines of your file should define day, month and year to be some integers. You wish to calculate doy - which is the date of the year. For example, 1 January is the 1st day of the year, while 1 February is the 32nd day of the year. Your code should be able to handle the special case of leap years, which are defined as follows: years divisible by 400 are leap years, years divisible by 100 but not 400 are not leap years, years divisible by 4 but not 100 are leap years, all other years are not leap years. Your code should be able to correctly calculate the following test cases: When day 1, month 1, year = 2017 then doy-1 When day 31, month 12, year 2017 then doy 365 When day 1, month 3, year 2016 then doy 61 When day = 1, month 3, year = 2100 then dov-60 When month 12, then doy -1 When day 32, month 1, year 2017 then doy 1 When day 31, month 4, year 2017 then doy 1 Store the code of this problem in the filename calcDayOfYear.m. This file must go into your zipped folder as it will be evaluated.

Explanation / Answer

Given below is the code for the question. You may change the values for day, month and year and check all test cases.
Please do rate the answer if it was helpful. Thank you

day = 1;
month = 3;
year = 2016;

daysOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

%check if year is leap
leap = false;

if rem(year, 400) == 0
leap = true;
elseif rem(year, 4) == 0 && rem(year, 100) != 0
leap = true;
end

%add 1 more day to feb if leap year
if leap
daysOfMonth(2) = 29;
end

if month < 1 || month > 12 || day < 1 || day > daysOfMonth(month)
dayOfYear = -1; %invalid date
else
%add up all days in previous months i.e 1 to month-1 and add the day in current month
dayOfYear = sum(daysOfMonth(1:month-1)) + day;
end
disp(['The day of the year is ', num2str(dayOfYear)])

output
-------
The day of the year is 61

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote