CS246 Homework 2:  functions

Due: Monday, February 13, 2012 (Program desgin due Monday, February 6)

Please read the homework guidelines before you proceed.

For this homework, a program design is required from each group. A program design is a document containing skeleton code that represents the implementation of your actual program. All functions are represented by prototypes only, except for main(), which should be fleshed out with appropriate calls and local variable declarations. This part is due in class on Monday, 2/6. A hardcopy printout will suffice (no Latex).

In addition to the electronic submission of your program, you should also hand in a write-up on how your final implementation differs from your program design, if any, and why. This part should be done with Latex. Use the verbatim environment for any code quoted. Hand in the write-up together with the hardcopy printouts of your code.

Perpetual Calendar [100pts]

Write a program that displays a Gregorian calendar for any year. Essentially, you are implementing the Unix 'cal' command. Actually the Gregorian calendar was not adopted in Europe until 1582, and not until 1752 in Britain (If you check with the Unix 'cal' command, you will see that the 11 days between 9/3/1752 and 9/13/1752 are missing.) The Julian calendar was used before that in Christian countries, and the Gregorian didn't become the world standard until the 1900 on. For the purpose of this program though, we are still going to display a Gregorian calendar for every year 1-9999 After Christ (Anno Domini).

Here's an algorithm for calculating the calendar:

  1. Find the day of the week a year starts on.

    In general, for every year that is not a leap year, next year starts one day later in the week. For every year that is a leap year, next year starts two days later in the week.

    2012 starts on a Sunday. Count backwards (substract) for any year before 2012 following the same rule.

    Rules for deciding a leap year:
    Every year divisible by 4 is a leap year.
    However, every year divisible by 100 is not a leap year.
    However, every year divisible by 400 is a leap year after all.

  2. Find days in a month. January, March, May, July, August, October and December each has 31 days, Febuary has 28 days if current year is not a leap year, 29 if it is. All the rest of the months have 30 days.
  3. Now that you have the day of the week the year starts on, and the number of days in any month, you can find out the day of the week each month starts on. Indent (print white spaces) accordingly, and you have the correct calendar.

Your program must do the following:

Copy the executable ~dxu/handout/cs246/calndr and run it to get an idea how your program should behave. Also check out the unix shell command cal