Thread: Calendar in C

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Portugal
    Posts
    5

    Calendar in C

    I guys

    I'm learning the basic C language from a website (CodingUnit Programming Tutorials).

    I don't know too much about it yet but my purpose is to do some cool programs just for fun and learning some things in the process.

    I found in that site a calendar made in C. I'm trying to change the presentation when debug it. The months are in a list type presentation and what I want to do is put them side by side like this:

    ------------Jan--------------------------------------Feb
    Sun Mon Tue Wed Thu Fri Sat------------Sun Mon Tue Wed Thu Fri Sat

    ------------Mar-------------------------------------April
    Sun Mon Tue Wed Thu Fri Sat------------Sun Mon Tue Wed Thu Fri Sat


    I already tried everything but I canīt find a way to do this right Each line will cover two months and that's the main issue. Can someone help me? It's the first time I see something like this. When I made a tic-tac-toe game in C I remember being stuck with a similar problem but I found the solution.

    PS: How does the daycode is calculated? :S It's the only thing I don't understand

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define TRUE    1
    #define FALSE   0
    
    int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    char *months[]=
    {
    	" ",
    	"\n\n\nJanuary",
    	"\n\n\nFebruary",
    	"\n\n\nMarch",
    	"\n\n\nApril",
    	"\n\n\nMay",
    	"\n\n\nJune",
    	"\n\n\nJuly",
    	"\n\n\nAugust",
    	"\n\n\nSeptember",
    	"\n\n\nOctober",
    	"\n\n\nNovember",
    	"\n\n\nDecember"
    };
    
    int inputyear(void)
    {
    	int year;
    
    	printf("Please enter a year (example: 1999) : ");
    	scanf("%d", &year);
    	return year;
    }
    
    int determinedaycode(int year)
    {
    	int daycode;
    	int d1, d2, d3;
    
    	d1 = (year - 1.)/ 4.0;
    	d2 = (year - 1.)/ 100.;
    	d3 = (year - 1.)/ 400.;
    	daycode = (year + d1 - d2 + d3) %7;
    	return daycode;
    }
    
    int determineleapyear(int year)
    {
    	if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
    	{
    		days_in_month[2] = 29;
    		return TRUE;
    	}
    	else
    	{
    		days_in_month[2] = 28;
    		return FALSE;
    	}
    }
    
    void calendar(int year, int daycode)
    {
    	int month, day;
    	for ( month = 1; month <= 12; month++ )
    	{
    		printf("%s", months[month]);
    		printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );
    
    		// Correct the position for the first date
    		for ( day = 1; day <= 1 + daycode * 5; day++ )
    		{
    			printf(" ");
    		}
    
    		// Print all the dates for one month
    		for ( day = 1; day <= days_in_month[month]; day++ )
    		{
    			printf("%2d", day );
    
    			// Is day before Sat? Else start next line Sun.
    			if ( ( day + daycode ) % 7 > 0 )
    				printf("   " );
    			else
    				printf("\n " );
    		}
    			// Set position for next month
    			daycode = ( daycode + days_in_month[month] ) % 7;
    	}
    }
    
    int main(void)
    {
    	int year, daycode, leapyear;
    
    	year = inputyear();
    	daycode = determinedaycode(year);
    	determineleapyear(year);
    	calendar(year, daycode);
    	printf("\n");
    	system("pause");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Portugal
    Posts
    5
    I have post this thread in 4 forums because I want to know different points of views about my problem. Anyway I'm working on the solution that Salem gave me.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Prolapsis View Post
    I want to know different points of views about my problem.
    You couldn't possibly get different points of view on a single forum. Everyone here agrees with everything I say all the time!

    Innat right fellas?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    You couldn't possibly get different points of view on a single forum. Everyone here agrees with everything I say all the time!

    Innat right fellas?


    Quzah.
    And if we don't, we never hear the end of it...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    For points of view, read "No one is doing my homework for me on forum A, so I'm going to wander from forum to forum collecting snippets of progress, then repost them elsewhere as my own work. Eventually, I hope to achieve homework by successive approximation".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Location
    Portugal
    Posts
    5
    Quote Originally Posted by Salem View Post
    For points of view, read "No one is doing my homework for me on forum A, so I'm going to wander from forum to forum collecting snippets of progress, then repost them elsewhere as my own work. Eventually, I hope to achieve homework by successive approximation".
    I just want to know how to do this in many possible ways. I have already one solution that does what I want but I'm not going to use it since I don't understand it 100%. I want to learn even if someone send me the right code.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Prolapsis View Post
    I just want to know how to do this in many possible ways. I have already one solution that does what I want but I'm not going to use it since I don't understand it 100%. I want to learn even if someone send me the right code.
    If you don't understand the solution... I'll guarantee you don't understand the problem.

    The goal here is to learn, first, how to analyse a problem to derive a step by step solution then, second, to translate your solution into C syntax. There is no programmer on the face of this Earth who can code a solution to a problem he or she does not understand.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Prolapsis View Post
    I just want to know how to do this in many possible ways. I have already one solution that does what I want but I'm not going to use it since I don't understand it 100%. I want to learn even if someone send me the right code.
    Nobody wants to write a calendar program for a thief. If you studied the problem yourself and wrote the program yourself, you'd understand it.

    And as for why cross posting is bad, it's because the many good people here are spending their time working on helping you, and you're off getting the answer elsewhere, so you just wasted all our time.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Location
    Portugal
    Posts
    5
    Ok. It seems that all of you never tried to get some ideas trough other programs. In the program I made, the only thing that I used was the daycode calculation. Everything else I made myself. And the solution I have isn't made by me. It was some guy who told me that I could do using structs. Since I don't understand much about structs I'm still working on the solution that Salem gave me. I'm no thief. Everything I know about C it's because of some book that I read and the solved exercises and examples on it.

    Thanks anyway guys. Don't need to be so aggressive with people just because they have trouble and ask for help. If you don't want to help because you think I'm a thief just say no or don't say anything. I'm going to make the program by myself. If I still can ask for help later is this forum say something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  2. C++ calendar HELP!!!
    By chocha7 in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2004, 08:06 AM
  3. Calendar ALMOST DONE!!!
    By ftblstr2319 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 12:30 AM
  4. calendar
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-10-2002, 04:10 PM
  5. Calendar
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-04-2002, 04:57 AM