Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org C Program based on Gregorian Calendar
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 07-10-2006, 11:08 AM #1 duffmckagan Senior Member   Registered: Feb 2005 Distribution: Cent OS 6.4 Posts: 1,163 Rep: C Program based on Gregorian Calendar The Problem: If 01/01/1900 is monday...then find the day on every Jan 1st of the year entered through the keyboard. I have been thinking quite a lot about this program..trying to search around with some logic...but no! My level is too low to understand arrays, functions..etc. I've just done a couple of chapters..if-else and stuff..and I am given this program. Trying to figure out what is the logic like to find out what is the day after 365 days.. Still thinking..but no luck so far.. Finally decided to post and get some help. I finally found this link which helped me develop some ideas as to how the program is to be done: http://quasar.as.utexas.edu/BillInfo/doomsday.html I figured out that the closest Doomsday to the required Date is Jan 31 - 1/31 (2/0). For 19xx, the Doomsday is Wednesday. Now to calculate the doomsday for that particular year, the following procedure is to be followed xx as in 19xx The xx can be found out by 19xx%100 Q=xx/12 R=xx%12 S=R/4 Doomsday=Q+R+S Now, we have to count Q+R+S number of days ahead of Wednesday. If this Doomsday calculation results in a number less than 7, then there won't be a problem. But if it is a number greater than 7, then how can i write the code so that the addition starts again from 1? Like 3+7 should be 3! I hope I am going in the right direction! Also, just thought about the Leap Year thing. There has to be two conditions. If it is a leap year, then doomsday falls on 2/1 (2-Feb...1-1st Day) If it is not, then it falls on 1/31. I suppose I will have to use an if-else to sort out the two conditions.
 07-10-2006, 11:59 AM #2 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: Assuming that you are not expected to come up with any date earlier than the one given. Your calculations can be fairly simple.Calculate the number of days between your start date and the requested date Find the remainder when you divide by seven That will give you the day of the week, where monday == 0, tuesday == 1,..., Sunday == 6. So the crux of the problem is in step 1 Break this down into three partsHow many whole years between the start and end dates How many whole months between the start and end months How many day between the start and end days Again the only tricky part is in the first step. Each year has 365 days so multiple the number by 365. Then think about the number of leap years, a leap year occurs every four years, so divide the number of years that you have by four. The big exception... 1900 was not a leap year (you may want to read about leap centuries) in which case subtract one from that total. I've left a few thing for you to work out for yourself, but that should help you out.
 07-10-2006, 12:57 PM #3 jim mcnamara Member   Registered: May 2002 Posts: 964 Rep: FWIW - C has all of this stuff built into the standard library. Because of locale and other things you are not aware of (like when the country in question running this code adopted the Gregorian calendar) you're always better off using the standard library if you can. Depending on your implmenetation (some will not work before Jan 1, 1970) this will handle every year except 1900 & 1901. Code: ```#include #include #define STDSZ 64 /* day of week dow() */ char *dow(char *dest,const char *date) { struct tm one={0,0,0,0,0,0,0,0,0}; char *p=NULL; *dest=0x0; /* make dest zero length */ p=strptime(date, "%d/%m/%Y", &one); if(p!=NULL) /* if date is valid, */ { /* otherwise return dest as zero length */ strftime(dest, STDSZ, "%A", &one); } return dest; } int main() { int i=1900; char date[STDSZ]={0x0}; char result[STDSZ]={0x0}; for(;i<2039;i++) /* 2038 = max allowable */ { sprintf(date,"01/01/%d",i); printf("01/01/%d %s\n",i, dow(result,date) ); } return 0; }```
 07-14-2006, 02:00 PM #4 duffmckagan Senior Member   Registered: Feb 2005 Distribution: Cent OS 6.4 Posts: 1,163 Original Poster Rep: Finally I made the program...the program can be found at the following link: http://copperskullcprogramming.blogs...ay-on-1st.html