C++ Intro

Assignment: Serial Julian Date


Description

In this assignment you will write a program that uses dates of Gregorian calendar, which is the calendar commonly used today. In doing so, you will have a chance to create a number of useful C++ functions to convert external date to/from serial Julian date, which is simply an integer that stores the number of days since November 24 4714 BC, otherwise known as the Julian Period, or January 1st 4713BC in the Julian Calendar.

You will also explore the functionality of C++ streams while prompting, validating, and processing user input of external dates. One week is the time frame to complete your work. Start early, since you may discover things looking trivial at the beginning appear to take longer time to implement than you originally expected. Solutions to the given problems will be available at the next lecture.

Part 1. Getting started

  1. Input three integer variables from the user, corresponding to the month, day, and year. Don't worry about any input validation at this stage of your project.

  2. Create a new function, such as
    int serial_julian_date( int Month, int Day, int Year );
    to calculate and return the serial Julian date nDate as follows:










  3. All divisions in these formulas are integer divisions. The solution perfectly accounts for all leap years and varying month lengths to produce a unique serial number for each and every day. The limitation is only the numerical capacity of the integer data type on your machine.

  4. You must pay some special attention to the operator precedence when translating the above equations to the C++ expressions. Use parentheses were appropriate, and if in doubt about the order of calculation.

  5. Test your program with a few dates, you should be able to obtain the following serial dates:
         7/4/1776   2369916
        12/31/2000  2451910
         2/8/2007   2454140
         2/9/2007   2454141
                            
    Suggestion: your program should implement an endless loop, asking user for input and printing the calculated serial date on the screen, then repeat.

Part 2. Back to Gregorian calendar

  1. Add three more functions to your program,
        int serial_2_month( int nDate );
        int serial_2_day( int nDate );
        int serial_2_year( int nDate );
    


    where nDate is the value calculated by the serial_julian_date( ) function from above. The new functions should return calendar month, day, and year, respectively.

  2. Performing the inverse calculations for the Gregorian calendar can be achieved using the following formulas:











    Here, a, b, c, d, e, and m are temporary variables. Month, Day, and Year are the results that you have to return from your functions.

  3. Again, operator precedence and order of evaluation of the expressions are paramount in those calculations. Since we have to deal with integer division,
        int Month = m + 3 - 12 * ( m / 10 ); // CORRECT
    and
        int Month = m + 3 - ( 12 * m ) / 10; // *** WRONG!
    give different results, of which the last version is incorrect due to misplaced parentheses.

  4. Add new function calls to your main loop and display the results on the screen. Verify that inverse calculations return expected results.

Part 3. Input validation and other improvements

Consider validating the user input as follows:

  1. Verify that the month range is correct. If not, tell the user.

  2. Verify that the day range of the month is correct. If not, tell the user. Again, you can use a switch statement based on the integer month's value to do your checks.

  3. February is a bit tougher case to verify, because in leap year it will have 29 days. Here is a simple function to determine if the year entered is a leap year or not:
    bool is_leap_year( int Year ) {
        if ( ( Year % 100 ) == 0 ) {
            return ( ( Year % 400 == 0 ) ? true : false );
        } else {
            return ( ( Year % 4 == 0 ) ? true : false );
        }
    }
    
  4. To turn in your homework, please submit your program before the next lecture.

  5. To learn more about calendars, visit http://www.tondering.dk/claus/cal/node3.html.