# Thread: day of week when entering in date

1. ## day of week when entering in date

I am trying to figure out when entering a date in the format of mm/dd/yyyy that it will output:
mm/dd/yyyy => (day of the week it was i.e. Monday, Tuesday)

So far, I have all my factors, I am missing something or several somethings. I also need to make this repeat so that I may keep entering in a date until a 0 is entered for mm or dd or yyyy.

Code:
```#define _CRT_SECURE_NO_WARNINGS
#define numdays ((yyyy-1)*365+(yyyy-1/4)-(yyyy-1/100)+(yyyy-1/400))
#include <stdio.h>
void main()
{
int mm;
int dd;
int yyyy;
int numdays;
int day;

printf("Please enter date (m d y):");
scanf("%d %d %d", &mm, &dd, &yyyy);

printf("%d/%d/%d =>", mm, dd, yyyy);

switch(mm)
{
case 1: numdays +31; break;//Jan
case 2: numdays +31+28; break;//Feb
case 3: numdays +31+28+31; break;//Mar
case 4: numdays +31+28+31+30; break; //Apr
case 5: numdays +31+28+31+30+31; break; //May
case 6: numdays +31+28+31+30+31+30; break;//Jun
case 7: numdays +31+28+31+30+31+30+31; break;//Jul
case 8: numdays +31+28+31+30+31+30+31+30; break; //Aug
case 9: numdays +31+28+31+30+31+30+31+30+31; break;//Sep
case 10: numdays +31+28+31+30+31+30+31+30+31+30; break;//Oct
case 11: numdays +31+28+31+30+31+30+31+30+31+30+31; break; //Nov
case 12: numdays +31+28+31+30+31+30+31+30+31+30+31+31; break; //Dec
}

day=numdays+dd;

!yyyy%4 && !yyyy%100 || !yyyy%400;
day+1;

switch(day%7)
{
case 0: printf("Sunday\n"); break;
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
}
}```

2. Well, to make it repeat, you need to use a loop. There are tons of tutorials on line, including some on this very site. Also, you could check your textbook and class notes.

You're not actually assigning values, just adding stuff up and throwing away the results. The difference between:
Code:
```numdays + 31;  // this adds 31 to numdays, but doesn't store the result anywhere, just throws it away
// and
numdays += 31;  // this is shorthand for numdays = numdays + 31; which adds 31 to the current value in numdays and stores it there```
You do the same thing in your check for leap year, which also has another problem: there's no 'if' statement. Similar to the "adding and throwing away the results", you check if yyyy is a leap year, but throw away the results (you just put a ; after it). Read up on if statements in the same place you find info on loops. Something like:
Code:
```if (!yyyy%4...)  // notice, no ; here
day = day + 1;  // or perhaps better yet is day++;```
Also, your logic is a bit off. In your first switch statement, when month is 1, you do numdays+31. But think about it, if they enter January 7th, you don't want to add 31 days, it's only the 7th day of the year.

Last thing is that you assume all years start on the same day. You must find out the day of the week for January 1st for that year, and base your second switch statement off that.

3. There's a nice little formula called Zeller's congruence that calculates the day of week given the year, month, and day.

4. Well I did some changes..still not getting what i need. How do I figure out the first week in January? posting 2 changes. First:
Code:
```#define _CRT_SECURE_NO_WARNINGS
#define numdays ((yyyy-1)*365+(yyyy-1/4)-(yyyy-1/100)+(yyyy-1/400))
#include <stdio.h>
void main()
{
int mm;
int dd;
int yyyy;
int numdays;
int day;

printf("Please enter date (m d y):");
scanf("%d %d %d", &mm, &dd, &yyyy);
printf("%d/%d/%d =>", mm, dd, yyyy);

if(!(yyyy%4) && (yyyy%100) || !(yyyy%400));
day+1;

switch(mm)
{
case 1: numdays +31; break;//Jan
case 2: numdays +31+28; break;//Feb
case 3: numdays +31+28+31; break;//Mar
case 4: numdays +31+28+31+30; break; //Apr
case 5: numdays +31+28+31+30+31; break; //May
case 6: numdays +31+28+31+30+31+30; break;//Jun
case 7: numdays +31+28+31+30+31+30+31; break;//Jul
case 8: numdays +31+28+31+30+31+30+31+30; break; //Aug
case 9: numdays +31+28+31+30+31+30+31+30+31; break;//Sep
case 10: numdays +31+28+31+30+31+30+31+30+31+30; break;//Oct
case 11: numdays +31+28+31+30+31+30+31+30+31+30+31; break; //Nov
case 12: numdays +31+28+31+30+31+30+31+30+31+30+31+31; break; //Dec
}

day=numdays+mm+yyyy;

switch(day%7)
{
case 0: printf("Sunday\n"); break;
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
}
}```
The one above came the closest to me figuring out the correct output, as its matching the sample runs given to me.

Next:
Code:
```#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void main()
{
int mm;
int dd;
int yyyy;
int numdays;

printf("Please enter date (m d y):");
scanf("%d %d %d", &mm, &dd, &yyyy);
printf("%d/%d/%d =>", mm, dd, yyyy);

numdays =((yyyy-1)*365+(yyyy-1/4)-(yyyy-1/100)+(yyyy-1/400));

switch(mm)
{
case 1: numdays +=0; break;//Jan
case 2: numdays +=31; break;//Feb
case 3: numdays +=31+28; break;//Mar
case 4: numdays +=31+28+31; break; //Apr
case 5: numdays +=31+28+31+30; break; //May
case 6: numdays +=31+28+31+30+31; break;//Jun
case 7: numdays +=31+28+31+30+31+30; break;//Jul
case 8: numdays +=31+28+31+30+31+30+31; break; //Aug
case 9: numdays +=31+28+31+30+31+30+31+30; break;//Sep
case 10: numdays +=31+28+31+30+31+30+31+30+31; break;//Oct
case 11: numdays +=31+28+31+30+31+30+31+30+31+30; break; //Nov
case 12: numdays +=31+28+31+30+31+30+31+30+31+30+31; break; //Dec
}

if(!(yyyy%4) && (yyyy%100) || !(yyyy%400));
numdays = numdays+1;

switch(numdays%7)
{
case 0: printf("Sunday\n"); break;
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
}
}```
This one isnt doing what it should, not even close. Still dont know how to figure out first week of january, because it changes on a given year. Ive read the book and chapter multiple times. I cant ask for help this weekend because school is closed till tues.

5. O_o

If you reverse the order of addition in the `switch' you can implement proper "fall through" to get the desired day of the year.

That out of the way, it doesn't seem like you are using a known epoch.

You can't just say "The seventy-third day of the year is Tuesday."; it certainly will not be correct for most years.

Either use an algorithm that adjust from a known epoch implicitly (like Zeller's) or pick one and do the mathematics yourself.

A known epoch, by the by, says, for example, that the first day of the year of 2006 was a Sunday (or whatever). Knowing that you can adjust for the other years.

Soma

6. nothing in the book or in the handout says I'm suppose to know what Zeller's is yet. I'm relatively early in the class, and we havn't covered that.

7. I'm relatively early in the class, and we havn't covered that.
O_o

Okay.

None of us said that you had to implement Zeller's algorithm.

You do however need a known epoch (implicit or explicit) to adjust the day for the relevant year because different years start on different days.

That isn't programming. That is a simple statement of fact.

It would be kind of awesome if every year did start on the same day, but that isn't the calender almost everyone uses so just pick an epoch and start calculating.

Soma