CS1101C Lab 1 (Even Week)

Nth Day of the Month

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

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

Preliminary

Certain special dates fall on an nth day in the month. For example, in many countries, Mother's Day is celebrated on the 2nd Sunday in May. Father's Day is celebrated on the 3rd Sunday in June.

What date of the month is the 2nd Sunday in May 2007? What date of the month is the 3rd Sunday in June 2007?

These are the questions we would like to be able to answer by writing a C program.

You may wish to refer to Lab 1 Odd Week. Here is the question and suggested solutions are HERE and HERE. You may of course write your own solutions to Lab 1 Odd Week.

Sample Runs

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

$ gcc -Wall day2.c -o day2

$ ./day2

Enter year (1800-2299): 2007

Enter month (1=January,2=February,...,12=December): 8

Enter day of the week (1=Monday,2=Tuesday,...,7=Sunday): 4

Enter nth day of the month (1-5): 5

The date of the 5th Thursday of August 2007 is: 30

$ ./day2

Enter year (1800-2299): 2000

Enter month (1=January,2=February,...,12=December): 5

Enter day of the week (1=Monday,2=Tuesday,...,7=Sunday): 7

Enter nth day of the month (1-5): 2

The date of the 2nd Sunday of May 2000 is: 14

$ ./day2

Enter year (1800-2299): 1987

Enter month (1=January,2=February,...,12=December): 1

Enter day of the week (1=Monday,2=Tuesday,...,7=Sunday): 1

Enter nth day of the month (1-5): 1

The date of the 1st Monday of January 1987 is: 5

$

Note and Ponder

  1. Assume that all user input is valid. No error checking is required.

  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. 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.

  6. Most importantly, have lots of fun programming!


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

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

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