
% Homework 3 Solutions
% tomorrow.m
% CS 1109, Summer 2011
% July 18

% This script prompts the user to enter today's date in MM/DD format and
% outputs tomorrow's date in MM/DD format.

clc
clear

% This vector stores the lengths of the months in order i.e., Jan. through
% Dec.
monthLengths = [31 28 31 30 31 30 31 31 30 31 30 31];

% Get current date as a string from the user; we get it as a string so that
% Matlab doesn't divide the two numbers the user enters
today = input('Enter date: ', 's');

% Extract the day and month fields from the user input and convert them to
% numbers
todaysDay = str2num(today(4:5));
todaysMonth = str2num(today(1:2));    
    
% Compute default next date
newDay = todaysDay + 1;
newMonth = todaysMonth;

% If the new day exceeds the number of days in the month, we need to wrap
% around to the next month
if (newDay > monthLengths(todaysMonth))
    newDay = 1;
    newMonth = todaysMonth + 1;
    
    % If the new month is beyond December, wrap around to January
    if (newMonth > 12)
        newMonth = 1;
    end
end

% Print out tomorrow's date padded appropriately with zeros
fprintf('Tomorrow is: %02d/%02d\n', newMonth, newDay);

% Common mistakes / other observations:
%
% 1) Notice how the clever use of an array ('monthLengths' in this case)
% significantly shrunk the size of our code. Many people took a more
% exhaustive approach and used a series of nested if-statements etc.
% Anytime your programs start getting unwieldly and large, treat it as a 
% signal to ``proof-read'' your code -- can something be done more
% efficiently? Am I repeating code that can be placed in a loop? Etc.
%
% 2) Some people did not correctly format the output. Note that the handout
% says that single-digit days/months should be padded with leading zeros.
% This is accomplished by using the %02d format specifier in the fprintf
% statement.
%
