Thread: Subtracting Days after Sorting

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    25

    Subtracting Days after Sorting

    After people are sorted in order of their birthdays, how can I subtract two birthdays to find out the days in between so I can know if the person to the left or the right of someone has the closest birthday to them?

    example: JAN 31 FEB 2 FEB 15
    Person1 Person2 Person3

    Person2 has closest b-day to person1.

    In case you wonder about the leap year, February is only be counted as 28 days, unless someone has that b-day.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What format are the dates? And how do you connect each birthday with a person?

    If the array is sorted you could just subtract the index before and after the date you are interested in if I'm not missing anything.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    There is an input file that says the birthdays of each person.
    The date in the input file would be written as JANUARY 31 1989, but the year doesn't matter because it's sorted by birthdays, not by age.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Hikaru90 View Post
    There is an input file that says the birthdays of each person.
    The date in the input file would be written as JANUARY 31 1989, but the year doesn't matter because it's sorted by birthdays, not by age.
    Ok, but presumably you will read this into memory at some point and you can not sort month alphabetically as december would appear before january for example. So you will need something like a person struct to hold things like date, name etc. If the date is then stored numerically you can sort by the date field of the person struct.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    store dates using time_t var and use difftime function to compare them
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    I'm just trying to get the idea of how I would do this, and I said I was talking about after the sorting, which also means after reading in the input. The input was read in the way it is in the file because I need to output it in a similar way. After the birthdays are sorted, I want to know if the person to the left or the right is closer to the one in the middle, which can be known by how many days are in between them. I already have a struct with firstname, lastname, month, day, and year, although year is not really needed.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    month would be an array month[length], unless I changed it to an int or I could maybe set January to equal 1, but I don't know where I would go from there, or I could add up the days and determine the day of the year and then subtract, like Feb 1 would be the 32nd day of the year, but it's harder to do that for months later in the year. This would maybe be easier if all the months had the same amount of days.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    we're supposed to use strcmp if the person has the same b-day as someone else.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by Hikaru90 View Post
    There is an input file that says the birthdays of each person.
    The date in the input file would be written as JANUARY 31 1989, but the year doesn't matter because it's sorted by birthdays, not by age.
    I think it does matter. Because someone born in December of one year might be closest to a birthday of another person born in early January of the next year.

    But anyways... I'd set up an array
    [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 394, 334]

    These represent the number of days since the beginning of the year, when indexed by a month number 0 to 11 (January to December respectively).

    So to convert Jan 31, Feb 2, and Feb 15 to the number of days in the year, you index into the array according to the month, and add the day.

    Jan 31 --> 31 (0 + 31)
    Feb 2 --> 34 (31 + 2)
    Feb 15 --> 46 (31 + 15)

    The difference between the middle birthday and the one on the left is 34-31 which is 3.
    The difference between the middle birthday and the one on the right is 46-34 which is 12.
    So the first and second birthdays are closer to each other.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    25
    yeah I guess I would have to do something different for December because the problem says it's not sorted by year, only month and day for the birthdays. Maybe I can make another function or if statements for that case. Anyway, I guess I will try to use an array and do something like this after setting the array up.

    Code:
       dayofyear = month[i] + day;
       differenceleft = month[i] - month[i-1];
       differenceright = month[i+1] - month[i];
    
       if (differenceleft < differenceright)
          //middle person closest to person on left
       else if(differenceright < differenceleft)
          //middle person closest to person on right
    I guess that would be much better than doing this:

    #define JANUARY 1 and do that for each month

    Then something like:

    if (month == JANUARY)
    dayofyear = day;
    else if (month == MARCH)
    dayofyear = day+28+31;
    ...and so on

    Does anyone have any idea about counting the days between December 31 and Jan 1?
    Last edited by Hikaru90; 03-27-2010 at 03:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Advancing Days
    By nhubred in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 06:22 PM
  3. sorting days of the week
    By brodacz in forum C Programming
    Replies: 3
    Last Post: 08-13-2002, 12:36 PM
  4. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 11:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM