Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Pyrofan1
Member
Posts: 234 Joined: Sun Apr 29, 2007 1:13 am
Post
by Pyrofan1 » Sun Dec 16, 2007 12:34 pm
i'm trying to figure out the day of the week using the information for this page
http://en.wikipedia.org/wiki/Calculatin ... f_the_week
here's my code
Code: Select all
unsigned char days_table[]={0,1,2,3,4,5,6};
unsigned char months_table[]={0,0,3,3,6,1,4,6,2,5,0,3,5};
unsigned char months_table_leap[]={0,6,2,3,6,1,4,6,2,5,0,3,5};
void get_week_day(void)
{
unsigned short num;
num+=year;
num+=year/4;
if(year%4==0)
{
num+=months_table_leap[month];
}
else
{
num+=months_table[month];
}
num+=day;
num=num%7;
week_day=(unsigned char)num;
}
this code always returns 0 instead of the right number
Last edited by
Pyrofan1 on Sun Dec 16, 2007 5:51 pm, edited 1 time in total.
Combuster
Member
Posts: 9301 Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:
Post
by Combuster » Sun Dec 16, 2007 3:45 pm
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[
My OS ] [
VDisk/SFS ]
Pyrofan1
Member
Posts: 234 Joined: Sun Apr 29, 2007 1:13 am
Post
by Pyrofan1 » Sun Dec 16, 2007 5:01 pm
week_day is a global variable
AndrewAPrice
Member
Posts: 2309 Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)
Post
by AndrewAPrice » Sun Dec 16, 2007 5:06 pm
Code: Select all
unsigned char months_table[]={0,0,3,3,6,1,4,6,2,5,0,3,5};
unsigned char months_table_leap[]={0,6,2,3,6,1,4,6,2,5,0,3,5};
int get_week_day(void)
{
unsigned short num;
num+=year;
num+=year/4;
if(year%4==0)
{
num+=months_table_leap[month];
}
else
{
num+=months_table[month];
}
num+=day;
num=num%7;
week_day=(unsigned char)num;
return week_day;
}
Please explain your reasoning behind including the code:
unsigned char days_table[]
Pyrofan1
Member
Posts: 234 Joined: Sun Apr 29, 2007 1:13 am
Post
by Pyrofan1 » Sun Dec 16, 2007 5:51 pm
fixed it
my code looked like
Code: Select all
char *days[]={
"Nothing",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
instead of
Code: Select all
char *days[]={
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};