c++ help

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

Saito

Banned
Dec 30, 2003
1,223
0
0
ok I have an assignment that I don't understand.

I have to write a program that prompts the user for a date, and then displays how days ago it was from todays date. In julian days. It also has to continue to prompt for dates and process until the user wants to stop.

The algorithm for julian day number : Set jd, jm, jy to the day, month, and year. If the year is negative add 1 to jy (there is no year 0, year 1 BC was immediately followed by year AD 1). If the month is larger than february add 1 to jm. Otherwise, add 13 to jm and subtract 1 from jy. Then compute.

long jul = floor(365.25 * jy) + floor(30.6001 * jm) + d + 1720995.0

Store the result in a variable of type long (simple integers may not have enough digits to hold the value). If the date was before October 15, 1582 return this value, otherwise the following correction needs to be made:

int ja = 0,01* jy;
jul = jul + 2 -ja + 0.25 * ja ;

So I know I need to write a function

long julian(int year, int month, int day)

that converts the date into a julian day number. That's part of it. I just have no idea how to do this whole program.
 

Eyegore

=================
Sep 4, 2001
293
0
0
52
Hanover
You're algorithm looks frickin too complicated.

Id use a switch to test for the month and calculate the date based off that. Then add a feature to either test for leap year or have the user enter that it's leap year. If you generate and algorithm to test for leap year (i.e. leap years, assuming started 0 BC - (probably not) should be divisible by 4) so...

if (year%4 == 0)
leapyear = 1
else
leapyear = 0

You could actually calculate back to the first leap year by starting backwards from this year to get the actaul start date - iI just used 0 BC as example.

Then test what month it is - put in switch
Ex.

Code:
switch(month)
{
     case January:
            {
                days = day;
                break;
            }
     case February:
            {
                days = day + 31;
                break;
            }

      etc, ...
}

Technically julian date resets on Jan 1st i.e. January 1, xxxx is alway JD 1, xxxx - if you need to calculate day count from 0 BC - I guess you need more complicated algorithm

maybe brutish - but it'll work just fine just be sure to have something to test leapyear and you should be good.
 

bobtheking

Monkey in a bucket
Dec 1, 2001
1,237
0
0
dx*dp >= h/4pi
Visit site
Saito, your original method is fine.

here is a function using your method:
Code:
long julian(int year, int month, int day)
{
   double jy = year;
   double jm = month;
   double jd = day;

   if(year < 0)
      jy += 1;
   if(month > 2)
      jm += 1;
   else
   {
      jm += 13;
      jy -= 1;
   }

   long jul = floor(365.25 * jy) + floor(30.6001 * jm) + d + 1720995.0

   if(month < 10 && day < 15 && year < 1582)
      return jul;
   else
   {
      int ja = 0,01* jy;
      jul = jul + 2 -ja + 0.25 * ja ;
      return jul;
   }
}
but, if you choose to do this switch madness...

you can make the switch much easier:
Code:
int day;
...
edit: these needed to be shifted by 1:
switch(month)
{
   case December:
      day += days in november
   case November:
      days += days in october
   case October:
      days += days in september

   case Feburary:    
      days += days in january
   // don't need to do anything for january
}
notice the lack of breaks, they make that work. this will mean less work of adding up the days in each month before it. more work for the computer, but this doesn't exactly seem like a processor intensive program.
If you generate and algorithm to test for leap year (i.e. leap years, assuming started 0 BC - (probably not) should be divisible by 4) so...

if (year%4 == 0)
leapyear = 1
else
leapyear = 0
this won't work, because he has to add a day for every leap year between 'year' and 0 bc. using 365.25 should work fine. what you have only tells you if the current year is a leap year.
Store the result in a variable of type long (simple integers may not have enough digits to hold the value)
what kind of compiler is this? more importantly, what decade is it from? i don't know what julian dates are, but it doesn't appear they exceed 2 billion something, which is 2^31 (32 bits with sign bit). ints and longs on a modern compiler are usually the same size anyways.
 
Last edited:

Eyegore

=================
Sep 4, 2001
293
0
0
52
Hanover
Oops, you're right - sorry.

this won't work, because he has to add a day for every leap year between 'year' and 0 bc. using 365.25 should work fine. what you have only tells you if the current year is a leap year.