0

The prompt is: Implement a function that reads in a string containing a textual description of a calendar date and that prints out the corresponding day of the week (Monday–Sunday). The two valid input formats for this function are:

mm/dd/yyyy

Example: 03/04/2014 Output: Tuesday

Month dd, yyyy

Example: March 04, 2014 Output: Tuesday

where dd is the numeric day, mm is the numeric month, yyyy is the year and Month is the name of the month. All days and months are specified using two digits (i.e. for March, use 03 instead of 3). In the second valid format, there is a single space between Month and dd and between dd, and yyyy. In order to receive full credit on this task, your program should print out the correct day of the week for any input in a correct format.

The code I have so far is able to give me the number of the day of the year that I input but from there I don't know what to do so that it will give me the day of the week because each year starts from a different weekday to begin with.

#include<stdio.h>
int main() {
    int month, day, year, dm, dn, leap;
    printf("enter the month:");
    scanf("%d",&month);
    printf("enter the day:");
    scanf("%d",&day);
    printf("enter the year:");
    scanf("%d",&year);

    if((year%100 == 0 && year%400 == 0) || (year%4==0)) {
        if(month==1)
            dm=0;
        if(month==2)
            dm=31;
        if(month==3)
            dm=60;
        if(month==4)
            dm=91;
        if(month==5)
            dm=121;
        if(month==6)
            dm=152;
        if(month==7)
            dm=182;
        if(month==8)
            dm=213;
        if(month==9)
            dm=244;
        if(month==10)
            dm=274;
        if(month==11)
            dm=305;
        if(month==12)
            dm=335;
    }
    else { 
        if(month==1)
            dm=0;
        if(month==2)
            dm=31;
        if(month==3)
            dm=59;
        if(month==4)
            dm=90;
        if(month==5)
            dm=120;
        if(month==6)
            dm=151;
        if(month==7)
            dm=181;
        if(month==8)
            dm=212;
        if(month==9)
            dm=243;
        if(month==10)
            dm=273;
        if(month==11)
            dm=303;
        if(month==12)
            dm=334;
    }

    dn = dm+day;
    printf("the day number is :%d",dn);

    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user3436065
  • 15
  • 1
  • 3
  • 7
  • I imagine the assumption is that you'll give it some starting epoch (say, January 1st, 1970...) which you know the day of the week of, then figure out the number of days between that day and the input, and from there you can easily figure out what day of the week it is (by simply doing `days % 7`). – aruisdante Mar 21 '14 at 00:13
  • I was thinking about that, this whole project is to work with the epoch time_t unixtime=time(NULL) which returns the seconds since january 1st, 1970; however, i dont know how to figure out how many days have passed between the users input date and the epoch date of january 1st, 1970 – user3436065 Mar 21 '14 at 00:32
  • If you are allowed to use standard time constructs, there is built-in math to do it for you. You do NOT really want to write your own time code. But the simple naive brute-force method that will work for most dates in the recent couple of centuries would be to simply subtract the difference in years and multiply by 365.242, then figure out the difference from months using your months->days code you figured out already, then the difference in days, and that should be good enough. – aruisdante Mar 21 '14 at 00:40
  • Im almost positive i ahve to do the brute force method as i have not been taught the normal time commands. Im not understand completely what subtracting the difference in years then multiply it by 365.242, then difference in days, and etc... would you be able to give me a more detailed explanation? For example if i put in the date 01/01/2014, it would be 2014-1970 which gives me 44 years, but isnt tehre still 2 months and 20 days left over in this year still? – user3436065 Mar 21 '14 at 00:51
  • correct. So you know the number of days for january of a non-leap-year (`0`), and the number of days for february (`31`), so the difference is `31`. And then you have `20` more days from the simple day component. Savy? Find the difference in each portion of the date (years-as-days, months-as-days, days), add them together, boom. Just remember to account for leap-years in the months-as-days portion (they're accounted for in the years portion by the decimal-days-as-years). – aruisdante Mar 21 '14 at 01:02
  • so i added into my code above day2=(year-1970)*(365.25) then i added another line, totdays=day2+dn so then i did rmd=totdays%/7 then i did 7 if statements with rmd==1 set to print out thursday since january 1970 was a thursday, however, some of the days i input give like a day behind i dont know whats wrong. – user3436065 Mar 21 '14 at 01:51
  • possible duplicate of [C beginner help : day of week for any given date](http://stackoverflow.com/questions/22549587/c-beginner-help-day-of-week-for-any-given-date) – AndersK Mar 21 '14 at 06:42

0 Answers0