CS 33: Lab #07

CS 33: Computer Organization


LAB 07: C Programming with Functions

Due 11:59pm Wednesday, November 12.

The program handin33 will only submit files in the cs33/lab/07 directory. (You should run update33 first to set up the directory and create any necessary files.)

Remember: You are encouraged to work with a partner.

Each program must follow these following guidelines:

  • the file should be named using the name provided,
  • your code should be compiled using a Makefile,
  • your code should be adequately commented,
  • your program should follow all input and output guidelines exactly, and
  • your program should be compiled via a Makefile.

  1. Write a program (called power.c) which contains this function:

    float raiseRealToPower(float base, int exponent);

    This function will raise base the power exponent. The base can be any floating point value (negative, positive, zero) and the exponent can be any integer value (negative, positive, zero).

    After you have written this function, test that the function is working by displaying a table of the powers of 0.5 between -15 and +5. Your table should be similar, but necessarily exactly the same as this:

      -15    32768.00000
      -14    16384.00000
      -13     8192.00000
      -12     4096.00000
      -11     2048.00000
      -10     1024.00000
       -9      512.00000
       -8      256.00000
       -7      128.00000
       -6       64.00000
       -5       32.00000
       -4       16.00000
       -3        8.00000
       -2        4.00000
       -1        2.00000
        0        1.00000
        1        0.50000
        2        0.25000
        3        0.12500
        4        0.06250
        5        0.03125

  2. In this program (called usholidays.c) you will write a program that displays the observed date of all of the U.S. federal holidays a given year.

    You will begin by prompting the user for a year between 1980 and 2030. If the year is not between 1980 and 2030, remind the user that the year must be between those two years and ask again. After the user has entered a valid year, you will show a table similar to the one shown in the sample output located at the end of the question. Though the exact number of spaces doesn't have to match, it should be well formatted.

    The following is a list of all nationally observed U.S. federal holidays. There are a number of caveats to this list, so be sure to read all of the instructions in this question carefully:
    New Year's DayJanuary 1
    Birthday of Martin Luther King, Jr.3rd Monday in January
    Washington's Birthday3rd Monday in Feburary
    Memorial DayLast Monday in May
    Independence DayJuly 4
    Labor Day1st Monday in September
    Columbus Day2nd Monday in October
    Veteran's DayNovember 11
    Thanksgiving Day4th Thursday in November
    Christmas DayDecember 25

    Federal employees get one day off from work for each of these federal holidays, even if the day falls on a weekend. Holidays which fall on Saturday are observed the previous day (Friday) and holidays which fall on Sunday are observed the following day (Monday). Therefore, the observed day of a holiday is always a weekday (Monday-Friday).

    Important note: On some years (such as 2005), New Year's Day falls on a Saturday. When this happens, the holiday is observed on Friday, December 31, 2004. That means that a) New Year's Day is not observed in 2005, and b) there are two New Year's Day observations in 2004 (Thursday, January 1, 2004 and Friday, December 31, 2004). You should handle this special case.

    You can view the "answers" on the website of the U.S. Office of Personnel Management. (In 2010, they mistakenly do not list the observance of 2011's New Year on 12/31/2010 as they should.)

    Your program should have at least the following four functions:

    1. int leapyear(int year);
      Returns 0 if the year is not a leapyear; 1 if it is a leapyear. Years that are divisible by 400 are leapyears. Years divisible by 100 are not leapyears. Years divisible by 4 are leapyears. All other years are not.
    2. int daysInMonth(int month, int year);
      Returns the number of days in the month. The year is specified so that you can handle the case of leapyears.
    3. int dayOfWeek(int month, int day, int year);
      This function returns the day of the week (Sunday = 0, Monday = 1, ..., Saturday = 6) according to the procedure found on the mathforum.org website under the section "How do I find the day of the week for any date?". Use "Zeller's Rule".
    4. int nthWeekdayOf(int n, int weekday, int month, int year);
      This function returns the day number of the nth weekday in a month and year. For example, if n=4 and weekday=4 (Thursday), month=11 (November) and year=2008, this function will return the day that the 4th Thursday of November 2008 falls on. In this case, that day number is 27, which corresponds to the date on which Thanksgiving will occur in 2008.

      This function should return -1 whenever:

      • The weekday is not between 0-6,
      • The month is not between 1-12,
      • The year is not between 1980-2030, or
      • n is not between 1-5, or
      • There is no nth weekday of that month in that year, e.g. there is a 5th Saturday in November 2008, but there will be no 5th Saturday in November 2009.
    5. void tablePrint(char name[], int month, int day, int year);
      This function prints out one row of the table. Assuming that month, day, and year store correct values, you would call this function as follows (for example):
      tablePrint("Thanksgiving Day", month, day, year);
      Do not worry about the syntax (char name[]) used to pass a string as a parameter. We will talk about this in more detail next week.

    Here are a few runs of the program to give you an idea what I'm looking for:

    lab[~]$ ./usholidays 
    Enter a year between 1980 and 2030: 2008
                              New Year's Day  01/01/2008
         Birthday of Martin Luther King, Jr.  01/21/2008
                       Washington's Birthday  02/18/2008
                                Memorial Day  05/26/2008
                            Independence Day  07/04/2008
                                   Labor Day  09/01/2008
                                Columbus Day  10/13/2008
                               Veteran's Day  11/11/2008
                            Thanksgiving Day  11/27/2008
                               Christmas Day  12/25/2008
    lab[~]$ ./usholidays 
    Enter a year between 1980 and 2030: 1904
    The year must be between 1980 and 2030.
    Enter a year between 1980 and 2030: 1945
    The year must be between 1980 and 2030.
    Enter a year between 1980 and 2030: 2004
                              New Year's Day  01/01/2004
         Birthday of Martin Luther King, Jr.  01/19/2004
                       Washington's Birthday  02/16/2004
                                Memorial Day  05/31/2004
                            Independence Day  07/05/2004
                                   Labor Day  09/06/2004
                                Columbus Day  10/11/2004
                               Veteran's Day  11/11/2004
                                Thanksgiving  11/25/2004
                               Christmas Day  12/24/2004
                              New Year's Day  12/31/2004
    lab[~]$ ./usholidays 
    Enter a year between 1980 and 2030: 2005
         Birthday of Martin Luther King, Jr.  01/17/2005
                       Washington's Birthday  02/21/2005
                                Memorial Day  05/30/2005
                            Independence Day  07/04/2005
                                   Labor Day  09/05/2005
                                Columbus Day  10/10/2005
                               Veteran's Day  11/11/2005
                                Thanksgiving  11/24/2005
                               Christmas Day  12/26/2005
  3. (Extra Credit) If you attended Thursday's talk by Eric Roberts (Nov 6, 7:30pm, Sci Ctr 101), you automatically get this! If you didn't attend, or if you want extra practice, complete this for extra credit. (Note: if you attended the talk and just want the extra credit, put that in a comment at the top of your program to remind me.)

    Extend the usholidays.c program to include Election Day which you should add to the calendar only during years in which there is a presidential election (in years divisible by 4). Election Day is always the first Tuesday after the first Monday in November.