tags:

views:

100

answers:

3

Hello Experts,

I have to write a program in C which, given the name of a day, returns all of the dates in this month that the day will be on.

For example, if the input is "Sunday" the output should be: 5,12,19,26 (which are the days Sunday will be on this month.)

Does any one have any idea how to do this? I have tried a lot. Please help me sort this out.

Thanks in advance.

+2  A: 

You can use the time() function to get the current time.

Then use the localtime() function to get a struct (struct tm) with the information (year, month, day, ...) of the current time.

From the "struct tm" get the tm_mday and the tm_wday. Use these fields to determine the next or previous sunday. e.g. if tm_mday is 12, and tm_wday is 3 (wednesday), then we now that the 9th of this month is a sunday (12-3 = 9). From that number simply add or subtract 7 to get all other sundays.

Patrick
#include<time.h> #include<conio.h> #include<stdio.h> void main() { struct tm *Sys_T=NULL; time_t currenttime; currenttime = time(NULL); Sys_T=localtime( Day=Sys_T->tm_mday; printf("%d",currenttime); getch(); }I were trying this to get system date.But getting error on Day.How to read Day?
PrateekSaluja
What error do you get? I see that Day has not been defined.
Patrick
+1  A: 

You need to know it for a given year too? Or is this for only this year? If you need to know it for any given year, you can do a "days per month" enum, having one for the leap years and one for the non-leap years.

You just need to know in which day of week started a year (i.e: "Monday", "Tuesday", etc)

You will, at least, have 5 dates for any given month, so, you can have a fixed length array of ints.

You know that the gregorian calendar repeats itself each 400 years, and that if a year X started with day "Y", then, year X + 1 will start with day ("Y" + 1) % 7 if x is not a leap year, if it is a leap year, it will start with day ("Y" + 2). that could give you the first date of any year, and knowing how many days have all the months for any given year, you can easily get what date that month starts in ("Monday", etc).

Then, all you have to do, is something like

int offset = 0;
int i;
while (myDate + offset != monthStartingDate) {
    offset++;
}
i = offset + monthStartingDate;

(myDate is the number of day of week, and monthStartingDate is the number of day of week for the first day of that month)

when you go out of that loop, you will have the first ocurrence, then, you just add 7 until i is out of month bounds.

you can add each i into the array..

int res[5] = {0,0,0,0,0}

for ( ; i < daysOfMonth(month, year); i += 7) {
   int res[i / 7] = i;
}

then you just return res.

Oh, I dind't know that you were able to use date functions :P I think the idea of the excercise was practicing C :P

Marco
Buh, when I wrote the answer the question was another. It was something like "given a month and a day of week.." Blah.
Marco
+1  A: 

1) Take a string (weekday name) from the input (use scanf or gets)

2) Convert it to a number (find it's index in a table of weekdays using loop and strcmp), assign 0 to Sunday, 1 for Monday ...

3) Get current time with time function and convert it to tm struct with localtime function

4) From tm struct calculate the first day in current month of a given weekday

first_mday_of_given_wday = (tm_struct.tm_mday + given_wday - tm_struct.tm_wday + 6) % 7 + 1

5) Find out how many days is in current month. To do this:

  • put 1 into tm_mday and 0 into tm_isdst of your tm struct
  • duplicate the struct
  • increase by 1 tm_mon in the duplicate (watch for the last month! in that case increase tm_year by 1 and set tm_mon to 1)
  • convert booth strutcs to time_t with mktime function and calculate difference (just subtract these time_t values), convert result from seconds to days (divide by 60*60*24)

6) Run a loop though calculated range:

for (i = first_mday_of_given_wday; i <= num_days_in_month; i += 7) printf("%d, ", i)

5th step can be omitted in a certain situations. We know that a month can have from 28 to 31 days. If any of hypothetical days 29,30,31 of current month cannot be a given weekday we can assume that current month has 28 days.
So simply - assume we have 28 days in current month if first_mday_of_given_wday is more then 3, otherwise calculate the number like shown in 5th step.

adf88