day of week when entering in date

This is a discussion on day of week when entering in date within the C Programming forums, part of the General Programming Boards category; I am trying to figure out when entering a date in the format of mm/dd/yyyy that it will output: mm/dd/yyyy ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    6

    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;
                        }
    }
    please help. I dont know if i posted this right, apologies if it looks like garbage.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,640
    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. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    There's a nice little formula called Zeller's congruence that calculates the day of week given the year, month, and day.
    Last edited by christop; 10-05-2012 at 08:32 PM.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    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. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    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. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    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. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,370
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-07-2012, 08:37 AM
  2. Replies: 10
    Last Post: 03-28-2012, 11:30 AM
  3. Replies: 11
    Last Post: 03-28-2012, 12:37 AM
  4. Week Selection from Date Time Picker
    By gaurav_13191 in forum Windows Programming
    Replies: 2
    Last Post: 10-31-2011, 01:57 PM
  5. 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21