Answer to Question #122287 in C++ for Sheikh Tahir

Question #122287
Write a function num_indep_days() which computes the number of days elapsed since 14th August 1947 to the date provided as input to the function. Remember that February of each leap year has 29 days! The function prototype is given below:
int num_indep_days(int day, int month, int year);
1
Expert's answer
2020-06-16T06:37:04-0400
#include<iostream>

using namespace std;

int countLeapYears(int years,int month)

{
    // Check if feburary has passed in that date

    // if yes, the year can taken to calculate

    //leap yeara

    if (month <= 2)

        years--;


    // A leap year is a multiple of 4,

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

}

// To store number of days in each month.

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

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



// This function returns number of days between given

// date and 14th august 1947

int num_indep_days(int day, int month, int year)

{

    //intializing no. of days using year and day

    long int d2 = year*365 + day;



    // Add days of months in given date

    for (int i=0; i<month - 1; i++)

        d2 += monthDays[i];


    // Since every leap year is of 366 days,

    // Add one day for every leap year

    d2 += countLeapYears(year,month);


    //similarly count the days of 14th august 1947

    long int d1 = (1947*365) + 14;

    for (int i=0; i<7; i++)

        d1 += monthDays[i];

    d1 += countLeapYears(1947,8);

    // return difference between two counts

    return (d2 - d1);

}

int main()

{

    //intializing the variables of date

    int day=15;

    int month=6;

    int year=2020;

    //calling the num_indep_days

    cout << "Difference between two dates is " << num_indep_days(day,month,year) << " days.";

    return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS