c++ Many computer applications, such as Microsoft Excel, can compare date values
ID: 3751199 • Letter: C
Question
c++
Many computer applications, such as Microsoft Excel, can compare date values that occur after January 1, 1900. For example, these programs can determine if 06/06/99 is less than (comes before) 11/01/00. They use January 1, 1900 as their reference point. This becomes day 1. All other dates are calculated as to the number of days they are relative to January 1, 1900. For example January 2, 1900 is equal to 2. October 31, 2000 is equal to 36829. Notwithstanding, Excel cannot tell you which day of the week these two dates fell on.
For this assignment, you are to write a C++ program which will:
1.Have the User enter in a date.
2.Calculate the "day value" of that date. For this you will need to write several functions as stated below.
a.A function that determines if a year is a leap year.
b.A function that determines the number of days in a month. Remember if the year is a leap year then February has 29 days. You will need the leap year function. This is an example of a function calling another function.
c.A function that determines the number of days in the year of the date in question. This function uses the days in month function.
d.Based on the "day value" of the date, and the fact that January 1, 1900 was Monday, write another function that prints the day of the week that date fell on
Ex. For 10/31/2000 you need to calculate the number of days from 01/01/1900 to12/31/1999, and then calculate the days from 01/01/2000 to 10/31/2000. This will give you the "day value". Use a modulus 7 division on this day value to find the day of the week the date falls out on.
e.Your input file will have at least twenty dates to process. You will read up to the end of file.
3.Your program should print a line of code for each date to the console as follows:
Tuesday 10/31/2000 has a day value of 36829
Explanation / Answer
CODE :
#include<iostream>
using namespace std;
// A date has day 'd', month 'm' and year 'y'
struct Date
{
int m, d, 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};
const string days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int isLeapYear(int y) {
if (y % 4 == 0)
{
if (y % 100 == 0)
{
if (y % 400 == 0)
return 1;
else
return -1;
}
else
return 1;
}
return -1;
}
int daysInMonth(int y, int m) {
if(isLeapYear(y) == 1) {
if(m == 2) {
return 29;
} else {
return monthDays[m - 1];
}
}
return monthDays[m - 1];
}
int daysInYear(Date dt) {
int numDays = 0;
for(int i=1; i<dt.m; i++) {
numDays += daysInMonth(dt.y, i);
}
numDays += dt.d;
return numDays;
}
// 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);
}
void getTotalDays(Date dt) {
Date dt1 = {1, 1, 1900};
Date dt2 = {12, 31, dt.y-1};
int numDays = getDifference(dt1, dt2);
numDays += daysInYear(dt) + 1;
string day = days[numDays % 7];
cout << day << " " << dt.m << "/" << dt.d << "/" << dt.y << " has a day value of " << numDays << endl;
}
int main()
{
Date dt = {10, 31, 2000};
getTotalDays(dt);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.