In: Computer Science
C Programming Please write a single function and not the whole program
Can you please provide comments and explanations for all questions. Thank you
Write a single function for the following:
void split_date(int day_of_year, int
year, int *month, int *day);
day_of_year is an integer between 1 and 366, specifying a particular day within the year
designated by the parameter year.
Month and day point to variables in which the function
will store the equivalent month (1 – 12) and day within the month
(1 – 31)
The question is trying to ask you to write a single function in C that takes a day of the year (1-366) and calculates which month and which day of the month corresponds to that day of in the month. So a day of the year of let's 15 would return 1 (for the month - January) and 15 for the day of the month. The second parameter (year) is needed because leap years have 366 days and non-leap years have 365 days.
int check_Leap(int year)
{
if( (year%4 == 0 && year%100 != 0) || (year%400 == 0)
)
return 1; //is a leap year
else
return 0; //is not a leap year
}
void split_date(int day_of_year, int year, int *month, int
*day)
{
//At first check whether the given year is a leap year
or not
//We are storing 0 in is_Leap_Year variable if it is
not a leap year
//else 1 for a leap year using function
check_Leap()
int is_Leap_Year = check_Leap(year);
if (day_of_year <1 || day_of_year>366)
printf("Invalid Day of
Year\n");
//if user enters day_of_year out of
the range
else if(year<1000 || year>9999)
printf("Invalid year\n");
//if user enters year out of the
range
else
{
//initializing total number of days
in every month in an array
int month_days[] =
{31,28,31,30,31,30,31,31,30,31,30,31};
//changing total number of days in
February as 29
//if year is a leap year
month_days[1] +=
is_Leap_Year;
//e.g. for 2016 as leap year,
month_days[1] = 28 + 1
int i, d = day_of_year;
for(i=0; d > month_days[i];
i++)
d -=
month_days[i];
//here in this for loop, total
number of days of every month is
//being subtracted from day_of_year
until day_of_year becomes
//less than total number of days in
a particular month.
//after loop ends, i stores the
actual value of month
//so we store the month's
answer
*month = ++i;
//since the array month_days[] has
0 index counting,
//so February has indexing 1. We
incremented i to store 2 for
//February in month.
*day = d;
//the remaining value of d holds
the actual answer for date
//when d becomes less than total
number of days in a month
//then d is the date we are looking
for.
}
}
INPUT: day_of_year: 234 year:
2008
OUTPUT: *day = 21 *month = 8
Explanation:
Since 2008 is a leap year, month_days[] becomes
month_days[] = {31,29,31,30,31,30,31,31,30,31,30,31}
Now checking for loop:
d = 234;
for(i=0; d > month_days[i]; i++)
d -= month_days[i];
#Pass 0:
234 > month_days[0] ----> 234>31 ---> YES
Subtract: d = 234-31 = 203
i++;
#Pass 1:
203 > month_days[1] ----> 203>29 ---> YES
Subtract: d = 203-29 = 174
i++;
#Pass 2:
174 > month_days[2] ----> 174>31 ---> YES
Subtract: d = 174-31 = 143
i++;
#Pass 3:
143 > month_days[3] ----> 143>30 ---> YES
Subtract: d = 143-30 = 113
i++;
#Pass 4:
113 > month_days[4] ----> 113>31 ---> YES
Subtract: d = 113-31 = 82
i++;
#Pass 5:
82 > month_days[5] ----> 82>30 ---> YES
Subtract: d = 82-30 = 52
#Pass 6:
52 > month_days[6] ----> 52>31 ---> YES
Subtract: d = 52-31 = 21
i++;
#Pass 7:
21 > month_days[7] ----> 21>31 ---> NO
EXIT from the Loop
So day becomes 21 and month becomes ++i = 8.