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 <iostream>
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
|
Last edited Feb 26, 2011, 9:37 am by Banner
|
|
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
|
|
Vigud
Conjurer

Group: Members
Posts: 199
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 <iostream>
#include <cstdlib>
#include <cstdio>
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.
|
|
|
Vigud
Conjurer

Group: Members
Posts: 199
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
|
|
|
|
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: 199
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.
|
|
|
|
|
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.
|
|
|
Davion
Idle Hand


Group: Administrators
Posts: 1,669
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 :).
|
......................... 
|
|
|
|
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;
|
|
|
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.
|
|
|
|