user3436065
user3436065

Reputation: 15

C beginner help : day of week for any given date

The prompt is: Implement a function that reads in a string containing a textual description of a cal- endar 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.

So as of right now i am able to get the correct days for every single day except in the years 2005 2009 2013 2017 etc etc... they are always a day behind, i notice that its going by a trend of every 4 years the days end up 1 day behind. Im not sure whats wrong. is it cause my method of using 365.25 as each year is wrong?

My code:

#include<stdio.h>

int main()
{
int month,day1,day2,totdays,year,dm,dn,leap,rmd;


    printf(" ");
    scanf("%d/%d/%d",&month,&day1,&year);


    if(((year%4==0) && (year%100!=0)) || (year%400==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=304;

         if(month==12)
            dm=334;
       }


      day2=(year-1970)*(365.25);
      dn=dm+day1;
      totdays=day2+dn;

      rmd=totdays%7;

      if(rmd==5)
        {
           printf("Monday \n");
        }

      if(rmd==6)
        {
           printf("Tuesday \n");
        }

      if(rmd==0)
        {
            printf("Wednesday \n");
        }

      if(rmd==1)
        {
            printf("Thursday \n");
        }

      if(rmd==2)
        {
            printf("Friday \n");
        }

      if(rmd==3)
        {
            printf("Saturday \n");
        }

      if(rmd==4)
        {
            printf("Sunday \n");
        }

      return 0;

}

Upvotes: 0

Views: 244

Answers (2)

hcs
hcs

Reputation: 1534

1969 wasn't a leap year, 1972 was. When you do

day2=(year-1970)*(365.25);

to discover how many days off January 1st of year year is, you'll count

  • 0 days for '70
  • 365.25 days for '71
  • 730.5 days for '72
  • 1095.75 days for '73
  • 1461 days for '74

The fractional portion of the floating point calculation is truncated, so day2 isn't going to count the extra day from 02/29/1972 until 01/01/1974, instead of 01/01/1973 as it should.

Put another way, you are making the assumption that 1970 was the first year after a leap year, so a leap day won't be counted until four years later.

Upvotes: 1

user3386109
user3386109

Reputation: 34829

The day2 calculation won't work. There are 1461 days in every four year period. First you need to compute how many 4 year periods have passed. Then figure out how many days there were to the beginning of the specified year, similar to what you did for the months.

The year%100 and year%400 exceptions add a little complexity, but fortunately the year 2000 was a leap year, so the first time you have to deal with that little wrinkle is the year 2100.

Upvotes: 0

Related Questions