I need some assistance with my code for this problem.
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 difference 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 of representing a date, called the Julian Day Number (JDN). This value is the number of days that have elapsed since January 1, 4713 B.C. 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 in not so simple. The algorithm computes several intermediate results that are added together to give 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 dates comes from the Gregorian calendar (later than October 15, 1582), then compute 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 the problem. 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 the user appropriately for input of the date; it should also properly label the output. Use proper coding style with comments to document the algorithm as needed.
Below is my code. I use 12-7-1941 as my date. I should get an output showing Sunday, but instead it shows Friday.
#include <string>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
//Determine Function Prototypes
void DetermineWeekDay(float, float, float, int, int, int, int, int&);
int main()
{
//Input variables
float day;
float month;
float year;
int DayofWeek;
int intRes1 = 0;
int intRes2 = 0;
int intRes3 = 0;
int JDN = 0;
bool dataOK = false;
do
{
cout << "Please enter the month ";
cin >> month;
cout << "Please enter the week day: ";
cin >> day;
cout << "Please enter the year: ";
cin >> year;
if (month<13)
dataOK = true;
else
cout << "Please enter a number less than 13 for the month: "
<< endl;
if (day<32);
else
cout << "Please enter a number less than 32 for the day: "
<< endl;
if((year>1582) || //Picks any years from Gregorian Calendar greater than 1582
(month>10) && (year==1582) || //Picks months greater than 10 in 1582 from Gregorian Calendar
(month== 10) && (day>15) && (year== 1582)) // Picks only month 10 and any days over 15 in year 1582 from Gregorian Calendar
{
// do calculation
}
} while (dataOK== false);
DetermineWeekDay (day, month, JDN, year, intRes1, intRes2, intRes3, DayofWeek);
cin.get(), cin.get();
return 0;
}
void DetermineWeekDay (float day,float month,float year, int JDN, int intRes1,
int intRes2, int intRes3,int& DayofWeek)
{
//Precondition: The day of the week must be entered.
//Postcondition: The user must enter information correctly.
intRes1 = (2-year / 100 + year / 400);
intRes2 = int(365.25 * year);
intRes3 = int(30.6001 * (month + 1));
JDN = ((intRes1 + intRes2 + intRes3 + day) + 1720944.5);
DayofWeek = ((JDN + 3) % 7);
cout << " The day of the week for "
<< "month "<<"day "<<"year " << "is" << DayofWeek;
if (DayofWeek == 0)
cout << " Monday";
else if (DayofWeek == 1)
cout << " Tuesday";
else if (DayofWeek == 2)
cout << " Wednesday";
else if (DayofWeek == 3)
cout << " Thursday";
else if (DayofWeek == 4)
cout << " Friday";
else if (DayofWeek == 5)
cout << " Saturday";
else if (DayofWeek == 6)
cout << " Sunday";
cout << endl;
return;
}
Output Notice how jumbled the last line is? Why?
Please enter the month 12
Please enter the week day 7
Please enter the year 1941
The day of the week for month day year is 4 Friday