I am trying to write a program in C with a main function and five other functions: input, output, calculate day of year for a calendar date, calculate calendar date from day of year and year, and determine if its a leap year function. I have a problem with calendar date from day of year and year function. I get the day of year and the year in the out put but I can't get the day and the month right.

This is the input:

```
5 4 1987
2 22 2004
8 9 2000
2 29 1900
3 2 2001
1 8 1999
9 31 2001
9 30 201
12 31 2004
22 9 2000
2 29 2000
```

and this should be the output:

```
Day of Year Calendar Date
124 05/04/1987
53 02/22/2004
222 08/09/2000
61 03/02/2001
8 01/08/1999
366 12/31/2004
60 02/29/2000
```

but i get this as an output:

```
Day of Year Calendar Date
124 2/ 2/1987
53 2/ 2/2004
222 2/ 2/2000
61 2/ 2/2001
8 2/ 2/1999
366 2/ 2/2004
Press any key to continue . . .
```

This is the C code I have for the program. The function I have problem with is calculateCalendarDate

```
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 13
#define MAX_ELEMS 100
int main (void)
{
// Function Declarations
int getCalendarDates ( int daysOfMonth [], int daysArray [], int yearsArray [] );
void writeResults ( int daysArray [], int yearsArray [], int daysOfMonth [], int numElems );
int convertDateToDayOfYear ( int daysOfMonth [], int month, int day, int year );
void calculateCalendarDate ( int daysOfMonth [], int dayOfYear, int year, int *month, int *day );
int determineLeapYear ( int year );
// Local Definitions
int daysOfMonth [MONTHS] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int daysArray [MAX_ELEMS];
int yearsArray [MAX_ELEMS];
FILE *fpInput; /* input textfile variable */
FILE *fpOutput; /* output textfile variable */
int numElems; /* number of elements read into array */
int i;
int dayOfYear;
// Statements
numElems = getCalendarDates ( daysOfMonth, daysArray, yearsArray );
writeResults ( daysArray, yearsArray, daysOfMonth, numElems );
system("pause");
return 0;
}
int getCalendarDates ( int daysOfMonth [], int daysArray [], int yearsArray [] )
{
// Local Declarations
int day;
int month;
int year;
int dayOfYear;
int i = 0;
FILE*fpInput;
// Statements
if (!(fpInput = fopen ("Prog1-TestIn1.txt", "r")))
printf("Error opening file\n"), exit (100);
while (fscanf(fpInput, "%d %d %d", &month, &day, &year) != EOF && i < MAX_ELEMS )
{
dayOfYear = convertDateToDayOfYear ( daysOfMonth, month, day, year );
if(dayOfYear != -1)
{
// store in arrays
yearsArray[i] = year;
daysArray [i] = dayOfYear;
i++;
}
}
return i;
} // getData
void writeResults ( int daysArray [], int yearsArray [], int daysOfMonth [], int numElems )
{
int i;
int dayOfYear;
int month;
int day;
int year;
printf("Day of Year\tCalendar Date\n");
for (i=0; i < numElems; i++)
{
printf("%6d\t\t", daysArray [i]);
calculateCalendarDate ( daysOfMonth, dayOfYear, year, &month, &day );
printf("%2d/%2d/%4d\n", month, day, yearsArray [i]);
}
}
int convertDateToDayOfYear ( int daysOfMonth [], int month, int day, int year )
{
int i;
int sum;
int dayOfYear;
if( month >= 1 && month <= 12 && day >= 1 && day <= daysOfMonth [month] && year > 1000 && year < 9999 )
if( determineLeapYear ( year ) == 1 && month > 2 || day > 28 )
for( i=0, sum=0; i < month; i++ )
{
sum += daysOfMonth [i];
dayOfYear = sum + day + 1;
}
else
for( i=0, sum=0; i < month; i++ )
{
sum += daysOfMonth [i];
dayOfYear = sum + day;
}
else
return -1;
return dayOfYear;
}
void calculateCalendarDate ( int daysOfMonth [], int dayOfYear, int year, int *month, int *day )
{
int daysinmonth;
int i;
int sum;
int daysin;
for (i = 1; i < 13; i++)
{
if (dayOfYear <= (daysinmonth = daysOfMonth[i - 1] + (i == 2 && determineLeapYear(year))))
break;
dayOfYear -= daysinmonth;
}
*month = i;
*day = dayOfYear;
return ;
}
int determineLeapYear ( int year )
{
if( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 )
return 1; /* a leap yaer */
else
return 0; /* not a leap year */
}
```

Thank you!