Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   calendar program (http://www.go4expert.com/forums/calendar-program-t620/)

 musicmancanora4 9Mar2006 15:07

calendar program

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

 shabbir 9Mar2006 19:46

Re: calendar program

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: C

`int month[]={{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};`

Then check for Leap Year
Code: C

`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: C

`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

 coderzone 9Mar2006 21:47

Re: calendar program

Too fast Shabbir. Well done.

 shabbir 9Mar2006 22:35

Re: calendar program

Thanks.

 musicmancanora4 10Mar2006 06:12

Re: calendar program

thnxs buddy

 musicmancanora4 10Mar2006 06:19

Re: calendar program

By the way could you explain a little more how that void days function works and how it prints it?

thnxs

 shabbir 10Mar2006 07:22

Re: calendar program

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.

 musicmancanora4 10Mar2006 07:31

Re: calendar program

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?

 shabbir 10Mar2006 10:37

Re: calendar program

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.

 All times are GMT +5.5. The time now is 12:34.