LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-10-2006, 10:08 AM   #1
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Rep: Reputation: 49
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.
 
Old 07-10-2006, 10:59 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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 parts
  • How 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.
 
Old 07-10-2006, 11:57 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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 <time.h>
#include <stdio.h>
#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;    
}
 
Old 07-14-2006, 01:00 PM   #4
duffmckagan
Senior Member
 
Registered: Feb 2005
Distribution: Cent OS 6.4
Posts: 1,163

Original Poster
Rep: Reputation: 49
Finally I made the program...the program can be found at the following link:

http://copperskullcprogramming.blogs...ay-on-1st.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to share between Outlook Calendar and a Linux calendar program domino1241 Linux - General 2 07-09-2006 09:13 PM
MS Office client - Linux host based shared calendar testerman Linux - Software 0 02-05-2004 09:42 AM
Calendar Program? digger Linux - Software 3 02-02-2004 07:57 AM
prompt based calendar program? Calendar? thebeaglebeagle Linux - Software 0 09-15-2003 01:10 PM
Calendar program - need name PhilD Linux - Software 8 06-27-2003 09:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration