Homework 2

Introduction

You will have the opportunity to work with your team on this homework during your Lab 7 session, BUT DO NOT WAIT until then to start this homework.

In this homework we will use the top-down approach to design and the bottom-up approach to implement a program to solve the task of finding the difference in days between any two dates. When completed, you will be able to use the program to compute all kinds of interesting things like how many days until Spring Break, or how many days since the start of the millennium (though this could be controversial :-)), or how many days old you are. We will implement this program incrementally; i.e. a little bit at a time, testing each part as we go along. In the process, you will use a few common debugging techniques such as function level debugging and function stubs. You will also get to use a Unix utility called make to help you compile the different test programs called for below.

For this homework, you will again work in your teams, and submit only ONE set of files per team. Again, ALL members of the team should contribute to the homework, and understand how all of the programs work. As I suggested last time, you should meet as a team to discuss understanding the problems and develop the algorithms as a group. One or more members of the team can then write the code and begin testing it. When a program compiles, you can exchange code with your teammates to let them understand, test, critique, and possibly improve it. You should rotate the job of "coordinator" for this assignment to a different person from the previous homework. The coordinator is the member who will collect the final versions of the files and submit them to be graded. Remember, this is a TEAM effort. You should help and learn from each other.

For this homework, you should pay particular attention to the documentation of your program; i.e. the comments on your code describing the ALGORITHM (in English, not C-lish) and the comments describing the interface and operation of your functions. See hw1 for the format of the heading comment you should have in each of your source files.


A mini-mini project

Task:

Write a program to find the difference between two dates. The start date and end date are entered as integers giving the day, month, and year (the full year (e.g. 2016), since dates can be in any century).

In this bottom-up implementation approach, we will begin by implementing simple functions, then use these functions to implement more complicated functions until we can solve the entire task. At each step, we will write a driver program as main() to test the function we are adding. You should test your function thoroughly at each step to ensure that it works correctly for appropriate data. You should also consider how "robust" your programs are for this homework; i.e. how well they can tolerate user errors. I have provided a sample executable in:

          ~ee160/Homework/Hw2/datediff

As a minimum, your final program should execute like that one. However, my program is not very "robust".

To help you compile the various modules and test drivers I have provided a makefile in ~ee160/Homework/Hw2/makefile. You should first copy this makefile to the directory where you will develop your programs and use the make commands given below to compile them. You will have to name your files as indicated below for the makefile to work correctly.


1. Leap Years (4 Points).

Sub-TaskWrite a program that uses a function to determine if a given year is a leap year. A year is a leap year if it is divisible by 400; or if it is divisible by 4 and it is not divisible by 100. The prototype is given below. which when given a year (> 0), determines if the year is a leap year, returning TRUE or FALSE. You should put the code for this function in the file leap.c and the prototype for the function in the file leap.h.

        int is_leap(int year);

Leap Year info: The length of the solar year, however, is slightly less than 365¼ days-by about 11 minutes. To compensate for this discrepancy, the leap year is omitted three times every four hundred years. In other words, a century year cannot be a leap year unless it is divisible by 400. Thus 1700, 1800, and 1900 were not leap years, but 1600, 2000, and 2400 are leap years.


Write a main() which repeatedly asks the user to enter a year and prints if it is a leap year or not. The program terminates when the user enters 0 for the year. Put the code for the driver in the file driver1.c. You will compile this program with the command:

         make driver1

which will create an executable called driver1.


2. Days in a Month (4 Points).

Sub-Task: Write a function:

        int days_in_month(int month, int year);

which is given a month and year and returns the number of days in that month. You should use your is_leap() function as needed to determine the number of days in the month (February has an extra day in leap years). Put the code for the function days_in_month() in the file days.c, and the prototype in the file days.h

Write a driver, main(), to ask the user to enter the month and year (use EOF to terminate the program) and prints the number of days. Put the driver in the file driver2.c. You will compile this program with the command:

         make driver2

which will compile and link the source files days.c leap.c and driver2.c to create an executable called driver2.


3. Julian Date (5 Points).

Sub-Task: Write a function:

        int julian_date(int day, int month, int year);

This function is given the day, month and year and returns the Julian date. The Julian date is the ordinal day number for that day. For example, 1 Jan is day 1 of any year, 31 Dec is day 365 for any non-leap year and 1 Feb is day 32 for any year. Use your days_in_month() function from the previous problem to calculate the Julian date. Put the code for the function julian_date() in the file julian.c, and the prototype in the file julian.h

Write a driver, main(), which asks the user to enter a month, day, year and prints the Julian date. Terminate with EOF. Put the driver in the file driver3.c. You will compile this program with the command:

         make driver3

which will create an executable called driver3.


4. Date Difference (7 Points).

To complete the overall task, you need to write just one more driver using the functions you have written so far. The driver is to read a start date and end date and compute the number of days between them. Terminate the program when EOF occurs when reading the start date. You should think about your algorithm for this program in terms of what "tools" you have to use (the functions written in the previous three problems). Implement your algorithm in main() in the file datediff.c. You will compile this program with the command:

         make datediff

which will create an executable called datediff.


Compiling and Debugging Tips

You will be using a Unix utility called make which will help you keep track of what files need to be recompiled when you make changes, and will link the necessary files to create each of the executables. In this homework you will just use make with the makefile I give you. To do this you should copy the file:

              • ~ee160/Homework/Hw2/makefile

to your directory where you will be writing these programs. In future labs and homeworks you will learn how to create a makefile for yourself. To compile each of the test programs and the final program, you should use the make command given above. You can also compile all 4 of the executables in one step with the command:

              • make

In the three test driver programs called for above, your objective is to test a new function to make sure it works before proceeding to the next step. A common technique for testing a function is to use debug lines at the entry and exit of the function to verify that the function is producing the correct results for the given input. For each of the functions you write, put a debugging printf() at the beginning of the function showing the values passed in to the function, and another debugging printf() just before the return to show the value being returned by the function. You should use Conditional Compilation (using #ifdef) for these debug printf()'s. to allow you to turn the debugging output on and off. (You also use this technique in Lab 6).

At first glance, it would seem that you need to write these functions in the order specified above - that you cannot do one part without getting the previous function to work correctly. Another common debugging technique to avoid this problem is the use of stub functions, functions that have the same external "appearance" as the real functions, but don't actually do real operations. Instead, they "fake it", by either asking the user for help, or just always giving the same answer. I have provided three files, leap.c, days.c and julian.c, in ~ee160/Homework/Hw2 that contain stub versions of the three functions called for above (you will need to provide your own .h files). You can copy those to your working directory and use the stub version to help debug your programs until the "real" version is complete. BUT BE CAREFUL that you don't accidentally destroy one of your own files containing the "real" code when you copy these files.

What You Turn In

You will submit ONE copy of all of the files for this homework per team all at one time. So transfer the version of your files your team wants to submit to your coordinator for this assignment, and the coordinator will send them in. (it might be a good idea to compile and test them one last time before sending them in to make sure they work). Please DO NOT send different files from different members - we do not want to hunt around to find all of your team's programs. Use the "grade" command to turn in all of the .c and .h files to make the four versions of the program. Your command will look like the following:


If you are in Section 001 use:

            grade -22s1,ee160 *.c *.h


If you are in Section 002 use:

            grade -22s2,ee160 *.c *.h


If you are in Section 003 use:

            grade -22s3,ee160 *.c *.h


If you are in Section 004 use:

            grade -22s4,ee160 *.c *.h


You should verify that you turned in things successfully, which you can do with the command (which simply leaves the file names off from the previous command).

            grade -22s1,ee160
 
            OR
 
            grade -22s2,ee160
 
            OR
 
            grade -22s3,ee160

            OR
 
            grade -22s4,ee160

NOTE: after the files are prepared for grading, you will no longer be able to see your file listing using the above command.

In addition, each member of the team should send me a confidential form evaluating the effort of the team using the ratings shown on the rating page. Include a description at the end of the form of what parts of the programs where done by each member of the team, including yourself, and an assessment of how teamwork was used to complete this assignment. This evaluation WILL be used to adjust the individual homework grades for BOTH hw1 and hw2. It is due at the same time as the assignment. NO LATE EVALUATIONS WILL BE ACCEPTED. If you do not email your eval on time, your homework grades will be adjusted based on your teammates' inputs and your voice will not be heard. In addition, your individual score will receive a 10% penalty for not turning in the eval. Each individual should send your eval to me via a confidential email (Check The Syllabus, Ratings go to a different email, This helps declutter my inbox from all the other emails I receive daily).

If you have any questions or problems with this homework, come see me or the TA's or send us email.