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:
- 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.
- 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.
- 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:
- Take input from the user for the year and month. If a number between 1 and 12 is entered for the month, print the calendar for that month only. If 0 is entered for the month, print the calendar for the entire year (all 12 months). If a number greater than 12 is entered for month, default it to 0.
- Print each month's name with the month's calendar.
- You must format your output so that the days of the week line up. Hint: When calling printf, putting an integer between % and d in the formatting string will cause printf to print in the width specified by the integer. For example, printf("%2d", x); will print x in a fieldwidth of two characters, right-flushed. For more information on printf formatting, refer to the text books.
- Pay special attention to designing your functions and your program organization. Your project will be graded on its organization and modularity as well as functionality. You should have at least the following functions, but probably more:
- Calculation functions:
- A function that takes a year and decides if it is a leap year or not
- A function that takes a year and finds the day of the week on which the given year starts.
- A function that determines the number of days in a given month.
- Input functions:
- A function that accepts the year from the user, and returns it.
- A function that accepts the month from the user, and returns it.
- Output functions:
- A function that prints the calendar for a given month.
- A function that prints the calendar for a give year
- Provide a prototype for every function in your program in a header file, which you then include in your source file using the #include directive. The file should be named the same as your program but with extension .h. Submit it together with your source code.
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