News UnrealEd Files Mods FragBU Liandri Archives
BeyondUnreal Forums

Go Back   BeyondUnreal Forums > BeyondUnreal > Off Topic > Hardware, Software & Troubleshooting

Reply
 
Thread Tools Display Modes
Old 28th Mar 2004, 08:15 PM   #1
Saito
Banned
 
Join Date: Dec. 30th, 2003
Posts: 1,223
c++ help

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.
__________________
It takes 2 people to have an argument, both are part of the problem.

Repeating the same opinion over and over again, will not make it a fact.

If you will only listen to opinions that enforce your own, than there is no point in voicing yours to begin with.
Saito is offline   Reply With Quote
Old 29th Mar 2004, 07:46 AM   #2
Eyegore
=================
 
Eyegore's Avatar
 
Join Date: Sep. 4th, 2001
Location: Hanover
Posts: 293
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.
Eyegore is offline   Reply With Quote
Old 31st Mar 2004, 02:48 AM   #3
bobtheking
Monkey in a bucket
 
bobtheking's Avatar
 
Join Date: Dec. 1st, 2001
Location: dx*dp >= h/4pi
Posts: 1,237
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.
Quote:
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.
Quote:
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 by bobtheking; 31st Mar 2004 at 02:59 AM.
bobtheking is offline   Reply With Quote
Old 31st Mar 2004, 05:31 AM   #4
Eyegore
=================
 
Eyegore's Avatar
 
Join Date: Sep. 4th, 2001
Location: Hanover
Posts: 293
Oops, you're right - sorry.

Quote:
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.
Eyegore is offline   Reply With Quote
Old 2nd Apr 2004, 12:29 PM   #5
Saito
Banned
 
Join Date: Dec. 30th, 2003
Posts: 1,223
thx
__________________
It takes 2 people to have an argument, both are part of the problem.

Repeating the same opinion over and over again, will not make it a fact.

If you will only listen to opinions that enforce your own, than there is no point in voicing yours to begin with.
Saito is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:51 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.

Copyright ©1998 - 2012, BeyondUnreal, Inc.
Privacy Policy | Terms of Use
Bandwidth provided by AtomicGamer