Homework 1 — Display a Calendar

Due: Sunday, November 9, 2008 at 11:59pm

Abstract

Write a C program that displays a one-year calendar. Prompt the user for the day of the week on which January 1 falls, and whether or not the year is a leap year.

Outcomes

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

·        Develop a C program on a Unix platform

·        Design a program that contains nested selection and iteration constructs

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

Before Starting

Read Chapters 1 and 2 and §§3.1-3.5 of The C Programming Language, 2nd edition, by Kernighan and Ritchie.

Details about scanf() and printf() can be found in Appendix B, §§B1.2-B1.3, or by executing in a Linux command shell any of the commands:–

man printf
man scanf
info printf
info scanf

The Assignment

Write a C program called hw1.c that displays a one-year calendar. The program should prompt the user for the day of the week on which January 1 falls, and then it should prompt whether or not the year is a leap year. The day that January 1 falls on should be coded as:–

·        0 – Sunday

·        1 – Monday

·        2 – Tuesday

·        3 – Wednesday

·        4 – Thursday

·        5 – Friday

·        6 – Saturday

The calendar should be formatted as shown in the sample execution. Note that numbers are right-justified under the names of the days, and that two spaces separate the names of the days.

Include files

·        <stdio.h> provides printf and scanf

Assumptions and Restrictions

You may assume that the user enters valid data; that is, your program does not have to check for valid responses. You should not define any functions in the solution for this program other than the main() function.

Sample Execution

                        MONTHLY CALENDAR
 
This program displays a calendar.  You need to provide the day of the week
on which January 1 falls, and indicate whether or not the year is a leap year.
 
Enter the code number for the day of the week on which January 1 falls:
0- Sun    1- Mon    2- Tue    3- Wed    4- Thu    5- Fri    6- Sat
 
Enter day code now (0 - 6):  4
 
Is the calendar for a leap year? (type 1 for yes, 0 for no): 0
 
 
 
     ***    CALENDAR   ***
 
January
 
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
 
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)

Exception

We have not yet introduced arrays of strings to use for the month names. Therefore you may substitute month numbers for month names. The calendar would then read as follows:–

Month  1
 
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
 
Month  2
 
Sun  Mon  Tue  Wed  Thu  Fri  Sat
  1    2    3    4    5    6    7
  8    9   10   11   12   13   14
                 .
                 .
                 .

Deliverables

Write a short file called README summarizing your program, how to run it, and detailing any problems that you had. From your Unix account, submit your C source code file using the following turnin command:–

/cs/bin/turnin submit cs2301 hw1 hw1.c README

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

Grading

This assignment is worth five (5) points toward your final grade. Your program must compile without errors in order to receive any credit. It is suggested that before your submit your program, compile it again one more time to be sure that it does not blow up.

·        Correct compilation without warnings – 0.5 point

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

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

·        Correct usage of if-else statements and loops – 2 points