Algorithm help

This is a discussion on Algorithm help within the C++ Programming forums, part of the General Programming Boards category; I need to figure out how to determine from an input date the number of days to a set date. ...

>I just do not know how to mathmatically work through the rest of the problem
How are you going about it right now? A quick and easy (relatively) way is to convert your dates to the number of days in the year and then use simple arithmetic to find a difference.

I was thinking something along those lines, but am not sure how to go about that. For example if I were to enter 4 4 2004, how many days is it until 12/25/2005? How would I convert 4 4 2004 to an integer? What if the date were 1 1 1980?

>How would I convert 4 4 2004 to an integer?
Take the total number of days up to April and then add 4. That's how many days have elapsed this year.

>how many days is it until 12/25/2005
Find the number of days from January 1st 2005 to December 25th (no problem I hope ) and then add the difference from the total number of days in 2004 and the days that have already passed.

>What if the date were 1 1 1980?
What if it were? There's no difference except you have several full years in the middle to consider. The only crux part is leap years.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

one way is to use the ctime (or time.h) file to help you. I forget the exact name of the function but in ctime is a function called t_diff (or something like that) that calculates the difference from current date to start of specific starting date in seconds, or milliseconds or something like that. You can use this function to calculate two differences, one for currentDate and one for Christmas 2005. Then you can calculate the difference in length between the two differences to get the time in seconds (or milliseconds) between the two dates. Then use the number of seconds (or milliseconds) in one day to determine the number of days between the two dates.

If using ctime isn't allowed in the exercise, then you can do a similar thing yourself. Alternatively, doing it yourself, it might be easier to determine the number of full years between the two desired dates. Determine how many of those years are leap years and compensate by one day for each leap day included. Then determine which full months separate the two dates, and add the number of days for each full month. Then determine the number of days in the two partial months. Then add em all up. For example:

start:
Jan 19, 2003

stop:
Dec 25, 2005

full years between Jan 19 2003 and Dec 25, 2003 is one---2004---= 365 days

number of leap years in between 1 = 1 day

number of full months in between:
feb 2005 = 28 days
march 2005 = 31 days
april 2005 = 30 days
may 2005 = 31 days
june 2005 = 30 days
july 2005 = 31 days
aug 2005 = 31 days
sept 2005 = 30 days
oct 2005 = 31 days
nov 2005 = 30 days

with a little expansion, (for example, how do you determine which of the two dates comes first, and how to determine the number of full years between the two dates---the solutions to both questions is similar) I think this technique can be done successfully without problem, too.

I was thinking something along those lines, but am not sure how to go about that. For example if I were to enter 4 4 2004, how many days is it until 12/25/2005? How would I convert 4 4 2004 to an integer? What if the date were 1 1 1980?

This is getting my goat right now.....

4/4/2004 would be day 0.
Count the number of days left in the current month (April) --> total.
Start a loop beginning at the next month and start adding the number of days in each month to total
When you reach Feb, check if leap year and add 1
Stop loop when you finish with Nov 2005
Add the final days for Dec 25