Assigned: Thursday, September 11, 2003
Due AT THE START OF CLASS on Thursday, September 18, 2003
For each problem you need to hand in a concisely written description of your approach, your actual source code including comments, the output of the program, and any work you did to check and verify the results.
Thanksgiving is always the fourth Thursday in November, which this year is the 27th. Using only this information and the old saying “30 days has September, April, June, and November; all the rest have 31 except February, which has 28 and in leap year 29,” write a C program that determines and prints out the numerical date of Thanksgiving for each year from 2003 through 2053.
Print your result in a nicely formatted table. Include a clear explanation of your algorithm.
(a) Write a function with the prototype:
unsigned short rotate_left(unsigned short xval, int n)
that returns the result of bit rotating the unsigned short integer xval by n bit positions to the left. Rotation means that bits shifted out of the left side of the integer reappear into the right side.
You may assume that n is a number in the range 1 through the short integer size in bits.
For example, assuming a 16-bit short integer size,
if xval=23 (0000000000010111) and n=3: output = 184 (0000000010111000)
if xval=31000 (0111100100011000) and n=3: output = 51395 (1100100011000011)
Write a main( ) routine that tests your rotate_left( ) function by allowing the user to enter both the unsigned short integer and the left rotation amount, then prints the integer in decimal AND in binary form. You may need to think about how best to print in binary format.
Show your results for a variety of small and large values of xval .
(b) Now modify your program to allow any integer value for n: if n is negative the rotation must occur to the right, and if n is greater in magnitude than the number of bits in the word the rotation must occur with the proper modulo.