Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Coding language is C++ Write a program to accept any two dates in the form of mo

ID: 3802061 • Letter: C

Question

Coding language is C++

Write a program to accept any two dates in the form of month day year (8 23 2000), separated by spaces, and calculate the total number of days that has elapsed between the two dates, inclusive of the beginning and ending days. Remember that leap year is a year divisible by 4, except for centennial years, which must be divisible by 400, such as 1600 or 2000 (1800 is not a leap year). You can test your program with any typed in dates (or dates stored in a file) but it must finally run with the data shown below set up in a file. Print the output (dates and total days) on the screen. You must read in the following data from a file.

Be sure to show the original dates and the days between the dates on your output.

DATA:                                            ANSWERS:

7 4 1776            1 1 1987            (76882)

2 1 1983            3 15 1984         (409)

7 7 1983            11 4 1983         (121)

7 3 1983            7 20 1983         (18)

12 25 1990       12 31 1992       (738)

Program must continue running until all the data is processed – use a loop 1 to 6 or a while (month != -1) or while (!fin.eof()) - choose your own end of data technique. Read the data from a defined data file – do not type in the data when the program runs.

Use if statements, switch statements, loops and functions to solve this problem. Do not use the Gregorian date method!

Your program must include at least four functions, not including the main method.

Explanation / Answer

Here is the code for your reference:

#include<iostream>

using namespace std;

// A date has day 'd', month 'm' and year 'y'

struct Date

{

    int d, m, y;

};

// To store number of days in all months from January to Dec.

const int monthDays[12] = {31, 28, 31, 30, 31, 30,

                           31, 31, 30, 31, 30, 31};

// This function counts number of leap years before the

// given date

int countLeapYears(Date d)

{

    int years = d.y;

    // Check if the current year needs to be considered

    // for the count of leap years or not

    if (d.m <= 2)

        years--;

    // An year is a leap year if it is a multiple of 4,

    // multiple of 400 and not a multiple of 100.

    return years / 4 - years / 100 + years / 400;

}

// This function returns number of days between two given

// dates

int getDifference(Date dt1, Date dt2)

{

    // COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1'

    // initialize count using years and day

    long int n1 = dt1.y*365 + dt1.d;

    // Add days for months in given date

    for (int i=0; i<dt1.m - 1; i++)

        n1 += monthDays[i];

    // Since every leap year is of 366 days,

    // Add a day for every leap year

    n1 += countLeapYears(dt1);

    // SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'

    long int n2 = dt2.y*365 + dt2.d;

    for (int i=0; i<dt2.m - 1; i++)

        n2 += monthDays[i];

    n2 += countLeapYears(dt2);

    // return difference between two counts

    return (n2 - n1);

}

// Driver program

int main()

{

    Date dt1 = {1, 2, 2000};

    Date dt2 = {1, 2, 2004};

    cout << "Difference between two dates is " << getDifference(dt1, dt2);

    return 0;

}