calendar program

Discussion in 'C' started by musicmancanora4, Mar 9, 2006.

  1. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys i have to create a program which is based on the unix calendar command which shows the ascii calendar in the format of

    example:

    March
    mon tu wed thu fri sat sun
    1 2 3 4 5 6
    7 8 etc etc


    But the program will run by typing in a month from 0 - 12 , 0 is suppose to be valid because if the user types 0 it will display all months. But if the user types 1 for example it will just display january. But after you have typed in the month you type in the year considering the leap years then it will display the month.


    I have done some input for both month and year functions but how will i create teh rest of the algorith could someone explain it very simply please? thnxs

    joey
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    I have the program ready but would like to give a part of it so that you can work on the main logic and go ahead.

    Initialize the variables as
    Code:
    int month[]={{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};
    Then check for Leap Year
    Code:
    if((yr%4==0)&&((yr%100!=0)||(yr%400==0)))
        month[1]=29;
    
    Then you can use the below code to display the calender
    Code:
    void days(int *m,int *yr)
    {
    	int i,md=0,leap=0,track;
    	unsigned int d,yrd;
    	for(i=Y;i<*yr;i++)
    	{
    		if((i%4==0)&&((i%100!=0)||(i%400==0)))
    			leap++;
    	}
    	for(i=0;i<(*m-1)&&(i<11);i++)
    		md=md+month[i];
    	yrd=(*yr-Y)*365;
    	d=yrd+leap+md;
    	track=d%7;
    	display(&track);
    }
    void display(int *track)
    {
    	int t,dt,loop;
    	t=*track;
    	printf("\n\n\n\n\t\t");
    	printf("Mon\tTues\tWed\tThurs\tFri\tSat\tSun\n\n");
    	for(loop=0;loop<t+2;loop++) /*t+2 due to two additional \t*/
    		printf("\t");
    	for(dt=1;dt<=month[m-1];dt++)
    	{
    		if(t%7==0 && t!=0)
    			printf("\n\n\t\t");
    		printf("%d\t",dt);
    		t++;
    	}
    }
    Thanks
    Shabbir
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    Too fast Shabbir. Well done.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
  5. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
  6. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    By the way could you explain a little more how that void days function works and how it prints it?


    thnxs
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Simple it checks how many leap years exists between the current year that is passed as parameter and Y.

    Then it adds up all the days of the month for the calender year.

    Then it calculates the days in the year, month and week.

    Then it displays them all through the display function by getting the array.
     
  8. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Also can i ask im getting a funny error when im intializing the variable array of months

    braces around scalar initializer error

    Where should i actually intialize the variable in main or in a seperate function because most likely all the other functions would have to use the array ?

    Code:
    int main(void)
    {
       unsigned int month [] = {{31}, {28}} /* etc etc*/
       unsigned long year;
       
       getMonth(&month)
       getYear(&year);
       
       return EXIT_SUCCESS;
    }
    
    unsigned getMonth(unsigned int *month)
    {
    }
    
    
    
    
    
    And is it a good idea to work out the leap year in a seperate function then call it in the main?
    Just trying to get an idea how it should be designed as well?
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Its a Global variable. I just did not want to make it passing to all the functions.

    Design can always be improved as I have just mad the algo to work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice