Beginner
Beginner

Reputation: 29

I have some difficulties commenting and understanding the calendar program

// This calendar example is provided by: // http://www.codingunit.com Programming Tutorials

#include<stdio.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");
}

for example why there's a zero in days_in_month[]?

we learned about arrays, pointers and strings only, but the doctor asked us to do a complete project and to explain everything in it.I can understand most of it(I'm doing a lot of researches) but I need a clearer explanation of it. can you help me to comment this program please

Upvotes: 1

Views: 148

Answers (2)

fluter
fluter

Reputation: 13816

Arrays in C are zero based, that means the index of the first element is 0, not 1. The code you saw index the array with natural months, so the array has 13 elements, and the first element are only a placeholder, it is not used.

The result of this is, to get the number of first month, you can use do days_in_month[month], no subtract by 1 needed, i.e. if it were 12 elements as you thought, you will need to do days_in_month[month - 1]. See below chart:

int days_in_month[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//         index:    0  1   2   3   4   5   6   7   8   9   10  11  12

Upvotes: 1

Mohamed Moanis
Mohamed Moanis

Reputation: 507

For the why there is a zero in days_in_month?

Arrays are zero indexed; first element is at index 0 while the last one in this array will be at (12 - 1). So instead of using index 0 to refer to the month number 1, he just added a dummy element at the beginning.

Upvotes: 1

Related Questions