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. ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    Question Algorithm help

    I need to figure out how to determine from an input date the number of days to a set date. For example:

    Code:
         int Month;
         int Day;
         int Year;
    
    
         cout << "Enter a starting date <mm dd yyyy>" << endl;
         cin >> Month >> Day >> Year;
    
    
         ...
    I have the code written to validate a good date, account for leap year, etc.... I just do not know how to mathmatically work through the rest of the problem.

    I DO NOT want code, I want to do the code myself...I do however need help working through the logic of the problem.

    Thank you for your help and understanding.
    Last edited by mmyers1; 04-04-2004 at 05:03 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The time library of C++ just picked an arbitrary date years ago as its starting point. That might be acceptable for you if this program is working with birthdays or appointments.
    FAQ

    "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.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    What the project is supposed to accomplish is to input a date and have the program return the number of days until Christmas 2005.

    The accepted starting date is 1/1/1980. This forces the consideration of leap years.

    If I can figure out the logic to make this happen, I feel very confortable that I can come up with the code. My brain is just not working out the logic for some reason.

    Thanks to everyone who have responded....


    Mark

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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

    number of partial days:
    jan 31 - 19 = 12
    dec = 25

    total = 365 + 1 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 31 + 30 + 31 + 30 + 12 + 25

    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.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Quote Originally Posted by mmyers1
    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
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21