Sunday, July 26, 2009

C++ code for calculating the day of the week on a date entered by the user?

This is the code which I've written but it has some bugs. I get the correct day for all yrs after 2001 as well as before 2001 except for the 1960's decade. I'm not able to detect the error. Please help me!

C++ code for calculating the day of the week on a date entered by the user?
Your leap year calc is wrong. Leap year is every year evenly divisible by 4, except when evenly divisible by 100, unless evenly divisible by 400.





1900 was not a leap year (evenly divisible by 100.)


2000 was a leap year (evenly divisible by 400.)





Hmm, but that shouldn't affect any input dates after 1900.





Edit:





What were you using as the authoritative source [of correct answers]? Because actually, according to the calendar in Windows XP, it isn't correct for any year prior to 2001:





1/1/2000 is a Saturday, not a Tuesday


1/1/1999 is a Friday, not a Monday





I believe part of the inaccuracy is introduced at your calc:





ref_lp = 2001 / 4;





which would be 500.25 (not 500) if the fraction wasn't truncated (lvalue is type int.)





When you consider the fact that you are attempting to calculate the number of leap years since 0 AD, it becomes clear that accuracy does matter.





Here's where it gets interesting: I replaced every occurrence of 2001 with ORG_YEAR, a macro I defined as:





#define ORG_YEAR (2000 - 1901)





I also subtracted 1900 from yy right after input, to compensate. This made it return one day earlier than the right day for every year I tested less than 2000. (So day[7 - wk] returns the right answer, for years in the 1900's.)





For years 2008-2011 day[7 - wk - 1] returned the correct answer. For all others %26gt;= 2000 the amount of inaccuracy changes depending on input.





Oh hmm, the above only applies for dates in Jan, Feb and March...





That's as far as I have time to take it (actually, somewhat farther, but it was interesting.)





If I were trying to do this for real, I'd make the working calcs part into a function (and handle UI separately), and then I'd write a unit test to iterate through time, comparing its results to the built-in C runtime and Windows date/time stuff -- testing by hand via i/o streams is too tedious and time-consuming. Also difficult to see patterns testing one date at a time.





Good Luck! :-)
Reply:I think "That Guy" has a point. You have not done any calculations for checking centuries.





"That Guy":





I think 'extra days' means, number of days over completed weeks.


ex: if days%7 gives 0, the printed will be day[0]=Monday or day[7-0-1]=day[6] i.e. Sunday
Reply:i don't see a code for calculating the day.You have only taken data in this code.Where is actual logic of getting day.
Reply:It's late and you lost me with the whole "extra days" thing but you do know the rule about leap days in a century year, right? Leap year every 4 years normally. However if a year is even divisible by 100 AND 400 it is a leap year otherwise it isn't. Therefore 2000 had a Feb 29th but 1900, 1800, etc., didn't.


No comments:

Post a Comment