Missing something in dayofweek program

• 11-15-2005
s_ny33
Missing something in dayofweek program
I had to write a program that prompts the user for three integers representing the day, month, and year of a given date (e.g. 5 24 1994), and that displays the day of the week for the given date (e.g. 5/24/1994 is a Tuesday). Below is the pseudo-code. I wrote the program but I seem to be missing something. When I execute the program, and I enter say, "5 12 1981" , the result shows "3 12 1981" subtracting two from the month. Why am I getting the adjusted month in my results? Can someone tell me what I'm missing?

a) First, adjust the month and year:
move the month 2 places down; that is; if the month entered is not January or February, subtract 2 from it to get the adjusted month, and make the adjusted year the same as the given year;
if the month is January or February, the adjusted month is month 11 or 12 of the previous year, which is the adjusted year.

b) Second, compute a month correction M, which is (26 times the adjusted month) minus 2, divided by 10 (integer division).

c) Third, compute the century C (actually century minus 1) and year Y within the century for the adjusted year (so, for example, if the adjusted year is 1993, C will be 19 and Y will be 93).

d) Fourth, compute a year correction Z which is Y + (Y divided by 4) + (C divided by 4) + (5 times C).

Again, all division are integer divisions.

e) Finally, compute a code D for the day of the week for the given date. D is the remainder after division by 7 of the sum of the day, month correction M and year correction Z. Now, if D is 0, 1, 2, …6 then, the day of the week is Sunday, Monday, Tuesday, …, Saturday respectively.

Here is my code:

Code:

``` #include <stdio.h> #include <stdlib.h> int main() {  int day,month,year;  int M,C,Y,Z,D;  char more[2];  do  {   printf ("Enter the date: (mm dd yyyy)");   scanf ("%d %d %d",&month,&day,&year);   if (month > 2)     month -= 2;   else   {     month = 10 + month;     year --;   }   M = ((26*month)-2)/10;   C = year/100;   Y = year % 100;   Z = (Y)+(Y/4)+(C/4)+(Y*C);   D = (day+M+Z)%7;     if (D=0)     {       printf("%d/%d/%d is a Sunday\n",month,day,year);     }     else if (D=1)     {         printf("%d/%d/%d is a Monday\n",month,day,year);     }     else if (D=2)     {         printf("%d/%d/%d is a Tuesday\n",month,day,year);     }     else if (D=3)     {         printf("%d/%d/%d is a Wednesday\n",month,day,year);     }     else if (D=4)     {         printf("%d/%d/%d is a Thursday\n",month,day,year);     }     else if (D=5)     {         printf("%d/%d/%d is a Friday\n",month,day,year);     }     else if (D=6)     {         printf("%d/%d/%d is a Saturday\n",month,day,year);     }   printf ("Continue? (y/n):");   scanf ("%s",&more);   }   while (more [0] != 'n');   return 0; }```
• 11-15-2005
Dave_Sinkula
Quote:

When I execute the program, and I enter say, "5 12 1981" , the result shows "3 12 1981" subtracting two from the month.
Well, that's the code you wrote.
Code:

```  if (month > 2)     month -= 2;   else   {     month = 10 + month;     year --;   }```
Perhaps you need another temporary variable, say m, to hold the (potentially modified) month.

This is assignment:
Code:

`else if (D=6)`
This is comparison:
Code:

`else if (D==6)`
Also, recheck this against your formula:
Code:

`Z = (Y)+(Y/4)+(C/4)+(Y*C);`
• 11-15-2005
itsme86
Code:

```    if (D=0)     {       printf("%d/%d/%d is a Sunday\n",month,day,year);     }     else if (D=1)     {         printf("%d/%d/%d is a Monday\n",month,day,year);     }     else if (D=2)     {         printf("%d/%d/%d is a Tuesday\n",month,day,year);     }     else if (D=3)     {         printf("%d/%d/%d is a Wednesday\n",month,day,year);     }     else if (D=4)     {         printf("%d/%d/%d is a Thursday\n",month,day,year);     }     else if (D=5)     {         printf("%d/%d/%d is a Friday\n",month,day,year);     }     else if (D=6)     {         printf("%d/%d/%d is a Saturday\n",month,day,year);     }```
Doesn't this look better?
Code:

```  char *days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",                   "Friday", "Saturday" };   printf("%d/%d/%d is a %s\n",month,day,year,days[D]);```