This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's
Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.
General Coding and Design =>
Calculator Programming
Author |
Message |
|
bananaman Indestructible
Calc Guru
Joined: 12 Sep 2005 Posts: 1124
|
Posted: 07 Nov 2006 07:51:43 pm Post subject: |
|
|
I need to know a good algorithm for determining what day of the week the first day of each month occurs. Does anyone know a good one for doing this? |
|
Back to top |
|
|
JoeImp Enlightened
Active Member
Joined: 24 May 2003 Posts: 747
|
Posted: 07 Nov 2006 11:42:32 pm Post subject: |
|
|
http://www.mcs.csuhayward.edu/~malek/Mathlinks/Weekdays.html
Use the GetDay function below as needed.
Code: #include <iostream>
#include <string>
#include <math.h>
using namespace std;
int GetDay(int day, string month, string year)
{
if (year.length() != 4)
{
cout << "Year is out of bounds";
return NULL;
}
int d = day;
int y = atoi(year.substr(2,2).c_str());
int c = atoi(year.substr(0,2).c_str());
int m = -1;
if (month == "March") { m = 1; }
if (month == "April") { m = 2; }
if (month == "May") { m = 3; }
if (month == "June") { m = 4; }
if (month == "July") { m = 5; }
if (month == "August") { m = 6; }
if (month == "September") { m = 7; }
if (month == "October") { m = 8; }
if (month == "November") { m = 9; }
if (month == "December") { m = 10; }
if (month == "January") { m = 11; y--; }
if (month == "February") { m = 12; y--; }
if (m == -1)
{
cout << "Invalid month";
return NULL;
}
return ((d + (int)(floor((2.6 * m) - .2)) - (2 * c) + y + (int)floor(y/4) + (int)floor(c/4))%7);
}
int main()
{
string DayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday" };
int day;
string month, year;
cout << "Enter day: ";
cin >> day;
cout << "Enter month: ";
cin >> month;
cout << "Enter year: ";
cin >> year;
if (int result = GetDay(day, month, year))
{
cout << "Day of the week: " << DayOfWeek[result];
}
return 0;
}
Sample Execution:
Code: Enter day: 8
Enter month: November
Enter year: 2006
Day of the week: Wednesday
Last edited by Guest on 08 Nov 2006 12:45:11 am; edited 1 time in total |
|
Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
»
Go to Registration page