Project 1 (20 points)
 Assigned: Thursday, January 14, 2010
Due: Thursday 21, 2010, 11:59 PM



Programming Assignment #1 — Display a 12-month Calendar

Abstract

Write a C program that displays a twelve month calendar for an arbitrary year. Prompt the user for the year of the calendar, and print out the calendar month by month, so that it looks like a real calendar.

Outcomes

After successfully completing this assignment, you should be able to:–

·        Develop a C program on a Unix platform

·        Design a program that contains nested selection and iteration constructs

·        Specify the loop invariants that you use to reason about your program

·        Use advanced formatting strings and conversion specifiers to do I/O in a C program

Before Starting

Re-read Chapters 1 and 2 of K&R or Chapter 2 of D&D. Read §§3.1-3.5 K&R or Chapters 3 and 4 of D&D. This assignment bears a slight resemblance to exercise 4.35 of D&D and to programming assignments in recent terms of CS-2301.

The Assignment

Write a C program that displays twelve-month calendar for a particular year. The program should prompt the user for the year to be printed, and then it should figure out (a) whether the year is a leap-year and (b) what day of the week the chosen year starts on.

The calendar should be formatted as shown in the sample execution below. Note that numbers the days must be right-justified under the names of the days and that two spaces separate the names of the days from each other.

Interfaces

The interface <stdio.h> provides the functions printf and scanf.

Assumptions and Restrictions

The user may enter any non-negative integer for the year. You must calculate the calendar according to the modern international standard calendar that was introduced by Pope Gregory XIII in the year 1582. For input years earlier than 1582, calculate them as if the modern calendar were in effect.

In the modern calendar, years that are divisible by 4 are leap years, except that years divisible by 100 are not leap years unless they are also divisible by 400. That is, there are 97 leap years every four centuries.

You will have to figure out what day of the week the calendar starts on. You may do this by referring to a known year in which you know the day of the week of a particular date. You will then work backwards from that known date to find the start of the input year.

Sample Execution

                        MONTHLY CALENDAR
 
Please enter year for this calendar:- 2009
 
 
  ***    CALENDAR for 2009   ***
 
January 2009      
 
Sun  Mon  Tue  Wed  Thu  Fri  Sat
                      1    2    3
  4    5    6    7    8    9   10
 11   12   13   14   15   16   17
 18   19   20   21   22   23   24
 25   26   27   28   29   30   31
 
February 2009
 
Sun  Mon  Tue  Wed  Thu  Fri  Sat
  1    2    3    4    5    6    7
  8    9   10   11   12   13   14
                 .
                 .
                 .

(output continues for all 12 months)

Implementation Notes

Since we have not yet studied arrays, strings, or arrays of strings, you should design your algorithm to use if-else or switch statements to print the month names and to set other variables.

You must partition your program into multiple functions. Here is an example partition:–

·        The function main() prompts the user for input, calls a function of your own design to determine the starting day of the input year. It then invokes the function printCalendar() to actually print the twelve month calendar.

·        The function printCalendar() takes two arguments, the year number and the starting day. It then loops through the year and calls the function printMonth() twelve times, once for each month.

·        The function printMonth() takes two arguments, the year number and the starting day of that particular month, and it returns the number of the day on which the next month starts. Print month has to first call a function printMonthName() and then print out the days of the month in calendar format.

·        The function printMonthName() takes the year number and the month number as arguments, prints out the line identifying the month, and returns the number of days in that month. The example output of printMonthName() should look resemble the following:–

January 2009      

Since we are not using arrays of strings, printMonthName() should use a switch statement to select and print the name of the month and to determine the number of days in that month. If the month is February, it should also figure out whether the year is a leap year and return the correct number of days.

Algorithm and Loop Invariants

There are many sources on the web and at WPI for a suitable algorithm for this assignment. You may consult any of these, but you must cite your source. If you worked out the algorithm on your own, you should say in your write up file that this is entirely your work.

If you borrowed some or all of an algorithm from someone else or from somewhere else, do not copy it. Write it out in your own words and your own coding style. Also, please explain enough about how the algorithm works that the graders can conclude that you understand it.

This project requires at least two loops. For each loop, write a loop invariant — that is, a logical statement in English or mathematical notation that says what salient facts are true about the relationships of the variable at the same point in the loop for each iteration.

Deliverables

Write a document called README.txt or README.doc summarizing your program, how to run it, and detailing any problems that you had. Also, if you borrowed all or part of the algorithm for this assignment, be sure to cite your sources and explain in detail how it works. Be sure also to specify the loop invariant of each loop.

From your CCC Linux shell, submit your C source code file using the following turnin command:–

/cs/bin/turnin submit cs2303 PA1 <your files> README

Programs submitted after 11:59pm on due date (January 21) will be tagged as late, and will be subject to the late homework policy.

Grading

This assignment is worth twenty (20) points. Your program must compile without errors in order to receive any credit. It is suggested that before your submit your program, compile it again on a CCC system to be sure that it does not blow up or contain surprising warnings.

·        Correct compilation without warnings – 2 points

·        Correct execution with graders’ test cases – 2 points

·        Correct usage of scanf() to get inputs from user – 1 point

·        Correct usage of print() to print the various lines of the calendar – 3 points

·        Correct usage of conditional and loop statements – 5 points

·        Satisfactory README file – 2 points

·        Loop invariant for each loop in README document – 5 points