Do you know the day of the week you were born?

This is a discussion on Do you know the day of the week you were born? within the C Programming forums, part of the General Programming Boards category; Hi. A couple of days a go a wrote some code that comuputed the day of the week of an ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33

    Thumbs up Do you know the day of the week you were born?

    Hi.

    A couple of days a go a wrote some code that comuputed the day of the week of an specific date.

    All you need is a function wich tell you if year (a) is a leap year or not.

    you enter the date like this scanf ("%d %d %d", &d, &m, &y);

    int days=0;

    for (year=0; year<=y; year++) (

    if (leap(year))
    days+=366;
    else
    days+=365;
    )


    After this loop, you have the number of days from january 1rst year 0 century 0, to date 31 dicember y-1.

    then you just add the number of days till you get to the exact day.

    Would someone be interested in doing this task? I had.

    Saludos (Mexico)
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That's a good idea. Let's see what you came up with.

    All you need is a function wich tell you if year (a) is a leap year or not.
    There's something like that in one of the *time() functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Code:
    int leap_year;
    /* assuming you've already declared 'year' and 'days' as an int */
    leap_year = year % 4;
    if (leap_year == 0)
        days = 366;
    else
        days = 365;

    btw, when you post code you should use the [ code ][ /code ] tags, makes things all nice & such.
    Last edited by willc0de4food; 07-06-2005 at 04:45 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Im actually doing an assignment at the moment and the correct way to test for a leap year is this
    Code:
    if( (year % 4 == 0) || (year % 400 == 0) && (year % 100 == 0){
      //is leap year
    }
    The reason for this is because....I can't be bothered explaining it but basically the years that end in two 0s ( divisible by 100 ) are only leap years if they are divisible by 400.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by sand_man
    Im actually doing an assignment at the moment and the correct way to test for a leap year is this
    ..

    The reason for this is because....I can't be bothered explaining it but basically the years that end in two 0s ( divisible by 100 ) are only leap years if they are divisible by 400.
    Your words are right, but your formula says 1900 is a leap year. (and 1800 and 1700 ...)

    Maybe something like this:
    Code:
    if( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
    Regards,

    Dave
    Last edited by Dave Evans; 07-06-2005 at 08:20 AM.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    not sure i understand... I have seen that formula before and I couldn't work it out. I thought mine was right but I guess not, but how does that work? ( 100 != 0 ?)

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by sand_man
    not sure i understand...( 100 != 0 ?)
    % has precedence over && and && has precedence over ||


    Whenever you are not sure, it's safer with extra parentheses to make sure it does the right thing, but is it more readable?

    You could look at something like this:

    Code:
        if( ((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0)))
    Regards,

    Dave

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The reason for this is because....I can't be bothered explaining it but basically the years that end in two 0s ( divisible by 100 ) are only leap years if they are divisible by 400.
    That's because there are about 365.24 days in a year, and not 365.25.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33
    No, that`s not the definition of a leap year. if y is divided by 4, then it is possible that y is a leap year, but it is not the only condition. You must ask also if Y is divided by 100, if it is , then you have to ask for 400, if is then it is a leap year. See definitions of gregorian calendar in the web. cheers.
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Gustaff
    No, that`s not the definition of a leap year. if y is divided by 4, then it is possible that y is a leap year, but it is not the only condition. You must ask also if Y is divided by 100, if it is , then you have to ask for 400, if is then it is a leap year. See definitions of gregorian calendar in the web. cheers.

    In C, you can do this in a single statement:
    Quote Originally Posted by Dave Evans
    Code:
    if( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
    Code:
    if a year is divisible by 400
      it is a leap year.
    else
      if a year is divisible by 4 and is not divisible by 100 it is a leap year
    else
      it is not a leap year
    (Did you look at it or try it?)

    If you would rather implement it some other way, that's OK with me (you don't need my permission), but I say that this way is not incorrect.

    Regards,

    Dave
    Last edited by Dave Evans; 07-06-2005 at 04:12 PM.

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    No that makes sense now, thanks.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    I was kind of waiting to see if any code showed up... Well, now the end of the week so I'll post an example for those that just want to compile and find out what day of the week a given date was.

    Regards,
    Brian
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	char c;
    	int a, y, m, d;
    	int month, day, year;
    	
    	char weekDay[][10] = {"Sunday", "Monday", "Tuesday", "Wednesday",
    							"Thursday", "Friday", "Saturday"};
    							
    	char monthName[][10] = { "", "January", "February", "March", "April",
    							 "May", "June", "July", "August", "September",
    							 "October", "November", "December"};
    	
    	cout << endl << "Enter date: mm/dd/yyyy: ";
    	cin >> month >> c >> day >> c >> year;
    	
    	a = (14 - month) / 12;
    	y = year - a;
    	m = month + 12 * a - 2;
    	d = (day + y + y / 4 - y / 100 + y / 400 + 31 * m / 12) % 7;
    	
    	/*
    	cout << endl << setw(2) << setfill('0') << month << "/";
    	cout << setw(2) << setfill('0') << day << "/";
    	cout << setw(4) << setfill('0') << year << " was a ";
    	cout << weekDay[d] << endl << endl;
    	*/
    	
    	cout << endl << monthName[month] << " ";
    	cout << setw(2) << setfill('0') << day << ", ";
    	cout << setw(4) << setfill('0') << year << " was a ";
    	cout << weekDay[d] << endl << endl;
    	
    	cout << "Press `Enter' to continue . . . ";
    	cin.sync( );
    	cin.ignore( );
    	return EXIT_SUCCESS;
    }

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Br5an
    I was kind of waiting to see if any code showed up... Well, now the end of the week so I'll post an example for those that just want to compile and find out what day of the week a given date was.

    Regards,
    Brian
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	char c;
    	int a, y, m, d;
    	int month, day, year;
    	
    	char weekDay[][10] = {"Sunday", "Monday", "Tuesday", "Wednesday",
    							"Thursday", "Friday", "Saturday"};
    							
    	char monthName[][10] = { "", "January", "February", "March", "April",
    							 "May", "June", "July", "August", "September",
    							 "October", "November", "December"};
    	
    	cout << endl << "Enter date: mm/dd/yyyy: ";
    	cin >> month >> c >> day >> c >> year;
    	
    	a = (14 - month) / 12;
    	y = year - a;
    	m = month + 12 * a - 2;
    	d = (day + y + y / 4 - y / 100 + y / 400 + 31 * m / 12) % 7;
    	
    	/*
    	cout << endl << setw(2) << setfill('0') << month << "/";
    	cout << setw(2) << setfill('0') << day << "/";
    	cout << setw(4) << setfill('0') << year << " was a ";
    	cout << weekDay[d] << endl << endl;
    	*/
    	
    	cout << endl << monthName[month] << " ";
    	cout << setw(2) << setfill('0') << day << ", ";
    	cout << setw(4) << setfill('0') << year << " was a ";
    	cout << weekDay[d] << endl << endl;
    	
    	cout << "Press `Enter' to continue . . . ";
    	cin.sync( );
    	cin.ignore( );
    	return EXIT_SUCCESS;
    }
    psst...there's a C++ forum just down the hall...

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    psst...there's a C++ forum just down the hall...
    Unfortunately this thread wasn't down the hall... Replace cin / cout with C syntax and get over it. BK

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You could do that, too, since you're the one posting it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
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, 10:56 AM
  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