Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After people are sorted in order of their birthdays, how can I subtract two birthdays to find out the days in between so that 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

Result: Person2 has closest birthday to person1.

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

Should I make January = 1, like should I put
#define JANUARY 1 and do that for each month?

Then could I do something like:
if (month == JANUARY)
  dayofyear = day;
else if (month == MARCH)
  dayofyear = day+28+31;
...and so on


*C programming only.

Thanks, yeah I finally thought of something like that:
int dayOfYear = dayOfMonth + monthOffset[monthNumber];


but didn't realize about December 31 and Jan 1. People are being sorted by their birthdays, not age, so the problem says to ignore the year for that so I guess the year doesn't come into play. I guess I will just have to come up with another function or if statement for December
Posted
Updated 26-Mar-10 14:16pm
v7

What is your trouble about?
A little more than a switch will do the job.
 
Share this answer
 
Hi,

you can base this on day-of-year, which can be calculated as a simple table look-up:
int dayOfYear = dayOfMonth + monthOffset[monthNumber];


where monthOffset is an array holding 12 constants, starting with 0 and 31. You may have two of them, one for leap years, one regular.

However, you have a potential problem around New Year. When a time span exceeds half a year, you should mirror it to the next year.

:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900