Related questions with answers
(Program) A Julian date is represented as the number of days from a known base date. The following pseudocode shows one algorithm for converting from a Gregorian date, in the form month/day/year, to a Julian date with a base date of 00/00/0000. All calculations in this algorithm use integer arithmetic, which means the fractional part of all divisions must be discarded. In this algorithm, M = month, D = day, and Y = year.
If M is less than or equal to 2 Set the variable MP = 0 and YP = Y - 1 Else Set MP = int(0.4 * M + 2.3) and YP = Y EndIf T = int(YP / 4) - int(YP / 100) + int(YP / 400) Julian date = 365 * Y + 31 * (M - 1) + D + T - MP
Using this algorithm, modify Program 12.4 to cast from a Gregorian Date object to its corresponding Julian representation as a long integer. Test your program by using the Gregorian dates 1/31/2012 and 3/16/2012, which correspond to the Julian dates 734898 and 734943.
Solution
VerifiedThe definition of the function:
Date::operator long() {
// using the given algorithm: int mp, yp; if (month <= 2) { mp = 0; yp = year - 1; } else { mp = int(0.4 * month + 2.3); yp = year; }
int t = int(yp / 4) - int(yp / 100) + int(yp / 400);
// cast the return value to long since the operator long // function returns a value type of long return long(365 * year + 31 * (month - 1) + day + t - mp); }
Create an account to view solutions
Create an account to view solutions
More related questions
1/4
1/7