CS 172 Program 1: Putting it all Together

This program explores the use of subprograms (i.e., procedures and functions), as well as basic mathematical operations, if/elsif/else statements, and simple I/O.

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. Your program should work for years in the range 1583 to 4000. You need to make sure you handle leap years correctly. Ask for clarification if you do not know how to determine if a year is a leap year. The simple explanation is that years evenly divisible by 4 are leap years, except that years evenly divisible by 100 are NOT leap years, except that years evenly divisible by 400 ARE leap years. So 2004 was a leap year, 1900 was NOT a leap year (nor were 1800, 1700), but 2000 WAS a leap year (and so was 1600).

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

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;

When you think your program is ready: