Thursday 8 August 2013

The First Day of a Month And The Number of Days In It

Yesterday, I was writing about finding a leap year. I included it in my main program, yesterday, and expanded a bit further based on the fact that 1st January, 1900 was a Monday.
It's a function that will accept 4 integers:
1. Month (in int form 1 for January, 2 for February etc.)
2. Year
3. Another Int 1
4. Another int 2.

After it runs, "Another Int 1" will contain the number of days in that month,
"Another Int 2" will contain the first day of that month (1=Sunday, 2=Monday...etc)

I needed this function to draw the calender for a month the user specifies. Here's the function used in a simple program (NOTE: Please make sure you are trying to compile the file as a .C file and NOT .CPP. The program will not compile as a .CPP file).

#include <stdio.h>
#include <strings.h>

void find_days(int month,int year,int *dates,int *day);

main()
{
    int month,year,num_of_days,first_day;
    char day[10];

    printf("Enter Month (1 for Jan, 2 for Feb etc.):");
    scanf("%i",&month);
    printf("Enter Year:");
    scanf("%i",&year);
    find_days(month,year,&num_of_days,&first_day);

    switch(first_day)
    {
    case 1:
        strcpy(day,"Sunday");
        break;
    case 2:
        strcpy(day,"Monday");
        break;
    case 3:
        strcpy(day,"Tuesday");
        break;
    case 4:
        strcpy(day,"Wednesday");
        break;
    case 5:
        strcpy(day,"Thursday");
        break;
    case 6:
        strcpy(day,"Friday");
        break;
    case 7:
        strcpy(day,"Ssturday");
        break;
    }

    printf("It has %i days and the first day is a %s",num_of_days,day);
}

void find_days(int month,int year,int *dates,int *day)
{
    int leap_year,loop;
    int loop_day=2,loop_month=1,loop_year=1900,day_limit=31; //day is at 2 because it will start at monday etc.

    while(1)
    {
        /**First, if the month is 2 (february) find if the year is a leap year*/
        if((loop_year%4==0 && loop_year%100!=0) || (loop_year%400==0))
        {
            leap_year=1;
        }
        else
        {
            leap_year=0;
        }

        /**set up the months, with dates going up**/
        if(loop_month==1 || loop_month==3 || loop_month==5 || loop_month==7 || loop_month==8 || loop_month==10 || loop_month==12)
        {
            if(loop_month==month && loop_year==year)
            {
                *dates=31;
                *day=loop_day;
                break;
            }
            for(loop=0;loop<31;loop++)
            {
                loop_day++;
                if(loop_day==8)
                {
                    loop_day=1; //sunday
                }
            }
            loop_month++;
        }
        else if(loop_month==4 || loop_month==6 || loop_month==9 || loop_month==11)
        {
            if(loop_month==month && loop_year==year)
            {
                *dates=30;
                *day=loop_day;
                break;
            }
            for(loop=0;loop<30;loop++)
            {
                loop_day++;
                if(loop_day==8)
                {
                    loop_day=1; //sunday
                }
            }
            loop_month++;
        }
        else if(loop_month==2)
        {
            if(loop_month==month && loop_year==year)
            {
                if(leap_year==1)
                {
                    *dates=29;
                }
                else
                {
                    *dates=28;
                }
                *day=loop_day;
                break;
            }
            for(loop=0;loop<28+leap_year;loop++)
            {
                loop_day++;
                if(loop_day==8)
                {
                    loop_day=1; //sunday
                }
            }
            loop_month++;
        }
        if(loop_month==13)
        {
            loop_year++;
            loop_month=1;
        }
    }
}



No comments:

Post a Comment