CS1101C Lab 1 (Odd Week)

Day of Week Calculator

The deadline for this lab question is Friday 14 September 2007, 23:59:59 hours.

The name of your C program file must be called day1.c, files with any other name will not be marked.

Preliminary

Have you ever thought about the day that you were born? Was it a Sunday? Or Monday? Are you a weekend baby? Or are you a mid-week baby?

Write a program that asks the user to input any date in DD MM YYYY format and calculate the day of the week that corresponds to the date entered.

Use the following algorithm. As an example, suppose that the user enters 25 12 1987.

  1. Take the last two digits of the year and add a quarter to itself. Example: 87 + 87/4 = 108. Ignore the decimals.

  2. Get the first two digits of the year, and obtain the century code according to the following table:

    CenturyCentury Code
    18
    2
    19
    0
    20
    6
    21
    4

    Example: 19 = 0.

  3. Get the corresponding month code for the month according to the following table:

    MonthMonth Code
    January
    1
    February
    4
    March
    4
    April
    0
    May
    2
    June
    5
    July
    0
    August
    3
    September
    6
    October
    1
    November
    4
    December
    6

    Example: 12 (December) = 6.

  4. Take the day. Example: 25.

  5. Add the above numbers together. Example: 108 + 0 + 6 + 25 = 139.

  6. If the year is a leap year AND the month is January or February, add 6 to the above total. In our example, 1987 is not a leap year so we do not add anything to the total.

  7. Divide the total by 7 and take the remainder. Example: 139 divided by 7 = remainder of 6.

  8. This remainder corresponds to the day of the week. 0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday. In our example, we get 6 = Friday.

Sample Runs

Assuming that the executable is day1, sample runs of the program are shown below. User input is denoted in bold.

$ gcc -Wall day1.c -o day1

$ ./day1

Enter date in DD MM YYYY format: 25 12 1987

The day for 25/12/1987 is: Friday

$ ./day1

Enter date in DD MM YYYY format: 9 2 2000

The day for 09/02/2000 is: Wednesday

$

Note and Ponder

  1. Assume that all user input is valid.

  2. Hint: Experiment with the format specifier %02i in your printf function. What does it do?

  3. Hint: Use the format specifier %d instead of %i in your scanf function. Why?

  4. You may use the cal command in sunfire to check if your program is working correctly. See the sample runs below. User input is denoted in bold.

    $ cal 12 1987
       December 1987
     S  M Tu  W Th  F  S
           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
    
    $ cal 2 2000
       February 2000
     S  M Tu  W Th  F  S
           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
    
    $
    
  5. What is the century code for the century 22?

  6. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  7. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 24 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 14 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 10 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.