944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6285
  • C++ RSS
Jul 8th, 2005
0

C++ help with Julian day program revised

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 8th, 2005
0

Re: C++ help with Julian day program revised

Quote 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?
Quote ...
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
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 8th, 2005
0

Re: C++ help with Julian day program revised

C++ Syntax (Toggle Plain Text)
  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).
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 8th, 2005
0

Re: C++ help with Julian day program revised

Will try it. But the floor() is the formula given in the book.

thanks.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 8th, 2005
0

Re: C++ help with Julian day program revised

I tested it with a convertor I found somwhere, and it worked without the floor().
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array Problem
Next Thread in C++ Forum Timeline: Linked stack modification issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC