Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

find the day realtive to sunday

Posted on 2002-06-26
6
Medium Priority
?
515 Views
Last Modified: 2008-01-09
// 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;
}

 
0
Comment
Question by:exiang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 3

Accepted Solution

by:
marcjb earned 300 total points
ID: 7110397
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
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 7112867
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
 

Author Comment

by:exiang
ID: 7113273
CoolBreeze:
what's the value for day in the formula?
can u pls explain further? Thanx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:exiang
ID: 7118926
0
 

Author Comment

by:exiang
ID: 7118930
0
 

Author Comment

by:exiang
ID: 7118938
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question