• C

find the day realtive to sunday

// Hi, my name is Allen
// This is a month calender program
// i m facing problem where i don't know the way to find "days that relative to sunday"
// pls help me solve the problem
 
#include <stdio.h>
#include <math.h>
 
int testyear(int yearinput);
int find_relative_to_sunday(int year, int leap, int month);
void printmonth(int startday, int days);
 

int main(void)
 
{
int year;
int month;
int i=0;
int days_of_month;
int days_of_relative;
int leap;
 
 do
 {
  year:
  printf("\nPls enter the year: ");
  scanf("%d",&year);
 
  if (year>0)
   {
    month:
    printf("Pls enter the month(1-12): ");
    scanf("%d",&month);
   }
  else
   {
    printf("Invalid year value entered! Please try again.\n");
    goto year;
   }
 
  leap = testyear(year);
 
  if(leap==1)
       printf("\nThis is a leap year.\n");
 
  if (month>=1 && month <=12)
   {
 
   switch(month)
    {
    case 1:  printf("31 days\n");days_of_month=31;break;
    case 2:  {
       if(leap==1)
        {
         printf("29 days\n");
         days_of_month=29;
        }
       else
        {
         printf("28 days\n");
         days_of_month=28;
        }
       }
       break;
    case 3:  printf("31 days\n");days_of_month=31;break;
    case 4:  printf("30 days\n");days_of_month=30;break;
    case 5:  printf("31 days\n");days_of_month=31;break;
    case 6:  printf("30 days\n");days_of_month=30;break;
    case 7:  printf("31 days\n");days_of_month=31;break;
    case 8:  printf("31 days\n");days_of_month=31;break;
    case 9:  printf("30 days\n");days_of_month=30;break;
    case 10: printf("31 days\n");days_of_month=31;break;
    case 11: printf("30 days\n");days_of_month=30;break;
    case 12: printf("31 days\n");days_of_month=31;break;
    default: printf("Program error");
    }
   }
  else
   {
    printf("Invalid month value entered! Please try again.\n");
    goto month;
   }
 
   printf("\n\n");
 
   days_of_relative = find_relative_to_sunday(year,leap,month);
   printmonth(days_of_relative,days_of_month);
 
 }while(i==0)  ;
 

return 0;
}
 
 
 

// test whether a year is leap year or not
// for leap year, return 1; else, return 0
int testyear(int yearinput)
{
 
 if(yearinput%4 == 0 && yearinput%100 != 0 || yearinput%400 == 0)
  return 1;
 
 else
  return 0;
}
 
 
 
 
// find value of days that relative to sunday of a particular month
int find_relative_to_sunday(int sub_year, int sub_leap, int sub_month)
{
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
 
 
 

// print calender function
void printmonth(int startday, int days)
 
{
int daycount;
int weekday;
 
printf("Sun Mon Tue Wed Thu Fri Sat\n");
printf("--- --- --- --- --- --- ---\n");
 
for (weekday =0; weekday < startday; weekday++)
printf("    ");
 
for (daycount =1; daycount <= days; daycount++)
 {
 if (weekday >6)
  {
   printf("\n");
   weekday=1;
  }
 else
  {
   weekday++;
  }
 printf("%3d ",daycount);
 }
 
 printf("\n--- --- --- --- --- --- ---\n") ;
 return;
}

 
exiangAsked:
Who is Participating?
 
marcjbConnect With a Mentor Commented:
One thing you could do is use the mktime function to figure out what the first of the month is, and go from there.

For example, this little program will print out the days since Sunday that the 1st of June was:

#include <time.h>
#include <stdio.h>

int main()
{
    struct tm t;
 
    /* What day is June 1st, 2002? */
    memset(&t, 0, sizeof(struct tm));
    t.tm_year = 2002 - 1900;
    t.tm_mon = 6 - 1;
    t.tm_mday = 1;
    if ( mktime(&t) == -1 )
        return -1;
    printf("Days since Sunday: %i\n", t.tm_wday);
    return 0;
}

The output is '6', meaning 6 days since Sunday, which is Saturday.  The structure tm is part of C.  The mktime attempts to fill in the struct and return the correct time, as long as enough information is given.  The tm_year member is the number of years since 1900.  The tm_mon member is the number of months since January (this is why 1 is subtracted).  The tm_mday is the day of the month.  The tm_wday member if the day of the week.

Hope this helps,

Marc

By the way, you can clean up your switch statement a little by allowing certain entries to 'fall through'.  This means that you leave out the 'break'.  For example:

switch(month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10;
    case 12:
        printf("31 days\n");
        days_of_month=31;
        break;
               
    case 2:
        if(leap==1) {
            printf("29 days\n");
            days_of_month=29;
        } else {
            printf("28 days\n");
            days_of_month=28;
        }
        break;
       
    case 4:
    case 6:
    case 9:
    case 11:
        printf("30 days\n");
        days_of_month=30;
        break;

    default: printf("Program error");
        break;
}

One other thing - try to get rid to the goto statements in your program.  They often make programs more difficult to read and maintain.  One thing you could do is set days_of_month to -1 just before the switch statement, and put the switch statement in a while statement that continues while days_of_month is -1.  

days_of_month = -1;
while ( days_of_month == -1 ) {
    switch(month) {
      /* the value gets set for valid data */
      /* but is kept at -1 for invalid data */

    } /* end of switch*/
} /* end of while - only break out is days_of_month != -1 *
0
 
CoolBreezeCommented:
there is a formula

day_relative_to_sunday
= ( day + 1 + (month*2) + ((int)(month+1)*3/5)
    + year + ((int)(year/4)) - ((int)(year/1000))
    + (year / 400) ) modulo 7
0
 
exiangAuthor Commented:
CoolBreeze:
what's the value for day in the formula?
can u pls explain further? Thanx
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
exiangAuthor Commented:
0
 
exiangAuthor Commented:
0
 
exiangAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.