day of week

This is a discussion on day of week within the C Programming forums, part of the General Programming Boards category; Can anyone help me with this program. I don't even know where to start with this one. I really want ...

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

    day of week

    Can anyone help me with this program. I don't even know where to start with this one. I really want to learn this so I'm not looking for complete code, just some guidance to help this rookie through it.

    I need 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).



    An algorithm, called Zeller’s algorithm, for determining the day of the week on which a given date falls goes by the following pseudo-code.



    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.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You've been given the pseudocode, what more do you want? Which part are you having trouble with? Post an attempt.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    I don't know if you can consider this an attempt, but like I said, I don't know how to get this going.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main()
    {
     int day,month,year;
     int a,M;
     char more [2];
    
     do
    
     {
       printf ("Enter the date: (mmddyyyy)");
       scanf ("%d","%d","%d",&day,&month,&year);
    
       a = (14 - month) /12;
       M = (26 * 10)-2) /10;

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You need to look up how to use scanf, for a start.
    Code:
    a = (14 - month) / 12;
    What is that supposed to correspond to?

    In your algorithm, you have:
    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.
    I would take that to mean:
    Code:
    /* Adjust the month and year */
    if (month > 2) /* If the month is March through December */
            month-=2;
    else /* January or February */
    {
            month = 10 + month; /* month becomes 11 or 12 */
            year--; /* year needs to go back one */
    }
    The rest of the algorithm is easy to translate into code also.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    another easy way is to use the functions in time.h -- struct tm has a day-of-week member. But that might be cheating

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    It almost certainly is cheating. The OP was given an algorithm for it.

    I've tested it and wrote the corresponding code, it appears to work for some random values I tried.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Thanks for the advice AD but if I don't think this through, I never learn C. Also, I think this assignment is looking for me to understand pointers (which is totally confusing to me) and probably most people when your first learning C. Unless your a genius. Anyways, I worked on this problem some more. I'm trying to figure the century (C) and year (Y) in step (c) and I'm wondering if I'm on the right track with the code I've added. If I am, can someone explain to me how I can figure the year using this pointer?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main()
    {
     int day,month,year;
     int M,C
     int *pYear;
     char more [2];
    
     do
    
     {
       printf ("Enter the date: (mmddyyyy)");
       scanf ("%d","%d","%d",&day,&month,&year);
    
       pYear=(int*)malloc(year*sizeof(int));
    
       if (month > 2)
            month-=2;
    
       else
    
        {
          month = 10 + month;
          year--;
        )
    
       M = (26*10)-2)/10;
       C = 
        
    
       printf ("Continue? (y/n):");
       scanf ("%s",&more);
    
      }
    
       while (more [0] != 'n');
       return 0;
    
    }

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Your scanf is still very wrong. You can only have one format string, but you are passing three.
    Code:
    M = (26 * 10) - 2) / 10;
    This code makes no sense. For a start you have unmatched parentheses. Secondly, it doesn't use any other variables, so just evaluates to a constant.

    From the algorithm:
    b) Second, compute a month correction M, which is (26 times the adjusted month) minus 2, divided by 10 (integer division).
    Pay particular attention to the bit in bold.

    By the way, you don't need to use any pointers, and I can't imagine any point to using one here, what did you have in mind?
    Last edited by cwr; 11-01-2005 at 10:28 PM.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    OK...I'll take a look at the adjusted month. I thought I may have to use a pointer to figure the year.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Oh...I would replace 10 with month...right? Plus, add the other paren.

    Code:
    M = (26 * month) - 2)) / 10;

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Ah huh, except your parentheses are now even more unbalanced.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Is this it...

    Code:
    M = ((26 * month) - 2) / 10;

  13. #13
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Bingo. Does it all compile?

    What about the next step, working out the year and the century?

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Do I use an array for my scanf with a for loop?

  15. #15
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    A for loop is not necessary. Look at the documentation to scanf. How do you want them to input the values?

    Also, you have:
    Code:
    printf ("Enter the date: (mmddyyyy)");
       scanf ("%d","%d","%d",&day,&month,&year);
    Apart from the scanf line being broken, as mentioned above, you have the parameters in day/month/year order, but you are asking for the format to be mmddyyy. (month day year).

    Consider prompting the user to enter the values in "mm dd yyyy" format (space delimited), then see how you might apply that to scanf.
    Last edited by cwr; 11-01-2005 at 10:59 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 05:55 PM
  2. Force end
    By Redattack34 in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 11:16 PM
  3. The day of the week America was discovered
    By Gustaff in forum C Programming
    Replies: 3
    Last Post: 08-12-2005, 09:47 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 10:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21