30 likes | 104 Vues
This code determines the year based on the number of days since Jan 1, 1980, accounting for leap years. Discover what it did on Dec 31, 2008, and learn how to fix a bug in the code.
E N D
What does this code do? year = ORIGINYEAR; while (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } } else { days -= 365; year += 1; } } Note: ORIGINYEARis1980 and days = number of days since Jan 1, 1980
What did this code do on December 31, 2008? year = ORIGINYEAR; while (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } } else { days -= 365; year += 1; } }
How can you fix it? year = ORIGINYEAR; while (days > 365) { if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } } else { days -= 365; year += 1; } }