» MUDBytes Community » Coding Discussions » Coding and Design » Zeller Algorithm
 Pages: << prev 1, 2 next >>
Zeller Algorithm
 Banner Sorcerer Group: Members Posts: 391 Joined: Jul 14, 2006 #1 id:54059 Posted Feb 26, 2011, 9:35 am I need to apply the Zeller Algorithm to acquire the day of the week as a numeral in a programming class. However, the algorithm is returning the incorrect day of the week. I need it to return 0 for Sunday, 1 for Monday, etc, and I used the algorithm here but it's not working as expected. Any ideas? Code (text):```/* Program to Calculate the Day of the week given a date ** COMP 1102 ** Date of last modification: Feb 26, 2011 ** Programmer: **/ // Pull in includes #include using namespace std; // local function definitions int convDay( int day, int month, int year ); int main() {     int d = 0, m = 0, y = 0, day = 0;         system("cls");  // clears the output screen         cout << endl;  // send a "end of line" or "new line"                     // symbol to the output stream (screen)     cout << "Provided with your date of birth, we are going to determine\n\r"             "The day you were born in history. Please provide all input in numeric form.\r\n\r\n";                    cout << "What month were you born?  ";     cin  >> m;         cout << "What day were you born?  ";     cin >> d;         cout << "What year were you born?  ";     cin >> y;     // Find and return the day        day = convDay( d, m, y ); //    cout << birthDay << birthMonth << birthYear;         // Display the result     printf( "%d/%d/%d is equivalent to Day %d of the week.\r\n", m, d, y, day);             system( "pause" ); // wait for user to press a key...         return 0; // normal exit } // end main() // function to Calculate the Day of the week given a date // IN:  a day, month, and year // OUT: a day of the week as a numerical value // E.g. 0 = Sunday, 1 = Monday, 6 = Saturday // USES: Calculating the day of the week you were born, of course!         int convDay( int day, int month, int year ) {     int h = 0, j = 0, k = 0;     /* j is the number of the century [i.e. the year / 100], k the year within the century [i.e. the year % 100], m the month, q the day of the month, h the day of the week [where 1 is Sunday];             j = year/100;     k = year % 100; */        if( month >= 3 )       month -= 2;     else       month += 10;      //    h = (day + 26 * ((month+1) / 10) + k + (k/4) + (j/4) + (5*j)) % 7;     h = (day + 26 * ((month+1)/10) + year + year/4 + 5) % 7;     return h; } // end convDay``` .........................Lead Developer, Star Wars: Galactic Insights -- sudo apt-get sandwich Last edited Feb 26, 2011, 9:37 am by Banner WWW AIM Yahoo MSN GT
 Kayle Wizard Group: Members Posts: 1,258 Joined: Nov 27, 2006 #2 id:54060 Posted Feb 26, 2011, 10:07 am You have j and k commented out, they're not actually being assigned any values that I can tell. .........................Owner/Coder -- Malevolent Whispers -- Development Phase - Not accepting players Coder -- Star Wars: The Sith Wars -- Open Alpha - Players Welcome - Full System Re-writes Imminent. FUSS Project Team Lead -- SmaugMuds.Org - The Smaug MUDs Community Center I3 Contact: Kayle@SithWars                            WWW
 Vigud Conjurer Group: Members Posts: 208 Joined: Jan 8, 2011 #3 id:54061 Posted Feb 26, 2011, 10:43 am Code (C++):```/* Program to Calculate the Day of the week given a date ** COMP 1102 ** Date of last modification: Feb 26, 2011 ** Programmer: **/ // Pull in includes #include #include #include using namespace std; // local function declarations int convDay( int d, int m, int y ); const char *day_names[ ] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; int main() {     int d = 0, m = 0, y = 0, day = 0;     system("cls");  // clears the output screen     cout << endl;  // send a "end of line" or "new line"                     // symbol to the output stream (screen)     cout << "Provided with your date of birth, we are going to determine\n\r"             "The day you were born in history. Please provide all input in numeric form.\r\n\r\n";                    cout << "What month were you born?  ";     cin  >> m;     cout << "What day were you born?  ";     cin >> d;     cout << "What year were you born?  ";     cin >> y;     // Find and return the day        day = convDay( d, m, y ); //    cout << birthDay << birthMonth << birthYear;     // Display the result     printf( "%d/%d/%d was %s.\r\n", m, d, y, day_names[ day ]);     system( "pause" ); // wait for user to press a key...     return 0; // normal exit } // end main() // function to Calculate the Day of the week given a date // IN:  a day, month, and year // OUT: a day of the week as a numerical value // E.g. 0 = Sunday, 1 = Monday, 6 = Saturday // USES: Calculating the day of the week you were born, of course! int convDay( int d, int m, int y ) { int J, K; if (m < 3) { m += 12; y--; } J = y / 100; K = y % 100; return (d + 26 * (m + 1) / 10 + K + K/4 + J/4 + 5*J) % 7; }```
 Banner Sorcerer Group: Members Posts: 391 Joined: Jul 14, 2006 #4 id:54068 Posted Feb 26, 2011, 2:23 pm Kayle said:You have j and k commented out, they're not actually being assigned any values that I can tell. I know. I was trying different approaches. j and k aren't being used in the algorithm I left uncommented.. Vigud, that isn't working either. I'm well aware of how to print the day names based on a numerical value. The assignment is to return a numerical value, not the day name, as I previously stated. Secondly, when entering 2/26/2011, it's returning 0 when it should be returning 6. .........................Lead Developer, Star Wars: Galactic Insights -- sudo apt-get sandwich WWW AIM Yahoo MSN GT
 Vigud Conjurer Group: Members Posts: 208 Joined: Jan 8, 2011 #5 id:54069 Posted Feb 26, 2011, 2:32 pm According to http://c-faq.com/misc/zeller.html it should be returning 0 for Saturdays, because h the day of the week [where 1 is Sunday]; and (anything % 7) returns value between 0 and 6. If 1 is Sunday, then... What isn't working? I compiled it and tested, everything was ok. Last edited Feb 26, 2011, 2:33 pm by Vigud
 Banner Sorcerer Group: Members Posts: 391 Joined: Jul 14, 2006 #6 id:54071 Posted Feb 26, 2011, 2:40 pm How can I make it print 0 for Sunday? That's what I need it to do for the assignment. .........................Lead Developer, Star Wars: Galactic Insights -- sudo apt-get sandwich WWW AIM Yahoo MSN GT
 chrisd Conjurer Group: Members Posts: 105 Joined: Jun 3, 2010 #7 id:54072 Posted Feb 26, 2011, 2:47 pm Banner said:How can I make it print 0 for Sunday? That's what I need it to do for the assignment. Really? If the result is 0, set it to 6. Otherwise, subtract 1.
 Vigud Conjurer Group: Members Posts: 208 Joined: Jan 8, 2011 #8 id:54073 Posted Feb 26, 2011, 2:52 pm return ((d + 26 * (m + 1) / 10 + K + K/4 + J/4 + 5*J) - 1) % 7; seems to be working, I tested it for last 7 days. But I'm tired so I don't guarantee anything.
 Cratylus Wizard Group: Members Posts: 1,948 Joined: May 22, 2006 #9 id:54074 Posted Feb 26, 2011, 2:56 pm I'm a little fuzzy on the Zeller algorithm ......................... WWW
 Banner Sorcerer Group: Members Posts: 391 Joined: Jul 14, 2006 #10 id:54075 Posted Feb 26, 2011, 3:38 pm Vigud said:return ((d + 26 * (m + 1) / 10 + K + K/4 + J/4 + 5*J) - 1) % 7; seems to be working, I tested it for last 7 days. But I'm tired so I don't guarantee anything.This seems like the best option and it is returning the expected results, although I'm unsure of why I've been provided with an algorithm that you need to cannibalize to get the results the assignment expects.. I suppose I'll find out when I submit it, though. Thanks for the help Vigud. .........................Lead Developer, Star Wars: Galactic Insights -- sudo apt-get sandwich WWW AIM Yahoo MSN GT
 Davion Idle Hand Group: Administrators Posts: 1,803 Joined: May 14, 2006 #11 id:54076 Posted Feb 26, 2011, 4:08 pm Banner said: although I'm unsure of why I've been provided with an algorithm that you need to cannibalize to get the results the assignment expects.. I suppose I'll find out when I submit it, though. Thanks for the help Vigud. Most likely, so you don't just c/p the algorithm from some website :). One can only truly understand an algorithm when they can manipulate it to their will :). ......................... WWW AIM
 David Haley Wizard Group: Members Posts: 7,841 Joined: Jun 30, 2007 #12 id:54077 Posted Feb 26, 2011, 4:13 pm You mean, when one can get an answer pasted to you for your homework assignment from a web forum...? .........................-- d.c.h -- BabbleMUD Project (custom codebase) Legends of the Darkstone (head coder) http://david.the-haleys.org ......................... WWW
 Banner Sorcerer Group: Members Posts: 391 Joined: Jul 14, 2006 #13 id:54078 Posted Feb 26, 2011, 5:22 pm Davion said: Banner said: although I'm unsure of why I've been provided with an algorithm that you need to cannibalize to get the results the assignment expects.. I suppose I'll find out when I submit it, though. Thanks for the help Vigud. Most likely, so you don't just c/p the algorithm from some website :). One can only truly understand an algorithm when they can manipulate it to their will :). David Haley said:You mean, when one can get an answer pasted to you for your homework assignment from a web forum...? He gave the algorithm and explained how to use it, I just didn't understand it. At any rate, Runter figured out how to do it as it was intended it to work in the first place, so it's all good. Code (text):`return( d+ (int)floor(26*(m+1)/10)+y+(int)floor(y/4)+5)%7;` .........................Lead Developer, Star Wars: Galactic Insights -- sudo apt-get sandwich WWW AIM Yahoo MSN GT
 sankoachaea Conjurer Group: Members Posts: 137 Joined: Dec 1, 2010 #14 id:54079 Posted Feb 27, 2011, 3:22 pm Cratylus said:I'm a little fuzzy on the Zeller algorithm Thanks for sharing Cratylus! :] .........................I'm looking for a project to join. C/C++/Lua? Sounds good. AIM GT
 quixadhal Wizard Group: Members Posts: 2,685 Joined: Oct 17, 2007 #15 id:54130 Posted Mar 3, 2011, 12:02 am You can always just use the system routine and tell your teacher it doesn't make sense to reinvent a wheel that's already been optimized and tested for 40 years. :) Code (text):```#include #include #include char s[256]; time_t t; struct tm *tmp; t = time(NULL); tmp = localtime(&t); if(tmp) {     strftime(s, 255, "%w", tmp);     printf("Day of week: %s\n", s); } else {     printf("AIEEEEE!  No time for time!\n"); }``` .........................
 Pages:<< prev 1, 2 next >>