CSC 121A Lab 8 - Spring 2005

Thursday, February 24, 2005
Due: Wednesday, March 2, 2005, 5:00PM

[Course home page] [Java API Documentation]

Answer all numbered and bulleted questions. Upload your answers and program files to the VikingWeb coursework section. Besides uploading your written answers to the VikingWeb, please turn in a printed copy either in class on the due date or leave a copy in the box outside my door later on during the day. (You may turn in the printed copies later than 5PM on the due date, but the files must be uploaded to VikingWeb at or before 5PM.)

You may work in pairs for all parts of this assignment, including the homework exercises.

Lab Exercises

 

Homework Exercises

Computing days of the week: Write a program that will input a date in the format mm/dd/yyyy and then print out which day of the week that date falls (or fell) on. Your program should run as follows (the bold text is what the user types):

	  
	$ ./datecompute
	  
	Please enter date in the format mm/dd/yyyy: 2/30/1892		

	2/30/1892 is a Tuesday.

	  

Name your source code file: datecompute.c

Your program should contain a reasonable amount of error-checking. If the user enters invalid input such as characters instead of numbers, then the program should print an error message and exit neatly (by returning -1 indicating an error), instead of crashing.

Use proper programming style, including indentation, appropriate variable names, etc.

In order to compute the day of the week from a given date, you can use Zeller's algorithm, described below.


Zeller's Algorithm

Let A, B, C and D denote integer variables that contain the following values:

Next let W, X, Y, Z and R denote integer variables, and compute the following values in this order, using integer arithmetic.

	W = (13A - 1) / 5
	X = C / 4
	Y = D / 4
	Z = W + X + Y + B + C - 2D
	R = the remainder obtained when Z is divided by 7.

The value of R represents the day of the week, where r = 0 represents Sunday, r = 1 represents Monday,. . . , and r = 6 represents Saturday. If R turns out to have a negative value such as -4, add 7 to it to get a nonnegative value between 0 and 6.

Note #1: If the month values are January or February, then the previous year is used for the computation, not the current year. This has to do with the fact that at one point in history, the beginning of the year was considered to be March 1, not January 1.

Note #2: Zeller's Algorithm is very specifically tied to the Gregorian calendar. For this reason, it will probably not give a correct result for older dates.


 

Extra-Credit Exercises

These will allow you to add points to your exam grades. They should actually also help you do better on the exams by giving you more practice with different aspects of C programming, so you should try to complete them.

This Extra-Credit portion is due: Friday, March 4

 



Last modified: Wed Feb 23 22:47:12 EST 2005