I\'m trying to write a code which, given a date in format mm dd yyyy is able to
ID: 3623368 • Letter: I
Question
I'm trying to write a code which, given a date in format mm dd yyyy is able to compute which day of the week this date fell on. Here are the program requirements:
The Program
Your program should consist of only one function, the main function. When
your program begins, the user is prompted to enter integer values representing
the month, day, and year, in that order. You do not need to validate the
date. The user’s entry is assumed to be a valid date. Your program should
then display [day-of-the-week-in-words], [month-in-words] [day], [year] and
indicate whether this date occurs in a leap year. Use a boolean variable
as a flag to indicate whether the year is a leap year or not.(The calendar
generator at http://www.timeanddate.com/calendar may be helpful to
you in verifying the correctness of your program.)
Additional Requirements
Declare two string variables to store the day-of-the-week-in-words and monthin-
words. A typical program interactions would be:
Sample Run 1:
Enter numeric values for month, day and year of a 10 31 1990
Wednesday, October 31, 1990 occurred in a non-leap year.
Sample Run 2:
Enter numeric values for month, day and year of a 2 13 2004
Friday, February 13, 2004 occurred in a leap year.
what I have written so far:
#include
using namespace std;
int month_of_the_year, day_of_week, year, century, x, u, y ;
int main()
{
cout << "Enter the numeric values for month, day, and year of the date. ";
cin >> month_of_the_year >> day >> year;
if(month_of_the_year == 1 || month_of_the_year == 10)
x = 0;
else if(month_of_the_year == 2|| month_of_the_year == 3 || month_of_the_year == 11)
x = 3;
else if(month_of_the_year == 5)
x = 1;
else if(month_of_the_year== 6)
x = 4;
else if(month_of_the_year == 8)
x = 2;
else if(month_of_the_year == 4 || month_of_the_year == 7)
x = 6;
else if(month_of_the_year == 9 || month_of_the_year == 12)
x = 5;
// Determination of month values complete
day_of_week = y %7;
if (day_of_week == 0)
cout << "Sunday" << endl;
else if (day_of_week == 1)
cout << "Monday" << endl;
else if (day_of_week == 2)
cout << "Tuesday" << endl;
else if (day_of_week == 3)
cout << "Wednesday" << endl;
else if (day_of_week == 4)
cout << "Thursday" << endl;
else if (day_of_week == 5)
cout << "Friday" << endl;
else if (day_of_week == 6)
cout << "Saturday" << endl;
}
return 0;
}
1) I was given a few formulas to help in the computing of this program.
---u=2(3-century mod 4) where century is the first two digits of the year, ---for example century is 20 for the year 2011.
2) v= the last two digits of the year, for example v is 11 for the year 2011.
3) w= v/4, using integer division, for example v=11/4 = 2 for 2011.
4) x was determined from a given table.
5) y = u + v + w + x + day. example y = 6 + 11 + 2 + 3 + 3 = 25
6) day of the week = y mod 7 which equals 4. Note 0 = sunday, 1 = monday, ...6 = ---saturday
* I feel as though I have not done much calculation yet, but I'm trying to understand how the program flows. I do not know how to assign the values to century and v. Furthermore the leap year part also confuses me. Descriptions as to the flow of the program are greatly appreciated. Thanks.
Explanation / Answer
please rate - thanks
you were missing a step, I found the algorithm on line If the Month is Jan. or Feb. and the Year is a leap year, subtract 1.
#include<iostream>
using namespace std;
int main()
{int month_of_the_year, day_of_week, year, century, x, u,v,w, y,day ;
string dow,month;
bool leap;
cout << "Enter the numeric values for month, day, and year of the date. ";
cin >> month_of_the_year >> day >> year;
u=2*(3-(year/100)%4);
v=year%100;
w=v/4;
if ( month_of_the_year== 1)
{month= "January" ;
x = 0;
}
else if ( month_of_the_year == 2)
{month= "February";
x = 3;
}
else if ( month_of_the_year == 3)
{month="March";
x = 3;
}
else if ( month_of_the_year== 4)
{ month="April";
x = 6;
}
else if ( month_of_the_year == 5)
{month="May";
x = 1;
}
else if ( month_of_the_year == 6)
{month= "June";
x = 4;
}
else if ( month_of_the_year == 7)
{month="July";
x = 6;
}
else if ( month_of_the_year == 8)
{month="August";
x=2;
}
else if ( month_of_the_year== 9)
{month= "September";
x=5;
}
else if ( month_of_the_year == 10)
{month= "October";
x = 0;
}
else if ( month_of_the_year == 11)
{month= "November";
x = 3;
}
else
{month= "December";
x=5;
}
// Determination of month values complete
//leapyear
if((year%4==0&&year%100!=0)|| year%400==0)
leap=true;
else
leap=false;
y=u+v+w+x+day;
if(month_of_the_year<3&&leap)
y--;
day_of_week = y %7;
if (day_of_week == 0)
dow= "Sunday" ;
else if (day_of_week == 1)
dow= "Monday";
else if (day_of_week == 2)
dow= "Tuesday";
else if (day_of_week == 3)
dow= "Wednesday";
else if (day_of_week == 4)
dow= "Thursday";
else if (day_of_week == 5)
dow= "Friday";
else
dow= "Saturday";
cout<<dow<<", "<<month<<" "<<day<<", "<<year<<" occurred in a ";
if(leap)
cout<<"leap year. ";
else
cout<<"non leap year. ";
system("pause");
return 0;
}