Thread: Day of the Week Programme

  1. #1
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7

    Day of the Week Programme

    Hi everybody,

    Here I come again. I feel so stuck... I'm faced with a coding exercise, the solution to which, I'm unable to find even after trying to break my head onto the wall....

    The problem is that this is more of a beginner's exercise and as such I'm allowed to only use if statements!

    Following is the thing:-

    Q. According to the Gregorian calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard, write a program to find out what is the day on 1st of January of the year input.

    And, below is some scribbling that I tried. But, my head seems to go blank after that...

    Code:
    /* Determination of Day on 01-01 */
    
    #include<stdio.h>
    
    int main()
    {
      int year, Day;
      
      printf("\nEnter a Year: ");
      scanf("%d",&year);
      
      Day = /* Doomsday Algorithm, maybe... or Zeller's Congruence... aaah... forget it...*/
    
    }
    Thanks in Advance for your help.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    1. Find the Algorithm
    2. Write code that does the Algorithm.

    Which part are you having a problem doing?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Anish Kuriakose View Post
    The problem is that this is more of a beginner's exercise and as such I'm allowed to only use if statements!
    You already broke the rule because you've called two library functions printf and scanf.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    January 1, 2001 = Monday.
    So in 2002 it would be?
    add 365 days - which is like adding exactly 52 weeks + 1 day. 52 x 7 + 1 = 365.
    So the weekday slips ahead one day from Monday to Tuesday.
    Then for 2003 the weekday would slip ahead again to Wednesday.
    Then for 2004 the weekday would slip ahead again to Thursday.
    For 2005 ... but 2004 is a leap year, so slip ahead 2 days to Saturday.
    So now you need to figure out what's a leap year.
    Now you also have to consider what happens if the input year is before 2001.

    This is how you solve things. Do several examples by hand.

  5. #5
    Registered User Anish Kuriakose's Avatar
    Join Date
    Jan 2014
    Location
    Madel, Tivim, Bardez, North Goa, Goa, India
    Posts
    7
    Yeah. That's my problem...! I'm unable to be able to figure out the algorithm.... I'll try this by doing some examples by hand.I guess I'm just too impatient...

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by Anish Kuriakose View Post
    I guess I'm just too impatient...
    i'm so sorry

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,762
    Challenge was accepted.

    Armed with the following information:
    * Definition of a leap year;
    * Start of the Gregorian calendar (year)
    * How many days are in a normal year

    I set about writing a function to determine the day of January 1st for any year in the Gregorian calendar. I used no references apart from what I listed above and it took me 45 minutes to derive a formula, implement it and debug it (and this was probably slow, I admit!)

    Looking at my derived formula, and if I rearrange the formula and simplify, I have derived Gauss' method. I'm not great at maths, I applied mostly logic (on paper! the maths is pretty simple) and arrived at something that works and is the same as an accepted and published method. Surely if I can do this you can at least Google a formula and simply convert it to C notation.

    Edit: What stumped me the most was how to deal with the years 1600 and 1700 and pondering on how to deal with those dates and keep the rest of the leap years rules "aligned" took me the most time. At first I was shifting the input year back in time and then something clicked (shifting back in time was a mistake, but, well, this is how problem solving works... you try things.) This can (and probably should) be done using paper and pencil.
    Last edited by Hodor; 02-12-2014 at 01:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. day of week
    By s_ny33 in forum C Programming
    Replies: 18
    Last Post: 11-02-2005, 11:56 AM
  2. A week away from RoD?? PARTY!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 02-28-2003, 06:58 AM
  3. e-week, networking, and a 96 hour week...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-23-2003, 05:23 PM
  4. worst week
    By Breach23 in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 11-03-2001, 12:43 AM

Tags for this Thread