C++ help with Julian day program revised

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

C++ help with Julian day program revised

 
0
  #1
Jul 8th, 2005
Below is my new code for this julian day program. I have fixed some of the errors I mentioned earlier. However, the day is still one short for the julian day and my conditional if statement seems to be the problem. I have tried a couple of ways to fix it. Any suggestions.

Thanks.
  1. // Assignment #3, Programming I, Summer 2005
  2. // Name: Darius Juan Bloomer
  3.  
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. long julian ( int year, int month, int day );
  13.  
  14. int main()
  15. {
  16. const long int julian_day = 2450000;
  17. int year = 0;
  18. int month = 0;
  19. int day = 0;
  20. int t_year = 0;
  21. int t_month = 0;
  22. int t_day = 0;
  23.  
  24.  
  25. cout << " Enter a year, month, day ( ex. 1323 11 30 or -1400 8 11): ";
  26. cin >> year;
  27. cin >> month;
  28. cin >> day;
  29. cout << endl << endl;
  30.  
  31.  
  32. long julian_date = julian( year, month, day);
  33.  
  34. cout << julian_date << endl;
  35.  
  36.  
  37.  
  38. cout << " What is today's date in the same format(ex. 2005 2 22): ";
  39. cin >> t_year;
  40. cin >> t_month;
  41. cin >> t_day;
  42.  
  43.  
  44. long today_date = julian( t_year, t_month, t_day);
  45. long difference = today_date - julian_date;
  46.  
  47. cout << difference << endl << endl;
  48.  
  49. return 0;
  50. }
  51.  
  52.  
  53.  
  54. long julian ( int year, int month, int day)
  55. {
  56.  
  57. int j_year = year;
  58. int j_month = month;
  59. int j_day = day;
  60.  
  61. if ( j_year < 0)
  62.  
  63. j_year += 1;
  64.  
  65. if ( j_month > 2)
  66.  
  67. j_month += 1;
  68.  
  69. else
  70. {
  71. j_month += 13;
  72. j_year -= 1;
  73. }
  74.  
  75. long jul =static_cast<long>(floor(365.25 * j_year) + floor(30.6001 * j_month) + j_day + 1720995.0);
  76.  
  77.  
  78. // if (j_year <= 1582) {
  79. // if (j_month <= 10)
  80. // if ( j_day < 15 )
  81.  
  82. if ( j_year < 1582 || ( j_year == 1582 && (j_month < 10 || ( j_month == 10 && j_day < 15))))
  83. return jul;
  84.  
  85.  
  86. else
  87. {
  88. int ja = static_cast<int>(0.01 * j_year);
  89. jul += static_cast<int>(2 - ja + 0.25 * ja);
  90.  
  91. return jul;
  92. }
  93.  
  94. }
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ help with Julian day program revised

 
0
  #2
Jul 8th, 2005
Originally Posted by djbsabkcb
Below is my new code for this julian day program. I have fixed some of the errors I mentioned earlier. However, the day is still one short for the julian day and my conditional if statement seems to be the problem. I have tried a couple of ways to fix it. Any suggestions.
Could you better describe the problem?
C:\Test>testpp
Enter a year, month, day ( ex. 1323 11 30 or -1400 8 11): 2005 7 7


2453559
What is today's date in the same format(ex. 2005 2 22): 2005 7 8
1


C:\Test>testpp
Enter a year, month, day ( ex. 1323 11 30 or -1400 8 11): 2005 7 6


2453558
What is today's date in the same format(ex. 2005 2 22): 2005 7 8
2
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: C++ help with Julian day program revised

 
0
  #3
Jul 8th, 2005
  1. long jul =static_cast<long>(floor(365.25 * j_year) + floor(30.6001 * j_month) + j_day + 1720995.0);
first of. 1720995.0 shuld be changed to 1720994.5
and second. remove the floor() - and it is doing it corectly.

>Could you better describe the problem?
As far as I know it is showing 1 day less in some years, (not when calculating, but the julian date is 1 less than is shuld be).
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Re: C++ help with Julian day program revised

 
0
  #4
Jul 8th, 2005
Will try it. But the floor() is the formula given in the book.

thanks.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: C++ help with Julian day program revised

 
0
  #5
Jul 8th, 2005
I tested it with a convertor I found somwhere, and it worked without the floor().
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 4552 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC