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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-05-2005
Gustaff
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.

Saludos (Mexico)
• 07-05-2005
dwks
That's a good idea. Let's see what you came up with.

Quote:

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.
• 07-06-2005
willc0de4food
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.
• 07-06-2005
sand_man
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.
• 07-06-2005
Dave Evans
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
• 07-06-2005
sand_man
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 ?)
• 07-06-2005
Dave Evans
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
• 07-06-2005
dwks
Quote:

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.
• 07-06-2005
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.
• 07-06-2005
Dave Evans
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
• 07-06-2005
sand_man
No that makes sense now, thanks.
• 07-09-2005
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; }```
• 07-09-2005
kermit
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... ;)
• 07-09-2005
Br5an
Quote:

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
• 07-09-2005
dwks
You could do that, too, since you're the one posting it.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last