26 Feb, 2011, Banner wrote in the 1st comment:
Votes: 0
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?

/* 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
26 Feb, 2011, Kayle wrote in the 2nd comment:
Votes: 0
You have j and k commented out, they're not actually being assigned any values that I can tell.
26 Feb, 2011, Vigud wrote in the 3rd comment:
Votes: 0
/* 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;
}
26 Feb, 2011, Banner wrote in the 4th comment:
Votes: 0
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.
26 Feb, 2011, Vigud wrote in the 5th comment:
Votes: 0
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.
26 Feb, 2011, Banner wrote in the 6th comment:
Votes: 0
How can I make it print 0 for Sunday? That's what I need it to do for the assignment.
26 Feb, 2011, chrisd wrote in the 7th comment:
Votes: 0
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.
26 Feb, 2011, Vigud wrote in the 8th comment:
Votes: 0
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.
26 Feb, 2011, Cratylus wrote in the 9th comment:
Votes: 0
I'm a little fuzzy on the Zeller algorithm
26 Feb, 2011, Banner wrote in the 10th comment:
Votes: 0
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.
27 Feb, 2011, Davion wrote in the 11th comment:
Votes: 0
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 :).
27 Feb, 2011, David Haley wrote in the 12th comment:
Votes: 0
You mean, when one can get an answer pasted to you for your homework assignment from a web forum…? :thinking:
27 Feb, 2011, Banner wrote in the 13th comment:
Votes: 0
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…? :thinking:

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.

return( d+ (int)floor(26*(m+1)/10)+y+(int)floor(y/4)+5)%7;
27 Feb, 2011, sankoachaea wrote in the 14th comment:
Votes: 0
Cratylus said:
I'm a little fuzzy on the Zeller algorithm

Thanks for sharing Cratylus! :]
03 Mar, 2011, quixadhal wrote in the 15th comment:
Votes: 0
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. :)

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

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");
}
03 Mar, 2011, Runter wrote in the 16th comment:
Votes: 0
quixadhal said:
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. :)


That's the Quixadhal method for getting an A. :p
03 Mar, 2011, Tyche wrote in the 17th comment:
Votes: 0
quixadhal said:
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. :)


He could just outsource it to China or India, and get credit towards an MBA. ;-)
03 Mar, 2011, Kayle wrote in the 18th comment:
Votes: 0
I'm up for both options, use the system call, and then tell the teacher that you outsourced it to India and their response was that reinventing the wheel was a waste of time when there's a system call to do just that. That way you get an A AND credit towards your MBA.
0.0/18