Write a program that determines the day of the week for a given date. You can in
ID: 664979 • Letter: W
Question
Write a program that determines the day of the week for a given date. You can invent your own complex algorithm that takes into account the special leap year rules, and changes in calendars, but this is a case where it makes sense to look for things that are familiar. Who else might need to compute values from dates over a wide span of time? Historians work with dates, but generally don’t compute from them. Astronomers, however, need to know the differences in time between orbital events in the solar system that span hundreds of years. Consulting an astronomy text, you will find that there is a standard way or representing a date, called the Julian Day Number (JDN). This is the number of days that have elapsed since January 1, 4713 BC. Given the JDN for a date, there is a simple formula that tells the day of the week:
DayofWeek = (JDN + 1) % 7
The result is in the range of 0 to 6, with 0 representing Sunday.
The only remaining problem is how to compute the JDN, which is not so simple. The algorithm computes several intermediate results that are added together to give the JDN. We look at the computation of each of these three intermediate values in turn.
If the date comes from the Gregorian calendar (later than October 15, 1582), then compute intRes1 with the following formula, otherwise let intRes1 be zero.
intRes1 = 2 – year / 100 + year / 400 (integer division)
The second intermediate result is computed as follows:
intRes2 = int(365.25 * year)
We compute the third intermediate value with this formula:
intRes3 = int(30.6001 * (month + 1) )
Finally, the JDN is computed this way:
JDN = intRes1 + intRes2 + intRes3 + day + 1720994.5
Your program should make appropriate use of value-returning functions in solving this program. These formulas require nine significant digits; you may have to use the integer type long and the floating-point type double. Your program should prompt appropriately for input of the date, and properly label the output. Use proper coding style with comments to document the algorithm as needed.
Please consider leap years and make sure the output is a valid date.
Explanation / Answer
Code:
#include <string>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
//function that determines the day of the week
void CalcWeekDay(int day,int mon,int yr, int JulianDate, int intR1,
int intR2, int intR3,int& DayofWeek);
int main()
{
//Input variables
float day;
float mon;
float yr;
int DayofWeek;
int intR1 = 0;
int intR2 = 0;
int intR3 = 0;
int JulianDate = 0;
bool dataOK = false;
cout << "Please enter the month as a number: ";
cin >> mon;
cout << "Please enter the day of the month as a number: ";
cin >> day;
cout << "Please enter the year as a number: ";
cin >> yr;
if (mon<13)
dataOK = true;
else
cout << "Please enter a number less than 13 for the month: " << endl;
if (day<32)
dataOK = true;
else
cout << "Please enter a number less than 32 for the day:" << endl;
if((yr>1582) || (mon>10) && (yr==1582) || (mon== 10) && (day>15) && (yr== 1582))
while (dataOK== false);
CalcWeekDay (day, mon, JulianDate, yr, intR1, intR2, intR3, DayofWeek);
cin.get(), cin.get();
return 0;
}
//********************************************************************************************************************************
void CalcWeekDay (int day,int mon,int yr, int JulianDate, int intR1,
int intR2, int intR3,int& DayofWeek)
{
intR1 = (2-yr / 100 + yr / 400);
intR2 = int(365.25 * yr);
intR3 = int(30.6001 * (mon + 1));
JulianDate = ((intR1 + intR2 + intR3 + day) + 1720944.5);
DayofWeek = ((JulianDate + 3) % 7);
cout << " The day of the week is: " << DayofWeek;
if (DayofWeek == 0)
cout << "Sunday";
else if (DayofWeek == 1)
cout << "Monday";
else if (DayofWeek == 2)
cout << "Tuesday";
else if (DayofWeek == 3)
cout << "Wednesday";
else if (DayofWeek == 4)
cout << "Thursday";
else if (DayofWeek == 5)
cout << "Friday";
else if (DayofWeek == 6)
cout << "Saturday";
cout << endl;
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.