-4

I wanna know how to find how to calculate the values of t[] array in this algorithm?

int dow(int y, int m, int d)        
{        

   static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};        

   y -= m < 3;        

   return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;        

}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Sudeep
  • 23
  • 4
  • 1
    You can pick some random dates for which you know the day. Now calculate the day `y -= m < 3;` and `(y + y/4 - y/100 + y/400 + d) % 7`. Now set t[m-1] to a value in such a way that your answer is correct. Do it for all 12 values of m. To check the correctness of this formula [read this post](http://stackoverflow.com/questions/6385190/correctness-of-sakamotos-algorithm-to-find-the-day-of-week) – Mohit Jain May 30 '14 at 07:19
  • 1
    By the way, this will not work on and earlier than September 1752. – Bathsheba May 30 '14 at 07:20
  • 1
    [Wikipedia: Determination of the day of the week](http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Months_table) explains it. – Yu Hao May 30 '14 at 07:20
  • i have read that post but i am still unable to find out the t[] array,would u plz explain by giving an example @MohitJain – Sudeep May 30 '14 at 07:29
  • OK, I would write a small code for you. – Mohit Jain May 30 '14 at 07:30

1 Answers1

1

You can use code below to generate the required array

#include <stdio.h>

int main(void) {
    int arr[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int totalDaysInThisYear = -1, m;  /* Parity would make totalDaysInThisYear = 0 */
    for(m = 1; m <= 12; ++m) {
        const int parity = (m < 3);
        printf("%d\n", (totalDaysInThisYear + parity) % 7);
        totalDaysInThisYear+= arr[m - 1];
    }
    return 0;
}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100