Thread: Missing something in dayofweek program

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    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;
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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)
    [edit]Also, recheck this against your formula:
    Code:
    Z = (Y)+(Y/4)+(C/4)+(Y*C);
    Last edited by Dave_Sinkula; 11-15-2005 at 09:23 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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]);
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Compile Program missing .lib file have .dll
    By John Hobbes in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2004, 05:52 PM
  3. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM