Solved

find the day realtive to sunday

Posted on 2002-06-26
6
506 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
  • 4
6 Comments
 
LVL 3

Accepted Solution

by:
marcjb earned 100 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:exiang
ID: 7118926
0
 

Author Comment

by:exiang
ID: 7118930
0
 

Author Comment

by:exiang
ID: 7118938
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why this code doesn't work? 8 99
Need example 5 113
Problem to scan all sheets 3 103
How to creat good software interface ? 1 66
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

808 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