CS 180 Program 4: Subprograms

Assignment date: 11 October 2006

Due date: 23 October 2006

This program explores the use of subprograms (i.e., procedures and functions).

Your program is going to ask the user for a date (in the form of three integers: month, day, year) and first confirm that the given date is valid (e.g., 9/31/1947 is not a valid date because September (9) has only 30 days), then tell the user the day of the week that the date fell on. For example, if the user gives you the date 1/26/1963, your program should first check that the date is valid (it is), then tell the user it was a Saturday.

You will undoubtedly find it useful to use a formula discovered by a British theologian named Zeller. His formula says:

Day = ((26M-2)/10 + D + Y + Y/4 + C/4 - 2C) MOD 7

An alternate version of the formula that avoids negative values is:

Day = ((26M-2)/10 + D + Y + Y/4 + C/4 + 5C) MOD 7

where M is the number of the month, D is the day, Y is the last two digits of the year number and C is the century (the first two digits of the year number). Integer division is used. The result will be a value between 0 and 6, where 0 means Sunday, 1 means Monday, . . . 6 means Saturday. Things are made slightly more complicated by the fact that months have to be numbered starting with March as month 1; January and February are treated as months 11 and 12 of the previous year. We therefore need to adjust the month and year like this:

IF Month < 3 THEN
  Year := Year - 1;
  Month := Month + 10;
ELSE
  Month := Month - 2;
END IF;

A detailed example of the formula for the date: January 25, 1956 (i.e., 1-25-1956)

Day = ((26M-2)/10 + D + Y + Y/4 + C/4 -2C) MOD 7
   = ((26*11-2)/10 + 25 + 55 + 55/4 + 19/4 - 2*19) MOD 7
   = (284/10 + 25 + 55 + 13 + 4 -38) MOD 7
   = 87 MOD 7
   = 3 (meaning Wednesday)

Your program also needs to use procedures and functions. Minimally you should have a procedure for getting a valid month, day and year from the user, a function for calculating the day of the week, and a procedure for formatting the output to the user. This means your main program will look something like:

begin
  GetValidDate(Month=> M, Day => D, Year => Y);
  DayOfWeek := UseZeller(Month =>M, Day => D, Year => Y);
  OutputDayOfWeek(Day => DayOfWeek);
end Zeller;

Hand in